Hello and good day. I have a problem with my code (I think its easy to solve but I just cant figure it out).
Long short story I have a RGB led and with the push of button1 it should do the code and if I push it again it should stop and if I push it again it should resume (I know I should use a counter and do something like x % 2 == 0).
if (valueRed >= 0 && valueBlue <= 255 && valueGreen == 0)
{ while (x % 2 == 0)
{
delay(1000);
if (button1State == HIGH)
{
x = x+1;
}
else {
analogWrite (redLed, valueRed);
analogWrite (blueLed, valueBlue);
analogWrite (greenLed, valueGreen);
delay (1000);
valueBlue += 15;
}
this is my code. help pls
My problem is that the code always go to "else" even if i keep pressing the button like a crazy man.
I have a feeling that even if there was a setup() and loop(), this code wouldn't even compile. Seems to be missing a couple }
LarryD
February 21, 2017, 1:15am
3
Where do you read button1State?
Show us your complete current sketch. Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]
Please do not post code fragments.
I count four { and two }. This fragment is like getting half a wheel - I can't tell what's wrong with a wheel if I only get half.
Weird indenting. Use ctrl-T to reformat.
I can't see where x is initialized. What is it set to on entering this fragment?
The loop will exit as soon as x is incremented - x%2 is == 0 for every even value of x.
I valueBlue a byte? If so, valueBlue will always be <= 255 and that clause in your if() does nothing.
Just a really disordered, confused bit of code.
const int blueLed = 9;
const int redLed = 5;
const int greenLed = 11;
const int button1 = 3;
const int button2 = 2;
int button1State = 0;
int button2State = 0;
int valueRed = 255;
int valueBlue = 0;
int valueGreen = 0;
int x = 2;
int y = 1;
bool pressed;
void setup() {
// put your setup code here, to run once:
pinMode (button1, INPUT);
pinMode (button2, INPUT);
pinMode(redLed, OUTPUT);
pinMode (blueLed, OUTPUT);
pinMode (greenLed, OUTPUT);
Serial.begin (9600);
pressed = false;
}
void loop() {
// put your main code here, to run repeatedly:
button1State = digitalRead(button1);
button2State = digitalRead(button2);
if (valueRed >= 0 && valueBlue <= 255 && valueGreen == 0)
{ while (x % 2 == 0)
{
delay(1000);
if (button1State == HIGH)
{
x = x+1;
}
else {
analogWrite (redLed, valueRed);
analogWrite (blueLed, valueBlue);
analogWrite (greenLed, valueGreen);
delay (1000);
valueBlue += 15;
}
Serial.print('\n');
Serial.print("Value of red is :");
Serial.print(" ");
Serial.print(valueRed);
Serial.print('\n');
Serial.print("Value of green is :");
Serial.print(" ");
Serial.print(valueGreen);
Serial.print('\n');
Serial.print("Value of blue is :");
Serial.print(" ");
Serial.print(valueBlue);
Serial.print('\n');
Serial.print(button1State);
}
}
}
I didnt think all code might be needed but here we go
LarryD
February 21, 2017, 1:27am
6
Always use CTRL T to format your code.
Put
button1State = digitalRead(button1);
before
if (button1State == HIGH)
in the 4th reply there is my full code
LarryD
February 21, 2017, 1:54am
10
delay(1000);
button1State = digitalRead(button1);
if (button1State == HIGH)