Hi guys, I’ve written some code to turn off and on, three LED’s but I want the cycle to be activated by a push button. i used a millus() function because I want to simultaneously sample the intensities of the LED’s using LDRS (using SARCduino SampleAndSend()). Now the code I’ve written works perfectly fine without the push button actuation, but doesn’t work at all when I add it in. Any ideas on how to remedy this problem?
/RGB COLOUR SENSOR v1.2/
const int LED_RED = 2; // LED connected to digital pin 2
const int LED_GREEN = 3; // LED connected to digital pin 2
const int LED_BLUE= 4; // LED connected to digital pin 2
const int button_Pin = 5; //Sample button pushed sets the program to run once
int counter_max = 5;
// variables will change:
int button_State = 0; // variable for reading the pushbutton status
int counter = 0;
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pins as an output:
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(button_Pin,INPUT);
Serial.begin(57600);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop(){
// read the state of the pushbutton value:
const int button_Pin = 5; //Sample button pushed sets the program to run once
button_State = digitalRead(button_Pin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (button_State = LOW){
if (millis()%1000==0)
{
if (counter<counter_max)
counter++;
else
counter=0;
Serial.print("counter = " );
Serial.print(counter);
// Then you can turn on or off the lights depending on the value of the counter, using a switch statement.
switch (counter) {
case 0: digitalWrite(LED_RED,HIGH);
break;
case 1: digitalWrite(LED_RED,LOW);
break;
case 2: digitalWrite(LED_GREEN,HIGH);
break;
case 3: digitalWrite(LED_GREEN,LOW);
break;
case 4: digitalWrite(LED_BLUE,HIGH);
break;
case 5: digitalWrite(LED_BLUE,LOW);
break;
}
}
}
}