i'd like help with my first project (ever), it seems simple but error keep coming up and i cant understand how to fix them.
id like my code to work like this: 5 IR sensor are place in to a grain bin and when the sensors are blocked an led turns on, one sensor per LED. so far i could get one sensor and LED to work but after that hiccup start.
The ir sensor are : E3S-GS30E4 from Amazon.ca
this is what i have so far:
/*
Turn LEDs on and off with the ir sensors. LED is off until the senor is blocked then LED turns on
The Circuit:
- LED(L1) will be controlled by sensors(S1) and will be attched to S1-pin(A0) with L1-pin8
- LED(L2) will be controlled by sensors(S2) and will be attched to S2-pin(A1) with L2-pin9
- LED(L3) will be controlled by sensors(S3) and will be attched to S3-pin(A3) with L3-pin10
- and so on, until all five senors are conected.
- later a anttena will be added.
Created Nov.2018
by krykuzz
*/
const int sensor_A = A0; // fullsensor conected to led 1
const int sensor_B = A1; //
const int led_A = 8;
const int led_B = 9;
int value_A = 0;
int value_B = 0;
int sensor_AValue = 0;
int sensor_BValue = 0;
void setup() {
Serial.begin(9600);
pinMode(sensor_A,INPUT);
pinMode(led_A,OUTPUT);
pinMode(sensor_B,INPUT);
pinMode(led_B,OUTPUT);
}
void loop() {
analogRead(sensor_A);
int senstatus = analogRead(sensor_A);
(if sensor_Avalue<=200);
digitalWrite(led_A, HIGH);
delay(2000);
else{
digitalWrite(led_A, LOW);
}
analogRead(sensor_B);
int senstatus = analogRead(sensor_B);
(if sensor_Bvalue<=200); // this is the line (so far) having errors
digitalWrite(led_B, HIGH);
delay(2000);
else{
digitalWrite(led_B, LOW);
delay(2000);
}
}
}
Error message:
C:\Users\Krystyn\Documents\Arduino\Bin_Senor\Bin_Senor.ino: At global scope:
Bin_Senor:67:1: error: expected declaration before '}' token
exit status 1
expected primary-expression before 'if'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Later id like to have this work with antennas. I have two bin that need the same setup and one master board in a shed about 30ft away. By the code i can figure out how to hookup everthing thank you for the help and comment.
As to the error, the last curly bracket is superfluous. Use autoformat on (ctrl-t or Tools, Auto Format) on the code and the problem is obvious.
(if sensor_Avalue<=200);
You need to look up the syntax for the if statement.
Where are sensor_Avalue and sensor_Bvalue given any value except when declared and initialized?
sensor_Avalue is not the same as sensor_AValue. C++ is case sensitive. Same with sensor_Bvalue.
The line you identify as having errors has the wrong syntax for an if statement.
Right above that line, you have two lines:
analogRead(sensor_B);
int senstatus = analogRead(sensor_B);
Here you analogRead() pin A0, then throw the result away, then read it again into a local variable named senstatus, but never use that variable.
Finally, you have an extra } at the end of the sketch.
(whoops, groundFungus beat me to it, and found even more errors!)
ChrisTenone:
analogRead(sensor_B);
int senstatus = analogRead(sensor_B);
Here you analogRead() pin A0, then throw the result away
That's quite normal if you want to read multiple sensors. The first reading is there to prevent an incorrect reading after the mux has switched to another channel.
if (sensor_AValue <= 200);
digitalWrite(led_A, HIGH);
delay(2000);
else
{
digitalWrite(led_A, LOW);
}
Mote errors here. Should be no semicolon at the end of the if statement if you want to conditionally execute to following code. The code that you want to execute conditionally needs to be enclosed in curly brackets.
analogRead(sensor_A);
int senstatus = analogRead(sensor_A);
analogRead(sensor_B);
int senstatus = analogRead(sensor_B);
You are declaring the senstatus variable twice. That is illegal.
This is your code corrected so that it will compile and formatted, though it may will not do what you want.
/*
Turn LEDs on and off with the ir sensors. LED is off until the senor is blocked then LED turns on
The Circuit:
- LED(L1) will be controlled by sensors(S1) and will be attched to S1-pin(A0) with L1-pin8
- LED(L2) will be controlled by sensors(S2) and will be attched to S2-pin(A1) with L2-pin9
- LED(L3) will be controlled by sensors(S3) and will be attched to S3-pin(A3) with L3-pin10
- and so on, until all five senors are conected.
- later a anttena will be added.
Created Nov.2018
by krykuzz
*/
const int sensor_A = A0; // fullsensor conected to led 1
const int sensor_B = A1; //
const int led_A = 8;
const int led_B = 9;
int value_A = 0;
int value_B = 0;
int sensor_AValue = 0;
int sensor_BValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(sensor_A, INPUT);
pinMode(led_A, OUTPUT);
pinMode(sensor_B, INPUT);
pinMode(led_B, OUTPUT);
}
void loop()
{
analogRead(sensor_A);
int senstatus = analogRead(sensor_A);
if (sensor_AValue <= 200)
{
digitalWrite(led_A, HIGH);
delay(2000);
}
else
{
digitalWrite(led_A, LOW);
}
analogRead(sensor_B);
senstatus = analogRead(sensor_B);
if (sensor_BValue <= 200)
{
// this is the line (so far) having errors
digitalWrite(led_B, HIGH);
delay(2000);
}
else
{
digitalWrite(led_B, LOW);
delay(2000);
}
}
sterretje:
That's quite normal if you want to read multiple sensors. The first reading is there to prevent an incorrect reading after the mux has switched to another channel.
Oh, ok, I get that. But the result isn't ever used, subsequently.
ChrisTenone:
Oh, ok, I get that. But the result isn't ever used, subsequently.
Why would you want to use the first result that more than likely is no true reflection of the input channel?
I think that Chris means that The OP ultimately reads the analog input into a variable (the second read), but they never use that variable anywhere in the program. The analog reads are worthless.
But the result isn't ever used, subsequently.
Ah. Thanks, now it makes sense.
I had that in a reply to OP but you and Chris already had mentioned that before I could post so I did not post it.