errors, errors and more errors

Hey guys,

ive been searching for quite a while and cant come up with a reason that I keep getting errors with this code. I’ve got some explanation inside the code of what im trying to acomplish. I keep getting

expected primary-expression before ‘||’ token

or

expected ‘)’ before ‘;’ token

depending on what I do to try and remedy the other issues.

int fan = 2;
int switchpin = 3;
int led1 = 4;
int led2 = 6; 
int led3 = 9;
int led4 = 10;
int sensorpin = A0; //potentiometer
int fanspeed = 255;
int brightness1 = 0;
int brightness2 = 0; 
int brightness3 = 0;
int brightness4 = 0;


void setup() {
  // put your setup code here, to run once:
pinMode(fan, OUTPUT);
pinMode(switchpin, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
//if potentiometer reads ~50% or more then turn led 4 on and turn the fan onto full speed
if (analogRead(sensorpin) >= || == 500) {
  digitalWrite(led4, HIGH);
  analogWrite(fan, 255);
}
//if button is low turn all leds on high
else if (analogRead(switchpin) == LOW){ 
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
}
// if button is low and potentiometer reads less then or equal to ~ 49%
//fade all lights in sequence
if (analogRead(switchpin) == LOW && analogRead(sensorpin) <= || == 499) {
  digitalWrite(led1, HIGH);
  delay(1000);
  digitalWrite(led1, LOW);
  (brightness2 = 255; brightness2 >=0; brightness2 -= 3)
  analogWrite(led2, brightness2);
  delay(30);
  (brightness3 = 255; brightness3 >=0; brightness3 -= 3)
  analogWrite(led3, brightness3);
  delay(30);
()  (brightness4 = 255; brightness4 >=0; brightness4 -= 3)
  analogWrite(led4, brightness4);
  delay(30);
}
// in all other occasions
else (analogRead(switchpin) == HIGH){
  digitalWrite(led1, HIGH);
  delay(300);
  digitalWrite(led1, LOW);
  delay(300);
  digitalWrite(led2, HIGH);
  delay(300);
  digitalWrite(led2, LOW);
  delay(300);
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led3, LOW);
  delay(300);
  digitalWrite(led4, HIGH);
  delay(300);
  digitalWrite(led4, LOW);
}
}

also, is anyone aware of any site or video that really dumbs down what all the functions and terms mean? I have a basic understanding, but when I try to figure out some of the more advanced stuff i get kinda confused. the reference and language page are not written in laymens terms and most other pages i can find go over the very basic stuff that i already have done or know.

THANKS!!!

In

  if (analogRead(switchpin) == LOW && analogRead(sensorpin) <= || == 499) {

what is analogRead(sensorpin) <= || == 499 supposed to mean / do? You have plenty of that type of constructions.

Yeah, those are pretty typical syntax errors. You are constructing your compound conditionals wrong. Each comparison needs to be written out in full. This site describes the keywords and usage pretty well.

bobbybonez:
Hey guys,

ive been searching for quite a while and cant come up with a reason that I keep getting errors with this code. I’ve got some explanation inside the code of what im trying to acomplish. I keep getting

expected primary-expression before ‘||’ token

or

expected ‘)’ before ‘;’ token

depending on what I do to try and remedy the other issues.

int fan = 2;

int switchpin = 3;
int led1 = 4;
int led2 = 6;
int led3 = 9;
int led4 = 10;
int sensorpin = A0; //potentiometer
int fanspeed = 255;
int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
int brightness4 = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(fan, OUTPUT);
pinMode(switchpin, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
//if potentiometer reads ~50% or more then turn led 4 on and turn the fan onto full speed
if (analogRead(sensorpin) >= || == 500) {
  digitalWrite(led4, HIGH);
  analogWrite(fan, 255);
}
//if button is low turn all leds on high
else if (analogRead(switchpin) == LOW){
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
}
// if button is low and potentiometer reads less then or equal to ~ 49%
//fade all lights in sequence
if (analogRead(switchpin) == LOW && analogRead(sensorpin) <= || == 499) {
  digitalWrite(led1, HIGH);
  delay(1000);
  digitalWrite(led1, LOW);
  (brightness2 = 255; brightness2 >=0; brightness2 -= 3)
  analogWrite(led2, brightness2);
  delay(30);
  (brightness3 = 255; brightness3 >=0; brightness3 -= 3)
  analogWrite(led3, brightness3);
  delay(30);
()  (brightness4 = 255; brightness4 >=0; brightness4 -= 3)
  analogWrite(led4, brightness4);
  delay(30);
}
// in all other occasions
else (analogRead(switchpin) == HIGH){
  digitalWrite(led1, HIGH);
  delay(300);
  digitalWrite(led1, LOW);
  delay(300);
  digitalWrite(led2, HIGH);
  delay(300);
  digitalWrite(led2, LOW);
  delay(300);
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led3, LOW);
  delay(300);
  digitalWrite(led4, HIGH);
  delay(300);
  digitalWrite(led4, LOW);
}
}





