I’m new to Arduino, so please excuse if this is a newb question. I’m trying to add a fade time of 4 seconds to this code that I found from Robojax. I like the way that it currently works however, I’d like the would like the LED to fade up in 4 seconds on the first push, and then fade out in 4 seconds when the button is pushed again. Any assistance would be greatly appreciated.
int pbuttonPin = 2;// connect output to push button
int ledPin = 9;// Connected to relay (LED)
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
// Robojax.com code and video tutorial for push button ON and OFF
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);// keep the load OFF at the begining. If you wanted to be ON, change the HIGH to LOW
}
void loop() {
// Robojax.com code and video tutorial for push button ON and OFF
val = digitalRead(pbuttonPin);// read the push button value
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
lightON = val;
if(pushed == HIGH){
Serial.println("Light ON");
digitalWrite(ledPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(ledPin, HIGH);
}
// Robojax.com code and video tutorial for push button ON and OFF
delay(100);