Go Down

Topic: Relay Shield Code Issues (Read 382 times) previous topic - next topic

ahesselgrave

My project is a Jeopardy style table that has 4 switches. When one switch is pressed, it lights up its corresponding light and disables the other switches for 5 seconds, after which it returns back to a waiting state. Here is the code:
Code: [Select]

//Pressing 1 switch blocks the others and lights up a light through a relay for 5 seconds

//4 switches and 4 corresponding relays
// These are constant because it makes the code run faster on the microcontroller.  The compiler more or less does this.
const byte switchA = 2;
const byte switchB = 3;
const byte switchC = 4;
const byte switchD = 5;
const byte relayA = 6;
const byte relayB = 7;
const byte relayC = 8;
const byte relayD = 9;

// enumerated states so that things don't die immediately...
enum state_t { A = 1, B = 2, C = 3, D = 4, Wait = 5 };

void setup(){
  pinMode (switchA, INPUT_PULLUP);
  pinMode (switchB, INPUT_PULLUP);
  pinMode (switchC, INPUT_PULLUP);
  pinMode (switchD, INPUT_PULLUP);
  pinMode (relayA, OUTPUT);
  pinMode (relayB, OUTPUT);
  pinMode (relayC, OUTPUT);
  pinMode (relayD, OUTPUT);
}

void loop() {
  static state_t state = Wait;
  // Variables with static are initialized the first time a function runs.
  // These are non-global so that CS professors won't complain.
  switch(state) {
    case A:
      digitalWrite(relayA,HIGH);
      digitalWrite(relayB,LOW);
      digitalWrite(relayC,LOW);
      digitalWrite(relayD,LOW);
      delay(5000);
      digitalWrite(relayA,LOW);
      state = Wait;
    break;
    case B:
      digitalWrite(relayA,LOW);
      digitalWrite(relayB,HIGH);
      digitalWrite(relayC,LOW);
      digitalWrite(relayD,LOW);
      delay(5000);
      digitalWrite(relayB,LOW);
      state = Wait;
    break;
    case C:
      digitalWrite(relayA,LOW);
      digitalWrite(relayB,LOW);
      digitalWrite(relayC,HIGH);
      digitalWrite(relayD,LOW);
      delay(5000);
      digitalWrite(relayC,LOW);
      state = Wait;
    break;
    case D:
      digitalWrite(relayA,LOW);
      digitalWrite(relayB,LOW);
      digitalWrite(relayC,LOW);
      digitalWrite(relayD,HIGH);
      delay(5000);
      digitalWrite(relayD,LOW);
      state = Wait;
    break;
    case Wait:
      if (!digitalRead(switchA)) {
        state = A;
      } else if (!digitalRead(switchB)) {
state = B;
      } else if (!digitalRead(switchC)) {
state = C;
      } else if (!digitalRead(switchD)) {
state = D;
      } else {
state = Wait;
      }
    break;
    default:
      state = Wait;
    break;
  }
}

My friend helped me program it as a FSM but when I tested it the relay for case A immediately activated when power was applied. It may be a hardware issue but can anybody see any issues with the programming itself? Thank you.

johnwasser

The code looks OK. 

My guess is that the Switch A pin is shorted to ground.

If you hold down B does the relay for B trigger after A has been on for five seconds?  That would help prove that the rest of the hardware was working as expected.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

ahesselgrave

I'm not sure yet. I only just plugged it in to power and heard the click and tested relayA. I'll go test the other ones and report back.

Go Up