int RedLed = 3;
int GreenLed = 4;
void setup()
{
int menuChoice;
pinMode(RedLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
Serial.begin(9600);
do
{
int menuChoice;
Serial.println("1. Blinking LEDs ");
Serial.println("2. LDR");
//Serial.print("3. ");
Serial.println("4. Exit");
while(Serial.available() == 0) {}
menuChoice = Serial.parseInt();
switch(menuChoice)
{
case 1: ledBlink(5,2); break;
case 2: LDR(); break;
}
}
while(menuChoice != 4);
{}
}
void ledBlink(int RedBlink, int GreenBlink)
{
int i,j,k=1;
for(k=1; k=3; k=k+1)
{
for(i=1; i<=RedBlink; i++)
{
digitalWrite(RedLed, HIGH);
delay(250);
digitalWrite(RedLed, LOW);
delay(250);
}
for(j=1; j<=GreenBlink; j++)
{
digitalWrite(GreenLed, HIGH);
delay(250);
digitalWrite(GreenLed, LOW);
delay(250);
}
}
}
void loop() {}
Hello,
so far I did a menu and depending on what number you input will call the corresponding function. However the function only runs once, so I decided to implement a for loop so that I could run it as many times as I want.
First I did the for loop like this to run infinitely for(k=1; k<=-1, k++), however the function didn't even execute.
Secondly when I did the for loop as shown above i.e. for(k=1; k=3; k++) the function runs infinitely, and does not stop after 3 executions.
any tips of what may be going wrong?
Firstly it is very unlikely that a for loop is the answer to your problem.
Secondly
for(k=1; k=3; k++)
is wrong. The expression in the centre tells the for loop when to stop and I expect you meant it to test whether k equals 3, but what you have done is to set k equal to 3.
The '=' is assignment, '==' test for equality, so: for(k = 1; k <= 3; k++) should work.
In the statement for(k=1; k<=-1, k++) middle part is condition and never will be fulfilled.
Use
for(;;) {
}
for infinity loop.
In short : you must use the double equal sign, when checking for a condition : (if) k==3
When you use the single equal sign, you are assigning the value 3 to K : K = 3.
Thus you are not checking for the K==3 condition.
In your other example, you are using the "less than or equal" <= which is fine, as this is a condition check, because you can not assign a value using <= to a variable.
Our helicopter Bob can not land anymore to get down into such simple explanations 
Anders53:
When you use the single equal sign, you are assigning the value 3 to K : K = 3.
Thus you are not checking for a condition.
Yes, it does. It is checked for zero like this: (k = 3) == 0, which is false in result.
I now understand k=3 is wrong in this case. When I used for(k = 1; k <= 3; k++) it worked as it should, however when I swapped the '<=' with '==', the function just terminated immediately.
The statement for(k=1; k<=-1, k++), is this terminating the loop immediately just because the condition will never be fulfilled ?
Budvar10:
- k = 1; // so k exactly is equal to 1
- test k <= -1; // true for -1; -2 ....; false for 0, 1....
- exit from loop
Did I miss something?
Couldn't have explained it better. Thanks for all your help guys
Much appreciated
Hi guys i need some help. Here's my code. I wanna stop the loop counting and show the actual value next to each option.
unsigned long i = 0;
void loop()
{
tft.setCursor(20,80);
tft.setFontScale(1);
tft.setFontSpacing(0);
if (i < 0.01) tft.print("Not penny: Only quarter");
tft.setCursor(400,80);
if (i > 0.01 )tft.print(i,DEC);
tft.setCursor(20,120);
tft.setFontScale(1);
tft.setFontSpacing(0);
if (i < 0.05) tft.print("Not nickel:Only quarter");
tft.setCursor(400,120);
if (i > 0.05 )tft.print(i,DEC);
tft.setCursor(20,160);
tft.setFontScale(1);
tft.setFontSpacing(0);
if (i < 0.10) tft.print("Not dime: Only quarter");
tft.setCursor(400,160);
if (i > 0.25 ) tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.print(i,DEC);
delay(100);
i++;
}
i is declared as an unsigned integer
You increment i by 1 each time through loop evert 100 milliseconds
if (i < 0.05) tft.print("Not nickel:Only quarter");
if (i < 0.10) tft.print("Not dime: Only quarter");
How long will its value remain small enough to meet either of the conditions above ?
if (i > 0.01 )tft.print(i, DEC);
if (i > 0.05 )tft.print(i, DEC);
How soon will its value always meet the above conditions ?
What is the printing relating to currency all about ?
What is in the rest of your code that you have not posted ?
What has this query got to do with a for loop posed in this thread which is over 2 years old ?
i is an integer. Integers contain the values 0, 1, 2, 3 etc. i starts at 0. First time through that's less than 0.01, 0.05 etc. Then you add 1 to it. Now it's 1 which is greater than all of 0.01, 0.05, 0.10 and 0.25.
I don't know what you're trying to do but I bet that code doesn't do it.
Steve
It's easier to understand the 'for' loop if you understand that is is shorthand for a common form of 'while' loop:
for (A; B; C)
D;
is shorthand for:
A;
while (B)
{
D;
C;
}