Adding a lever switch to a pir/relay/led setup breaks the code

Hey so I’m trying to make a setup that works something like this:

Arduino makes the rgb leds fade in and out and controls the relay powering the screen (and other things) using the pir sensor. The leds are powered by an extrenal power supply also connected to the relay so if there’s no motion everything turns off.

My goal is to add a lever switch that can alternate between two modes. One described above and the other just keeping the relay on at all times.

I got pretty far using various examples on the internet but I’m no programmer and I cannot implement the lever switch without it freaking out the pir sensor.

This is as far as I got and everything works fine here:

int lamp = 4; // choose the pin for the RELAY
int inputPin = 2; // choose the input pin (for PIR sensor)
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

int val = 0; // variable for reading the pin status

void setup() { 
//led
setColourRgb(0,0,0);

//relay
pinMode(lamp, OUTPUT); // declare lamp as output

pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);

}

void loop(){
//led
 unsigned int rgbColour[3];
 // Start off with red.
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  
 // Choose the colours to increment and decrement.
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;
   // cross-fade the two colours.
   for(int i = 0; i < 255; i += 1) {
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     delay(5);
   }
 }
//relay
val = digitalRead(inputPin); // read input value

Serial.println(val);

if( val== 1) {

digitalWrite(lamp,HIGH); // turn ON the lamp

} else {

digitalWrite(lamp,LOW); // turn OFF the lamp

}

}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);
}

After that I tried adding a simple piece of code like this to the main one:

int pinButton = 3;
int LED = 11;

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(LED, OUTPUT);
}
void loop() {
  int stateButton = digitalRead(pinButton);
  if(stateButton == 1) { 
     digitalWrite(LED, HIGH); 
  } else { 
     digitalWrite(LED, LOW);  
  }
}

So it looks like this:

int relay = 4;
int pir = 2;
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

int pinButton = 3;

int val = 0; // variable for reading the pin status

void setup() { 
  //led
  setColourRgb(0,0,0);

  //relay and pir
  pinMode(relay, OUTPUT);
  pinMode(pir, INPUT);

  Serial.begin(9600);

  //button
  pinMode(pinButton, INPUT);

}

void loop(){
  //led
  unsigned int rgbColour[3];
  rgbColour[0] = 255;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(5);
    }
  }

    int stateButton = digitalRead(pinButton);
    if(stateButton == 1) { 
    
    //relay and pir
    val = digitalRead(pir);
    Serial.println(val);

    if( val== 1) {
      digitalWrite(relay,HIGH); 
    } else {
      digitalWrite(relay,LOW);

    } 
    } else { 
      digitalWrite(relay,HIGH); 
    } 


}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

And here’s where the pir freaks out. The relay keeps turning on and off every few seconds when on the switch is on the ‘motion sensor mode’ and when it’s on ‘all ON’ it seems to be working fine for a few minutes then also starts to turn on and off.

Any help appreciated.

Before anyone asks. Everything works when isolated even to the point of the code at the top. The lever switch works fine with simple code too. So I think it has to do with my programming skills which are almost non existant. Aaand the idea is exactly the same as ILikeToMakeStuff’s arcade cabinet but with my own spin on it so I cannot use his code

How is your switch wired? If you do not have a pullup or pull down resistor that is probably your issue.

If your switch is wired using GND then try configuring the switch input as:

pinMode(pinButton, INPUT_PULLUP);

ToddL1962:
How is your switch wired? If you do not have a pullup or pull down resistor that is probably your issue.

If your switch is wired using GND then try configuring the switch input as:

pinMode(pinButton, INPUT_PULLUP);

Thanks! That solved the turning on and off issue. The problem now is that everything acts as if the switch wasn't there at all or was always on 'pir sensor' mode.

The switch doesn't have a resistor and is connected to GND. I'll try giving the pull up/pull down resistors a shot.

wixa2:
Thanks! That solved the turning on and off issue. The problem now is that everything acts as if the switch wasn't there at all or was always on 'pir sensor' mode.

The switch doesn't have a resistor and is connected to GND. I'll try giving the pull up/pull down resistors a shot.

If you use INPUT_PULLUP then you don't need a pullup resistor, you are using the internal pullup resistor. I suspect the input was floating which caused all of the craziness.

It sounds as if you are now reading a HIGH from your input, which means it is never getting connected to GND by the switch. Recheck your wiring. I assume by "lever" switch you mean it is NOT a momentary switch?

ToddL1962:
If you use INPUT_PULLUP then you don’t need a pullup resistor, you are using the internal pullup resistor. I suspect the input was floating which caused all of the craziness.

It sounds as if you are now reading a HIGH from your input, which means it is never getting connected to GND by the switch. Recheck your wiring. I assume by “lever” switch you mean it is NOT a momentary switch?

Hey I got it to work! Great call with the INPUT_PULLUP I didn’t know anything about that. What I also should have guessed is that the internal pullup resistor works only if you connect the switch directly to the GND on arduino (I think so at least). Good thing it has 3 of them because I’m already using the other 2. The switch wasn’t momentary but maintained (I think that’s how it’s called).

Thanks again for help!

For anyone wondering here’s the final script.

int relay = 4;
int inputPin = 2; //pir
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
int pinButton = 3;
int val = 0;

void setup() { 
setColourRgb(0,0,0);
pinMode(pinButton, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(inputPin, INPUT);
Serial.begin(9600);

}

void loop(){
 unsigned int rgbColour[3];
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;
   for(int i = 0; i < 255; i += 1) {
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     delay(5);
   }
 }

 int stateButton = digitalRead(pinButton);
  if(stateButton == 0) {
val = digitalRead(inputPin);
Serial.println(val);

if( val == 1) {
digitalWrite(relay,HIGH);
} else {
digitalWrite(relay,LOW);
}
 }
 else {
     digitalWrite(relay,HIGH);
  }
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);
}

Just to help as you get more experienced, check your { } indentation manually, or with CTRL+T
It will make your code more readable both for yourself and others.

Good thing it has 3 of them because I’m already using the other 2

That’s what breadboards are for when prototyping

Glad it worked out for you!