Cant get AND to work

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Cant get AND to work

#1 Post by Affinite » 22 Jan 2024, 19:40

I am trying to make an ESP8266/ESPEASY wind direction sender using 8 reed switches.
Each switch is connected to a port on a GPIO expander chip (MCP23017) and I am reading the status of all pins in Rules.
I then use the pin status to set a Dummy task value coinciding with wind angle.
All works fine when I only use 1 pin at a time and ask "if" the status of a pin is High however if I try to put an "and" into my statement, the Dummy Device is not updated (even though the pin status does)
I am obviously doing something wrong in line 12 of my Rule but I cant see it.
Any advice on what Im doing wrong will be very welcome.

Code: Select all

on System#Boot do
   MCPmodeRange,1,8,2
   MonitorRange,MCP,1,8
   LoopTimerSet,1,2
endon

On Rules#Timer=1 do 
   If [Wind_N#State]=1
     TaskValueSet,Wind,Direction,0 // Sets the Dummy device task "Wind,Direction", value to "000 deg"
   Endif

   If [Wind_N#State]=1 And [Wind_NE#State]=1
     TaskValueSet,Wind,Direction,22.5 // Sets the Dummy device task "Wind,Direction", value to "22.5 deg"
   Endif
   
   If [Wind_NE#State]=1
     TaskValueSet,Wind,Direction,45 // Sets the Dummy device task "Wind,Direction", value to "045 deg"
   Endif
   
   If [Wind_E#State]=1 
     TaskValueSet,Wind,Direction,90 // Sets the Dummy device task "Wind,Direction", value to "090 deg"
   Endif
   
   If [Wind_SE#State]=1
     TaskValueSet,Wind,Direction,135 // Sets the Dummy device task "Wind,Direction", value to "135 deg"
   Endif
   
   If [Wind_S#State]=1
     TaskValueSet,Wind,Direction,180 // Sets the Dummy device task "Wind,Direction", value to "180 deg"
   Endif
   
   If [Wind_SW#State]=1 
     TaskValueSet,Wind,Direction,225 // Sets the Dummy device task "Wind,Direction", value to "225 deg"
   Endif
   
   If [Wind_W#State]=1
     TaskValueSet,Wind,Direction,270 // Sets the Dummy device task "Wind,Direction", value to "270 deg"
   Endif
   
   If [Wind_NW#State]=1 
     TaskValueSet,Wind,Direction,315 // Sets the Dummy device task "Wind,Direction", value to "315 deg"
   Endif   
   
endon

User avatar
Ath
Normal user
Posts: 3519
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Cant get AND to work

#2 Post by Ath » 22 Jan 2024, 19:53

You could start to use LogEntry to show the actual value of all (or a few) states of the switches:

Code: Select all

On Rules#Timer=1 Do
  LogEntry,"N=[Wind_N#State],NE=[Wind_NE#State],E=[Wind_E#State],SE=[Wind_SE#State],S=[Wind_S#State],SW=[Wind_SW#State],W=[Wind_W#State],NW=[Wind_NW#State]"
/Ton (PayPal.me)

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: Cant get AND to work

#3 Post by Affinite » 22 Jan 2024, 21:38

ath
Thanks, yes that was useful and I think Ive got my head around it now.

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Cant get AND to work

#4 Post by TD-er » 22 Jan 2024, 22:36

It makes perfect sense why it isn't working as you'd expect in your rules

Code: Select all

   If [Wind_N#State]=1 And [Wind_NE#State]=1
     TaskValueSet,Wind,Direction,22.5 // Sets the Dummy device task "Wind,Direction", value to "22.5 deg"
   Endif
   
   If [Wind_NE#State]=1
     TaskValueSet,Wind,Direction,45 // Sets the Dummy device task "Wind,Direction", value to "045 deg"
   Endif
   
When you have the state [Wind_NE#State]=1, this applies to both parts.
So what you did in the line with AND, you undo in the next if check.

In this particular case you could have used "elseif", but that's also a bit tricky.
So it is best to check the primaries first and then the combinations with 2 wind directions.

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: Cant get AND to work

#5 Post by Affinite » 23 Jan 2024, 12:45

TD-er
Thanks yes I see that now.
A bit off-topic but ...
I was looking at ath's suggestion about writing the status to the log and it led me to wonder if its possible to read the status of a range of MCP23017 GPIO pins (say 1-8) and return a binary word ?
I can then see how I would translate that status word to a wind direction eg "10100000" = "NE"
(I see in the plugin (P009_MCP) notes that I can set the state of a range of pins using a bit mask but it would be useful (in my application) to read the states as well

User avatar
Ath
Normal user
Posts: 3519
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Cant get AND to work

#6 Post by Ath » 23 Jan 2024, 12:57

In P061, available in any Collection build, you can get more bits at once from an MCP23017, when configured for 'Direct 16' output.
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Cant get AND to work

#7 Post by TD-er » 23 Jan 2024, 13:18

Not sure if it makes the rules a lot simpler.
You still need to perform the same checks.
I've been thinking about some proper bitshifts/XORs but I've not found any quick fix yet to turn the 4-bit value into a 3 bit value where you only need to multiply it with 45 degree to get the wind direction.
So I don't see how it can be done easily with less compares.

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: Cant get AND to work

#8 Post by Affinite » 23 Jan 2024, 14:11

Thx
I knew you'd be ahead of the game. :D
I have got it working now but I was trying to reduce the number of ElseIf's !

Affinite
Normal user
Posts: 57
Joined: 03 Apr 2018, 18:46

Re: Cant get AND to work

#9 Post by Affinite » 24 Jan 2024, 18:55

Just tried P061 in direct16 mode
It works fine in returning a scancode with each pin/switches activated independently but it doesn't work for my application as it doesn't issue a unique scancode when 2 switches are activated.

P009 * 8 tasks works OK but it would have been preferable to reduce the number of tasks down to 1. - Ill have to migrate to ESP32 to get some headroom for the other tasks I'm running but at least I now have working solution.
Thanks again for the advice guys

Code: Select all

on System#Boot do
   MCPmodeRange,1,8,2
   MonitorRange,MCP,1,8
endon

On MCP* do
   If [Wind_N#State]=1 And [Wind_NW#State]=0 And [Wind_NE#State]=0  
     TaskValueSet,Wind,Direction,00 // Sets the Dummy device task "Wind,Direction", value to "000 deg"
   Elseif [Wind_N#State]=1 And [Wind_NE#State]=1
     TaskValueSet,Wind,Direction,22.5 // Sets the Dummy device task "Wind,Direction", value to "22.5 deg"  
   Elseif [Wind_N#State]=0 And [Wind_NE#State]=1 And [Wind_E#State]=0
     TaskValueSet,Wind,Direction,45 // Sets the Dummy device task "Wind,Direction", value to "45 deg"  
   Elseif [Wind_NE#State]=1 And [Wind_E#State]=1
     TaskValueSet,Wind,Direction,67.5 // Sets the Dummy device task "Wind,Direction", value to "67.5 deg"  

   ElseIf [Wind_NE#State]=0 And [Wind_SE#State]=0 And [Wind_E#State]=1  
     TaskValueSet,Wind,Direction,90 // Sets the Dummy device task "Wind,Direction", value to "90 deg"
   Elseif [Wind_E#State]=1 And [Wind_SE#State]=1
     TaskValueSet,Wind,Direction,112.5 // Sets the Dummy device task "Wind,Direction", value to "112.5 deg"  
   Elseif [Wind_E#State]=0 And [Wind_SE#State]=1 And [Wind_S#State]=0
     TaskValueSet,Wind,Direction,135 // Sets the Dummy device task "Wind,Direction", value to "135 deg"  
   Elseif [Wind_SE#State]=1 And [Wind_S#State]=1
     TaskValueSet,Wind,Direction,157.5 // Sets the Dummy device task "Wind,Direction", value to "157.5 deg"  

   ElseIf [Wind_SE#State]=0 And [Wind_S#State]=1 And [Wind_SW#State]=0
     TaskValueSet,Wind,Direction,180 // Sets the Dummy device task "Wind,Direction", value to "180 deg"
   Elseif [Wind_S#State]=1 And [Wind_SW#State]=1
     TaskValueSet,Wind,Direction,202.5 // Sets the Dummy device task "Wind,Direction", value to "202.5 deg"  
   Elseif [Wind_S#State]=0 And [Wind_SW#State]=1 And [Wind_W#State]=0
     TaskValueSet,Wind,Direction,225 // Sets the Dummy device task "Wind,Direction", value to "225 deg"  
   Elseif [Wind_SW#State]=1 And [Wind_W#State]=1
     TaskValueSet,Wind,Direction,247.5 // Sets the Dummy device task "Wind,Direction", value to "247.5 deg"  

   ElseIf [Wind_SW#State]=0 And [Wind_W#State]=1 And [Wind_NW#State]=0
     TaskValueSet,Wind,Direction,270 // Sets the Dummy device task "Wind,Direction", value to "270 deg"
   Elseif [Wind_W#State]=1 And [Wind_NW#State]=1
     TaskValueSet,Wind,Direction,292.5 // Sets the Dummy device task "Wind,Direction", value to "292.5 deg"  
   Elseif [Wind_W#State]=0 And [Wind_NW#State]=1 And [Wind_N#State]=0
     TaskValueSet,Wind,Direction,315 // Sets the Dummy device task "Wind,Direction", value to "315 deg"  
   Elseif [Wind_N#State]=1 And [Wind_NW#State]=1
     TaskValueSet,Wind,Direction,337.5 // Sets the Dummy device task "Wind,Direction", value to "337.5 deg"  

   Else
     TaskValueSet,Wind,Direction,999 // Sets the Dummy device task "Wind,Direction", Fault condition value to "999 deg"    
   Endif
endon

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 28 guests