I have coded everything and it isn’t giving me any errors and it uploads to the arduino fine, when change the “state” value something actually happens in real life but i cant get the state values to change using buttons ive tried so many things could anyone help me.
#define FLgreenLED 3 //defining which pins the LEDs are connected to
#define FRgreenLED 4 //LEDs are labelled by their position first then their color eg. FRgreenLED - green LED in the front right corner
#define FLredLED 5
#define FRredLED 6
#define RLredLED 7
#define RRredLED 8
#define RRGB 22 //Red portion of RGB LED
#define GRGB 24 //Green portion of RGB LED
#define BRGB 26 //Blue portion of RGB LED
#define LDR A0
//-----------fading setup-----------//
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
long IntervalMode1 = 6;
long IntervalMode2 = 7;
int brightness = 0;
int fadeAmount = 5;
int RedBrightness = 0;
int GreenBrightness = 255;
//----------button setup 1----------//
#define buttonPin 2
int state = 0;
int old = 0;
int buttonPoll = 0;
//----------button setup 2----------//
#define buttonPin2 9
int state2 = 0;
int old2 = 0;
int buttonPoll2 = 0;
void setup() {
pinMode(FLredLED, OUTPUT);
pinMode(FRredLED, OUTPUT);
pinMode(RLredLED, OUTPUT);
pinMode(RRredLED, OUTPUT);
pinMode(FLgreenLED, OUTPUT);
pinMode(FRgreenLED, OUTPUT);
pinMode(RRGB, OUTPUT);
pinMode(GRGB, OUTPUT);
pinMode(BRGB, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
Serial.begin(9600);
}
void loop() {
buttonPoll = digitalRead(buttonPin);
if (buttonPin == 1){
delay(50);
buttonPoll = digitalRead(buttonPin);
if (buttonPin == 0){
state = old + 1;
}
else
delay (100);
}
buttonPoll2 = digitalRead(buttonPin2);
if (buttonPin2 == 1){
delay(50);
buttonPoll2 = digitalRead(buttonPin2);
if (buttonPin2 == 0){
state2 = old2 + 1;
}
else
delay(100);
}
int val = analogRead(LDR);
Serial.println(val);
switch (state) {
case 1:
if (val<750){
digitalWrite(RRGB,HIGH);
digitalWrite(GRGB,HIGH);
digitalWrite(BRGB,HIGH);
}else{
digitalWrite(RRGB,LOW);
digitalWrite(GRGB,LOW);
digitalWrite(BRGB,LOW);
}
break;
case 2:
delay(100);
break;
default:
switch (state2) {
case 1:
if (val<750){
digitalWrite(RRGB,HIGH);
digitalWrite(GRGB,HIGH);
digitalWrite(BRGB,HIGH);
}else{
digitalWrite(RRGB,LOW);
digitalWrite(GRGB,LOW);
digitalWrite(BRGB,LOW);
}
// set the brightness of pin 9:
analogWrite(RLredLED, RedBrightness);
analogWrite(RRredLED, RedBrightness);
analogWrite(FLredLED, LOW);
analogWrite(FRredLED, LOW);
// set the brightness of pin 10:
analogWrite(FLgreenLED, GreenBrightness);
analogWrite(FRgreenLED, GreenBrightness);
// change the brightness for next time through the loop:
RedBrightness = RedBrightness + fadeAmount;
GreenBrightness = GreenBrightness - fadeAmount;
// reverse the direction of the fading at the ends of the fade:
// and blink the internal LED on pin 13 at the fade end point
if (RedBrightness == 0 || RedBrightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(6); // physiscal button acts up without this line and wont bridge the connection correctly
break;
default:
if (val<750){
digitalWrite(RRGB,HIGH);
digitalWrite(GRGB,HIGH);
digitalWrite(BRGB,HIGH);
}else{
digitalWrite(RRGB,LOW);
digitalWrite(GRGB,LOW);
digitalWrite(BRGB,LOW);
}
unsigned long currentMillis = millis(); // grab current time
analogWrite(FLredLED, brightness);// set the brightness of ledPin:
analogWrite(FRredLED, brightness);
analogWrite(RLredLED, brightness);
analogWrite(RRredLED, brightness);
digitalWrite(FLgreenLED,LOW);
digitalWrite(FRgreenLED,LOW);
if (currentMillis - previousMillis >= IntervalMode1){
brightness = brightness + fadeAmount; // change the brightness for next time through the loop:
previousMillis = millis();
}
if (brightness <= 0 )
{ // reverse the direction of the fading at the ends of the fade:
brightness = 0;
fadeAmount = -fadeAmount;
}
if (brightness >=255 )
{ // reverse the direction of the fading at the ends of the fade:
brightness = 255;
fadeAmount = -fadeAmount;
}
break;
}
break;
}
}