also, is anyone aware of any site or video that really dumbs down what all the functions and terms mean? I have a basic understanding, but when I try to figure out some of the more advanced stuff i get kinda confused. the reference and language page are not written in laymens terms and most other pages i can find go over the very basic stuff that i already have done or know.

THANKS!!!
  if (analogRead(sensorpin) >= || == 500) {

You have two operators in a row without an expression between, so its saying it expected an expression.

  (brightness2 = 255; brightness2 >=0; brightness2 -= 3)

You are missing the keyword 'for', so its completely confused.

Hi,
This might help.
https://www.tutorialspoint.com/arduino/index.htm

Look up "operators"

Tom... :slight_smile:

What I am going for here is if the button is low (pressed in) and the potentiometer is at less then 50% then it will blink the led. I actually just saw the brightness stuff below that and I’m not sure why I have it. It doesn’t need to be there.

sterretje:
In

  if (analogRead(switchpin) == LOW && analogRead(sensorpin) <= || == 499) {

what is analogRead(sensorpin) <= || == 499 supposed to mean / do? You have plenty of that type of constructions.

I only half understand what they are getting at on this site. I'm looking for something that explains it in a little simpler format.

What am I missing for the compound conditionals?

ChrisTenone:
Yeah, those are pretty typical syntax errors. You are constructing your compound conditionals wrong. Each comparison needs to be written out in full. This site describes the keywords and usage pretty well.

You're missing the fact that each comparison has to be complete. If you say (x > 10 || x == 5) the compiler know what you mean. If you say (x >10 || == 5) it doesn't. The compiler doesn't remember the x and guess that it is supposed to apply to the next comparison too.

To check if you're talking sense try putting parentheses round each comparison ((x>10) || (==5)). The ==5 on it's own makes no sense at all. What is supposed to be equal to 5? You haven't said.

Steve

To make your construction correct, it would look like

a <= 499 || a == 499

But <= means less than or equal. So you don’t need the ||

Hi,
You need to restructure you code so that at the start of the void loop(), you read ALL your inputs and assign the values to variables, then ONLY use those variable through the rest of you code.
The values being up dated each time the code loops back to the start.
For example;

switchState = digitalRead(switchPin);
sensorVal = analogRead(sensorPin);

// instead of
// if (analogRead(switchpin) == LOW && analogRead(sensorpin) <= || == 499) {

if (switchState == LOW && sensorVal <= 499) {

// which says..  if switchState is LOW AND sensorVal is LESS THAN OR EQUAL To 499.
}

analogReading each time that you need the value makes reading your code cumbersome, and not needed because of the speed of the loop.

In fact doing repeated unnecessary analogRead will probably slow the loop down.

So read ALL your variables and assign their values to meaningfull variable names at the start of the loop.
Then use the variables when you do tests. :slight_smile:

Did you read the link, look at operators.

https://www.tutorialspoint.com/arduino/arduino_operators.htm

Tom… :slight_smile:

bobbybonez:
I only half understand what they are getting at on this site. I'm looking for something that explains it in a little simpler format.

What am I missing for the compound conditionals?

Here is a phrase from your original code:

 if (analogRead(sensorpin) >= || == 500) {
  digitalWrite(led4, HIGH);
  analogWrite(fan, 255);
}

The conditional is the part in parenthesis between "if" and "}", specifically:
(analogRead(sensorpin) >= || == 500).

If you read this in English, it says "analogRead(sensorpin) is greater than or equal to, or (something unspecified) is equal to five hundred". What you want it to say is "analogRead(sensorpin) is greater than or equal to five hundred". Since >= means "greater than or equal to" - the "or" is implicit in the keyword >=. Simplify this expression to read:
(analogRead(sensorpin) >= 500).

You made this error in several places. Correct them all then move on to the next error (the above mentioned missing for.)

Try compiling after each correction, then once it compiles, (Yay!) move on to the optimization that Tom describes in the post right above this one.

ps, you can use the alternate keywords "and' and "or" instead of && and ||. I do this, and it makes it so much easier to read the code!

Adding, it’s far better to read once and store in a variable. E.g.

int val = analogread(A0);
if (val >= 500 && val <600)
{
  // do something
}

There are two advantages: analogRead takes approx. 100 uS so only reading once speeds up the process. And in those 100 uS the input signal might have changed and you would have been comparing two different readings if you used two analogReads.