Switch/case for multiple sensors

Hello!

I am writing a model program based off of the price is right that runs off of sensor triggers and holds up to two "spins" and then sends the score to a display. Everything was working for one sensor(one input), but when I tried to nest everything inside a for() loop for multiple sensors, I no longer seem to be switching into my "stopped" state. I need my code to actually switch into stopped state and then hold until the sensor that triggered that stopped state changes again.

NOTE: Stop should be triggered when: x == 0 AND x has stayed 0 for three seconds. (I am using N.O. hall effects sensors so unless it senses a magnet, x ==1) .

Here is my code below. I have not yet coded my reset button into my void loop, and as of now I am testing with only two sensors, but eventually there will be 20 sensors. I am also using an arduino mega 2560.

Thanks for checking this out, hoping someone can help me resolve this!



// Model Price is Right Wheel!!!!!!!!

enum {spinning, stopped}; //states of wheel for void looooop
unsigned char state; ///states for wheel

unsigned long startTime = 0; 
const long wait = 3000;
int x = 0;

int skipper = 7;
int resetter = 8;

typedef struct 
  {
    const int pin; //first term
    const int price; //second term
  } pinsandprice;

pinsandprice pandp[] = 
{
  {22, (05)}, 
  {23, (10)},
  {24, (15)},
  {25, (20)},
  {26, (25)},
  {27, (30)},
  {28, (35)},
  {29, (40)},
  {30, (45)},
  {31, (50)},
  {32, (55)},
  {33, (60)},
  {34, (65)},
  {35, (70)},
  {36, (75)},
  {37, (80)},
  {38, (85)},
  {39, (90)},
  {40, (95)},
  {41, (100)},
  {42, (200)},
};


const int list [] = {0, 0};//UP TO two spins per person
int uno = list[0];
int dos = list[1];
int z = uno + dos;


void STORESUMSEND(int w)
{
  if(uno == 0)
    {
     uno = w;
     Serial.println("FIRST");
    }
    
  else if(uno != 0 && dos == 0)
  {
    dos = w;
    Serial.println("SECOND");
    z = uno + dos; 
    Serial.print("SCORE: ");
    SCREEN(z);
  }  
}


void SCREEN(int z) 
{
//send the correct Z value to display
  Serial.write(z);
  Serial.println(z);
  uno = 0;
  dos = 0;
  z = 0;
}


void setup() {
  for (int k = 0; k < 2; k++) //eventually 20 sensors!!
  {
    pinMode(pandp[k].pin, INPUT);
  }
  pinMode(skipper, INPUT);
  pinMode(resetter, INPUT);
  Serial.begin(9600);
  

}

void loop() {

 if(digitalRead(skipper) == HIGH)
 { 
  SCREEN(uno + dos);
 }
 
 else
 {
 for (int j = 0; j < 2; j++)  //eventually 20 sensors!!
 {
   switch (state)
    {
      case spinning:
      {
       x = digitalRead(pandp[j].pin);
       if(x == 0);
          {
            startTime = millis();
            while((x==0) && (millis()-startTime < wait))
              {
                Serial.println("waiting...");
                x = digitalRead(pandp[j].pin);
              }
            if((millis() - startTime) >= wait)
              {
                state = stopped;
              }
          }
      }
      break;
  
      case stopped:
      {
        STORESUMSEND(pandp[j].price);
        while(x == 0)
          {
            x = digitalRead(pandp[j].pin);
          }
          
        state = spinning;
      }
      break; 
    }
   }
  }
}

A couple of questions

  1. where do you set the initial value of the state variable ?
  2. shouldn't each sensor have its own state variable ?

I wasn't aware I needed to! In the examples I had seen, and before I tried implementing multiple sensors, I had no problem switching states and everything was running perfectly without needing to declare anything initially...where would you recommend placing an initial state? And would that help fix my problem here?

Haha i don't know thats why I'm asking here! Eventually I will have 20 sensors so I was trying to find a concise way to expand the functionality out for 20 instead of just one. If I need to write each sensor individually I can, but I was worried about bogging my arduino down.

If you don't set the initial state then how do you know what the state will be when the sketch first executes switch (state) ?

It may be that the value of state is correct due to some happy accident or a default being applied, but why not do it explicitly when the variable is declared or in setup() ?

Looking more closely at your code, each sensor does not need its own state variable because the state relates to the whole system.

However, I thing that you have got things back to front and that the for loop that accesses each sensor should be inside the code for each state and not the other way round

In essence, given the state you should read and act on the state of each sensor

1 Like

Ok will do! Happy accident or not, I think your point still stands. Thanks for that!

BRILLIANT :)...I will rework some things and try to approach it through this format instead. Thanks! Do you happen to have an example of this type of thing? I am not all that experienced but could probably parse it together if I had a good reference.

I don't have a real example but something like this

switch (state)
{
case spinning:
  for (int s = 0; s < NUMBER_OF_DENSORS; s++)
  {
    //do stuff with sensor s
  }
  break;
case stopped:
  for (int s = 0; s < NUMBER_OF_DENSORS; s++)
  {
    //do stuff with sensor s
  }
  break;
};
1 Like

AWESOME! Thanks UKHeliBob, this totally worked! Big blessings :pray: :muscle:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.