I am trying to control two lights using one switch. It works as intended the first cycle through the switchpush count (1,2,3,4) with the following exceptions:
In switchpush 2 the lamp increases in brightness (i.e. stripPin) until fully bright then it starts the cycle over again from dim to bright - I want the lamp to ramp up and stay bright (not start the dim to bright ramp up process again)
If I change the switchpush to 3,4 or 1 and then go back to switchpush 2 it does not zero out, it stays at the last ramp up count - I want it to start the ramp up from 0 each time I enter switchpush 2.
I would appreciate any recommendations.
//Variable will change
int switchState = 0;// state of switch,high or low
int switchPushCounter = 0; // counter for the number of pushes
int lastswitchState = 0; // previous state of switch
int brightnessF = 125;// start point where light starts to illuminate this is 125 out of 255
int brightnessS = 0;// start point where light starts to illuminate this is 125 out of 255
const int buttonPin = 2;
const int floodPin = 4;
const int stripPin = 5;
void setup() {
pinMode(floodPin, OUTPUT);
pinMode(stripPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
switchState = digitalRead(buttonPin); //Compare buttonPIN to its previous state
if (switchState != lastswitchState) { //if the state has changed, increment the counter
if (switchState == HIGH) { //if the current state is high then the button went from off to on
switchPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(switchPushCounter);
} else {
//if the current state is LOW thenthebutton went from on to off:
Serial.println("off");
}
}
delay(50);//Delay to avoid bouncing
lastswitchState = switchState;//save the current state as the last state, for next time through the loop
if (switchPushCounter % 4 == 1) {
analogWrite(floodPin, brightnessF);
brightnessF += 1;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessF);
} else {
digitalWrite(floodPin, LOW);
if (switchPushCounter % 4 == 2) {
analogWrite(stripPin, brightnessS);
brightnessS += 1;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessS);
} else {
digitalWrite(stripPin, LOW);
if (switchPushCounter % 4 == 3) {
digitalWrite(floodPin, HIGH);
digitalWrite(stripPin, HIGH);
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(floodPin);
Serial.println(stripPin);
} else {
digitalWrite(floodPin, LOW);
digitalWrite(stripPin, LOW);
}
}
}
}
@dallrax19, your code should be properly shown in your post if you remove the text "emphasized text" that is after the [/code]. You can edit your post.
This is the update and it is working at the max brightness. Thanks!!. Now to get it to reset to 0 when it enters theloop.
int switchState = 0;// state of switch,high or low
int switchPushCounter = 0; // counter for the number of pushes
int lastswitchState = 0; // previous state of switch
int brightnessF = 125;// start point where light starts to illuminate this is 125 out of 255
int brightnessS = 0;// start point where light starts to illuminate this is 125 out of 255
int max_brightnessS=255;
const int buttonPin = 2;
const int floodPin = 4;
const int stripPin = 5;
void setup() {
pinMode(floodPin, OUTPUT);
pinMode(stripPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
switchState = digitalRead(buttonPin); //Compare buttonPIN to its previous state
if (switchState != lastswitchState) { //if the state has changed, increment the counter
if (switchState == HIGH) { //if the current state is high then the button went from off to on
switchPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(switchPushCounter);
} else {
//if the current state is LOW thenthebutton went from on to off:
Serial.println("off");
}
}
delay(50);//Delay to avoid bouncing
lastswitchState = switchState;//save the current state as the last state, for next time through the loop
if (switchPushCounter % 4 == 1) {
analogWrite(floodPin, brightnessF);
brightnessF += 1;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessF);
} else {
digitalWrite(floodPin, LOW);
if (switchPushCounter % 4 == 2) {
analogWrite(stripPin, brightnessS);
brightnessS += 1;
if (brightnessS > max_brightnessS) brightnessS = max_brightnessS;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessS);
} else {
digitalWrite(stripPin, LOW);
if (switchPushCounter % 4 == 3) {
digitalWrite(floodPin, HIGH);
digitalWrite(stripPin, HIGH);
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(floodPin);
Serial.println(stripPin);
} else {
digitalWrite(floodPin, LOW);
digitalWrite(stripPin, LOW);
}
}
}
}
OK it looks like I am in business. I added a reset to the switch count. I found out it wasn't necessary but I left it in. I reset the brightness ramp up when the switch is pushed the third time. I appreciate the help!!!
int switchState = 0;// state of switch,high or low
int switchPushCounter = 0; // counter for the number of pushes
int lastswitchState = 0; // previous state of switch
int brightnessF = 125;// start point where light starts to illuminate this is 125 out of 255
int brightnessS = 0;// start point where light starts to illuminate this is 0 out of 255
int max_brightnessS = 255; // sets the maximum brightness
const int buttonPin = 2;
const int floodPin = 4;
const int stripPin = 5;
void setup() {
pinMode(floodPin, OUTPUT);
pinMode(stripPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
switchState = digitalRead(buttonPin); //Compare buttonPIN to its previous state
if (switchState != lastswitchState) { //if the state has changed, increment the counter
if (switchState == HIGH) { //if the current state is high then the button went from off to on
switchPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(switchPushCounter);
} else {
//if the current state is LOW thenthebutton went from on to off:
Serial.println("off");
}
}
delay(50);//Delay to avoid bouncing
lastswitchState = switchState;//save the current state as the last state, for next time through the loop
if (switchPushCounter % 5 == 1) {
analogWrite(floodPin, brightnessF);
brightnessF += 1;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessF);
} else {
digitalWrite(floodPin, LOW);
if (switchPushCounter % 5 == 2) {
analogWrite(stripPin, brightnessS);
brightnessS += 1;
if (brightnessS > max_brightnessS) brightnessS = max_brightnessS;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessS);
} else {
digitalWrite(stripPin, LOW);
if (switchPushCounter % 5 == 3) {
brightnessS = 0; // resets the brightness count back to zero so in push 2 the brightening cycle is reset
digitalWrite(floodPin, HIGH);
digitalWrite(stripPin, HIGH);
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(floodPin);
Serial.println(stripPin);
} else {
digitalWrite(floodPin, LOW);
digitalWrite(stripPin, LOW);
}
if (switchPushCounter % 5 == 4) {
(switchPushCounter = 0);// resets switch counter back to zero
}
}
}
}
OK this is the finalized project. I am posting it so other people looking for similar have it to reference:
1 Switch
2 Lamps
Push 1 Lamp 1 = on, Lamp 2 = off
Push 2 Lamp 1 = off, Lamp 2 = on with ramp up in brightness (wake up mode)
Push 3 Lamp 1= on, Lamp 2 = on with ramp up in brightness (wake up mode)
Push 4 Lamp 1= off, Lamp 2 = off
int switchState = 0;// state of switch,high or low
int switchPushCounter = 0; // counter for the number of pushes
int lastswitchState = 0; // previous state of switch
int brightnessF = 125;// start point where light starts to illuminate this is 125 out of 255
int brightnessS = 0;// start point where light starts to illuminate this is 0 out of 255
int brightnessS2 = 0;// start point where light starts to illuminate this is 0 out of 255
int max_brightnessS = 255; // sets the maximum brightness
const int buttonPin = 2;
const int floodPin = 4;
const int stripPin = 5;
void setup() {
pinMode(floodPin, OUTPUT);
pinMode(stripPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
switchState = digitalRead(buttonPin); //Compare buttonPIN to its previous state
if (switchState != lastswitchState) { //if the state has changed, increment the counter
if (switchState == HIGH) { //if the current state is high then the button went from off to on
switchPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(switchPushCounter);
} else {
//if the current state is LOW thenthebutton went from on to off:
Serial.println("off");
}
}
delay(50);//Delay to avoid bouncing
lastswitchState = switchState;//save the current state as the last state, for next time through the loop
if (switchPushCounter % 5 == 1) {
analogWrite(floodPin, brightnessF);
brightnessF += 1;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessF);
} else {
digitalWrite(floodPin, LOW);
if (switchPushCounter % 5 == 2) {
analogWrite(stripPin, brightnessS);
brightnessS += 1;
if (brightnessS > max_brightnessS) brightnessS = max_brightnessS;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessS);
} else {
digitalWrite(stripPin, LOW);
if (switchPushCounter % 5 == 3) {
digitalWrite(floodPin, HIGH);
analogWrite(stripPin, brightnessS2);
brightnessS2 += 1;
if (brightnessS2 > max_brightnessS) brightnessS2 = max_brightnessS;
Serial.println("on");
Serial.print("Brightness: ");
Serial.println(brightnessF);
Serial.println(brightnessS);
} else {
digitalWrite(floodPin, LOW);
digitalWrite(stripPin, LOW);
}
if (switchPushCounter % 5 == 4) {
(switchPushCounter = 0);// resets switch counter back to zero
brightnessS = 0; // resets the brightness count back to zero so in push 2 the brightening cycle is reset
brightnessS2 = 0; // resets the brightness count back to zero so in push 3 the brightening cycle is reset
}
}
}
}