Hello, I have an LED line and a switch lever (if that's what they're called) and with every switch I want to switch a mode. Here's the code:
int modePin = 7;
int LEDPin = 11;
int lowPin = 2;
int highPin = 4;
int mode = 1;
int mButtonState = 0;
int mOldButtonState = 0;
void setup() {
Serial.begin(9600);
pinMode(modePin, INPUT_PULLUP);
pinMode(highPin, INPUT_PULLUP);
pinMode(lowPin, INPUT_PULLUP);
pinMode(LEDPin, OUTPUT);
}
void loop() {
chechkForMode();
while(mode == 1){
chechkForMode();
}
while(mode == 2){
chechkForMode();
}
while(mode == 3){
chechkForMode();
}
}
void chechkForMode(){
mButtonState = digitalRead(modePin);
if(mButtonState != mOldButtonState){
mode = mode++;
if(mode>3){
mode=1;
}
mOldButtonState = mButtonState;
}
Serial.print(mButtonState);
Serial.print(", ");
Serial.print(mOldButtonState);
Serial.print(", ");
Serial.println(mode);
}
But for some reason mode always stays 1. Can someone please help me. Thank you.
You mean I spent an hour looking what's the problem and it's this? :DDDDD Thank you very much
I think the result of
mode = mode++;
Is undefined, but you could have used
mode++;
// Or
mode += 1;
// Or
mode = mode + 1;
1 Like
Hello, I have an LED line and a switch lever (if that's what they're called) and with every switch I want to switch a mode. There's also two buttons which control the speed of waves and will later control the intervals between pulses in mode 3. Here's the code:
int modePin = 7;
int LEDPin = 11;
int lowPin = 2;
int highPin = 4;
int mode = 1;
int mButtonState = 0;
int mOldButtonState = 0;
int hButtonState = 0;
int hOldButtonState = 0;
int lButtonState = 0;
int lOldButtonState = 0;
float brightness = 255;
float waveLightSpeed = 1;
int intervals = 1000;
void setup() {
Serial.begin(9600);
pinMode(modePin, INPUT_PULLUP);
pinMode(highPin, INPUT_PULLUP);
pinMode(lowPin, INPUT_PULLUP);
pinMode(LEDPin, OUTPUT);
}
void loop() {
chechkForMode();
while(mode == 1){
digitalWrite(LEDPin, HIGH);
chechkForMode();
}
while(mode == 2){
while(brightness > 0){
analogWrite(LEDPin, brightness);
brightness = brightness - waveLightSpeed;
chechkForMode();
repeat();
}
while(brightness < 255){
analogWrite(LEDPin, brightness);
brightness = brightness + waveLightSpeed;
chechkForMode();
repeat();
}
while(brightness > 0){
analogWrite(LEDPin, brightness);
brightness = brightness - waveLightSpeed;
chechkForMode();
repeat();
}
while(brightness < 255){
analogWrite(LEDPin, brightness);
brightness = brightness + waveLightSpeed;
chechkForMode();
repeat();
}
chechkForMode();
}
while(mode == 3){
chechkForMode();
}
}
void chechkForMode(){
mButtonState = digitalRead(modePin);
if(mButtonState != mOldButtonState){
mode++;
if(mode>3){
mode=1;
}
mOldButtonState = mButtonState;
}
Serial.print(waveLightSpeed);
Serial.print(", ");
Serial.println(mode);
}
void repeat(){
lButtonState = digitalRead(lowPin);
hButtonState = digitalRead(highPin);
if (lButtonState != lOldButtonState) {
if (lButtonState == LOW) {
intervals = intervals - 100;
waveLightSpeed = waveLightSpeed - 0.2;
if(waveLightSpeed < 0.2){
waveLightSpeed = 0.2;
}
if(intervals < 100){
intervals = 100;
}
}
lOldButtonState = lButtonState;
}
if (hButtonState != hOldButtonState) {
if (hButtonState == LOW) {
intervals = intervals + 100;
waveLightSpeed = waveLightSpeed + 0.2;
if(waveLightSpeed > 5){
waveLightSpeed = 5;
}
if(intervals > 5000){
intervals = 5000;
}
}
hOldButtonState = hButtonState;
}
Serial.print(waveLightSpeed);
Serial.print(", ");
Serial.println(mode);
}
For some reason even when mode isn't 2 I can change waveLightSpeed which I shouldn't be able to do. And for some reason LEDs are are either staying always on or blinking very fast. Please help. Thank you in advance.
Hi,
Is this the same as this thread?
Hello, I have an LED line and a switch lever (if that's what they're called) and with every switch I want to switch a mode. Here's the code:
int modePin = 7;
int LEDPin = 11;
int lowPin = 2;
int highPin = 4;
int mode = 1;
int mButtonState = 0;
int mOldButtonState = 0;
void setup() {
Serial.begin(9600);
pinMode(modePin, INPUT_PULLUP);
pinMode(highPin, INPUT_PULLUP);
pinMode(lowPin, INPUT_PULLUP);
pinMode(LEDPin, OUTPUT);
}
void loop() {
chechkForMode();
while(mode == 1){
…
It would have been better for you to have continued with the other thread.
Tom...
1 Like
Oh sorry, haha. I don't usually ask in forums so I didn't know it was an option. Should I delete this and continue the other thread?
system
Closed
September 1, 2022, 11:16am
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.