Arduino Code Else Statement (C/C#)

Thread Starter

Zane Finner

Joined Jan 29, 2018
30
My else statement keeps putting errors. I have no idea what I'm doing wrong. What I'm trying to do is make is so when the anaolg signal is between 600 and 500, the function Pause commences. Here is my code:

Code:
int TurnOne()  {
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
}
int Pause()    {
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
}
int TurnTwo()    {
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    delay(100);
}

void setup()
{
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(A0, INPUT);

}

void loop()    {
    if (A0 > 600);    {
        TurnTwo();    }
    if (A0 < 500);    {
        TurnOne();    }
    else(); {                     //this is where my trouble is .-.
        Pause();
    }
}
UPDATE! I fixed this by making the if statements to while statements and taking out the 'else'. I still do want to know what was wrong with the else statement though..
 
Last edited:

be80be

Joined Jul 5, 2008
2,395
else is like this
Code:
else
{
 Pause();      // do Thing C
}
Not this
Code:
else(); {                     //this is where my trouble is .-.
        Pause();
    }
 

Ian Rogers

Joined Dec 12, 2012
1,136
Also If.. if.. else Should be if.. else if.. else...
C:
void loop() {
    if (A0 > 600) {
        TurnTwo();    }
    else if (A0 < 500) {
        TurnOne();    }
    else {                     //this is where my trouble is .-.
        Pause();
    }
}
 
Also, A0 is likely not doing what you think it is. If you want to read the analog input from A0, place a statement like this as the first line in main()

val = analogRead(A0);

in your if / else if / else statement, replace A0 with val

Then, you will take a reading of analog input A0 every time through the loop of main()

Also, when you declare the functions int TurnOne() / int TurnTwo() / int Pause(), your function is intended to return an integer value. Yours do not. If you don't need the function to return a value, declare it as "void TurnOne()". You can get away with it the way you have it, but it is not a good practice.

Arduino is a great learning tool, due not just to it's ease and simplicity, but also due to the tremendous amount of documentation available.

In this case, look at https://www.arduino.cc/reference/en/language/structure/control-structure/else/ and here https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/ for example. It is a very good idea to make use of this documentation as you plow forward.
 
Last edited:

be80be

Joined Jul 5, 2008
2,395
Yep he needs that too lol
val = analogRead(A0);

Or there nothing to test

This is your friend you need to use it. https://www.arduino.cc/reference/en/
it come's in lots of language.
You can change the en to yours if your language is not English.
Code:
Syntax
if (condition1)
{
  // do Thing A
}
else if (condition2)
{
  // do Thing B
}
else
{
  // do Thing C
}
sample
Code:
Example Code
Below is an extract from a code for temperature sensor system

if (temperature >= 70)
{
  //Danger! Shut down the system
}
else if (temperature >= 60 && temperature < 70)
{
  //Warning! User attention required
}
else
{
  //Safe! Continue usual tasks...
}
But like Raymond Genovese said without
Code:
val = analogRead(A0);
your test will not work.
 

BobaMosfet

Joined Jul 1, 2009
2,211
My else statement keeps putting errors. I have no idea what I'm doing wrong. What I'm trying to do is make is so when the anaolg signal is between 600 and 500, the function Pause commences. Here is my code:

Code:
int TurnOne()  {
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
}
int Pause()    {
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
}
int TurnTwo()    {
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    delay(100);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    delay(100);
}

void setup()
{
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(A0, INPUT);

}

void loop()    {
    if (A0 > 600);    {
        TurnTwo();    }
    if (A0 < 500);    {
        TurnOne();    }
    else(); {                     //this is where my trouble is .-.
        Pause();
    }
}
UPDATE! I fixed this by making the if statements to while statements and taking out the 'else'. I still do want to know what was wrong with the else statement though..

In the code above, your problem is that you are putting semicolons at the end if the if and the else. If is not a function. Nor is else. These are statements.

Code:
if(...)
     {
     // do code
     }
else
     {
     // do code
     };
Semicolon is only done once at the end of the last character of any statement.
 

-live wire-

Joined Dec 22, 2017
959
Really try using millis(), micros(), and/or port manipulation and timer registers. Why? Delay tells it to ignore everything for a set amount of time. E.G. You want an LED to turn on, wait 3 seconds, turn off, wait 3 seconds. If you press a button it should stop doing this immediately and not resume. So you say "if button pressed, bool = true. bool starts out false, if bool false, blink." The problem: it will ignore EVERYTHING else when there is a delay. So unless you are holding it down in the brief moment in between the delays, it gets ignored. Or if you want to have two LEDs blink independently. Or if you also want a buzzer to buzz. You get the point.

So now you're convinced. But how do you implement such a timer? Let's say you have the same on/off time to keep it simple. You create a "long" variable that starts out as 0. You create another long that you say = millis(); Let's call them pMil and cMil for previous and current millis(). You say "if cMil-pMil >= delayTime" then "do action (blink LED, whatever) and then set pMil = to cMil". This does your action every "delayTime" milliseconds. And obviously you can put all that in more if statements to add more inputs and control more things.

If you need separate on/off times or want different delays you can add a switch that changes delayTime. You do the same thing but then say when the time is exceeded you do something based on two variables. You first say "increase var1 and var2 by one". Then you say if var1 = 1 do this. If two, do something else. ect. Have a default for your switch that sets it equal to your starting value. That way it resets and loops. So you do that for both var1 and var2. Have var1 determine your action and var2 determine the delayTime. This could work for infinite different delays and things to do after them.

Google "arduino switch" and go to the reference of the arduino website if you do not know what it is. It is a great resource and shows you the exact syntax with comments.

I know how to code very well but I do not want to type that much. I know all the syntax and stuff. And I want to make it easy to understand. So that is why I did not post code.
 
Top