help with conditional statements

Hello

I'm need some help with my condition statements for my "gear indicator" Arduino program for my manual car

my H pattern manual gearbox has 4 sensors (up,down,left right)
I'm also using the I2C protocol to transmit data between the Arduino boards

how it works so far.

//below is the shift pattern
1 3 5
| | |
N--N--N
| | |
2 4 R

I'm sending 5 volts to the sensors, if a sensor returns a value of 5 volts to Arduino that sensor gets the true state
i want the code to be efficient so if the conditional statements read that its in gear1, then dont tell the arduino to write again


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

 
While(I2C communication active ){// if both Arduino boards can talk to each other

   read voltage value from sensors (up,down,left,right) if its 5v mark the sensor as true else it's false

   if(stateOfSensorLeft == true){ // need to check if its left or right in the H pattern first
   
      if(stateofSensorUp == true){//gear1
          strcmp last gear with gear1 if false tell arduino to remove to display it's in gear1// need to remove before writing
 
          strcmp last gear with current if true tell arduino to write to display only once
          {
             current gear is now gear 1
             last gear is now gear 1
          }

      }
     if (stateOfSensorDown == true){

      strcmp last gear with gear1 if false tell arduino to remove to display it's in gear1// need to remove before writing


     }

    else if (stateOfSensorUp == false && stateOfSensorDown == false){// in between first/second gears print to display in netural

         start 20 second timer so netural will be sent to display after 20 seconds 
   }

   else if (stateOfsensordown == true){
   // same as above.
   }
   else if (stateOfSensorRight == true){
   //same as above
   }

   else if (stateOfSensorUp == true){

   else //if all sensors return false it will be in nutural
   start 20 second timer so netural will be sent to display after 20 seconds 
   // this stops the display glitching out if moved between gears to fast.


   }
}
 
if you want it efficient, this begs a lookup table or a switch statement.

one way to do it is to skip the booleans and use like a character.
something like this:
enum states{up = 1, down = 2, left = 4, right = 8}; //powers of 2, named

unsigned char state = stateOfSensorLeft*left + stateOfSensorRight * right + .... etc (bools are numerically 0 and 1 for false and true, you can use them as these integers when necessary).

this gives you a number: state is an integer.
now you can switch off it, so if you want where the thing is up and left are both true, that is up+left... which is 5. or in binary, 00101 (4+1) so you can case 5: in the switch. Multiple cases that do the same thing can use switch fall-through.

Does this make sense?
you DO have to layout at least an empty case for 'don't cares' eg if you want up and left but down and right can be ANY value (not just false) then you need something like
case:5 (up, left only are true)
case:7 (up, left, and down are true)
case:13 (up, left, and right)
case:15 (up, left, down, right)
... you have to be very careful to trap every valid combination for each process you want to do.

**compilers are wired to try to convert switches into a type of lookup table.
Last edited on
Topic archived. No new replies allowed.