I'm trying to write the following code to close a relay that will cloose a tap on a resistor ladder based on an analog 1-5 vole input. I have some basic code working, but I am getting a compiling error when I change an if statement from a single condition to one that is linked with an &&. Works fine without the && but I get a keep getting a compiling error "expected identifier before '(' token. Here's the code that generates the error - when I remove the section that reads: "&& (adjSensor on line 21 the sketch works, but with it, it fails. I am struggling to figure out my error.
<200)3. void setup()
4 {Serial.begin(9600);}
6. int relay8 (OUTPUT, LOW);
7. int relay9 (OUTPUT, LOW); // Set relay to low to disengage resistance if preiously used
8. int sensorInput=A0; // Read input voltage from tank sensor
9 int adjSensor = sensorInput *100; //Raise reading to better distinguish readings
10 int adjvalue = 0;
11 int sensorinput=0;
13 void loop()
14 {
15 digitalWrite (relay8, LOW); // Set relay to low to disengage reistance if previously used
16 digitalWrite (relay9, LOW); // Set relay to low to disengage reistance if previously used
17 sensorInput = analogRead(sensorInput); // Read sensor voltage
18 adjSensor = sensorInput * 100; // Raise reading to better distinguish readings
19 Serial.print ("sensorInput :"); Serial.println (sensorInput); // debugging and tracking
20. Serial.print ("Adjusted Sensor Value: "); Serial.println (adjSensor); // debugging and tracking
21 if (adjSensor>100) && (adjSensor <200), DigitalWrite (relay9, HIGH); // Set resistance ladder at tab 9 (x ohms)
22 if (adjSensor>200) digitalWrite (relay8, HIGH); // Set resistance ladder at tab 8 (y ohms)
23 delay(2000); // Allow for reading
}`Use code tags to format code for the forum`
No, that does not read anything.
It just sets sesorInput to the fixed value A0 - which is just the name of the pin.
You would have to do analogRead(sensorInput) to read the analogue value at that pin.
But you actually do this:
which is fine for the very first time through the loop, but it changes the value in sensorInput from the pin name to the actual value - so the next time round the loop it will attempt to read from some random pin number, probably invalid!!
As @jremington said, you really need to step back and spend some time with a few examples...
Thank you. Yes the comment was a typo, I realize it was just setting the sensorInput to a 0 value. Your insight on the sensorInput real was very helpful, thanks. I've spent quit a bit of time on examples, both form the Arduino website and others, and used an example from one of the examples for the code that does not work, and frankly the recommendations above do not work either. Will keep trying.
Here's the code, as anwell pointed out I need to change the line where the sensorInput is set to the analogRead, since it will only work the first time through. My original issue is that i can't get the if statement that includes the && to work. Thanks.
void setup()
{Serial.begin(9600);}
int relay8 (OUTPUT, LOW);
int relay9 (OUTPUT, LOW); // Set relay to low to disengage resistance if preiously used
int sensorInput=A0; // Read input voltage from tank sensor
int adjSensor = sensorInput *100; //Raise reading to better distinguish readings
int adjvalue = 0;
int sensorinput=0;
void loop()
{
digitalWrite (relay8, LOW); // Set relay to low to disengage reistance if previously used
digitalWrite (relay9, LOW); // Set relay to low to disengage reistance if previously used
sensorInput = analogRead(sensorInput); // Read sensor voltage
adjSensor = sensorInput * 100; // Raise reading to better distinguish readings
Serial.print ("sensorInput :"); Serial.println (sensorInput); // debugging and tracking
Serial.print ("Adjusted Sensor Value: "); Serial.println (adjSensor); // debugging and tracking
if (adjSensor>100) && (adjSensor <200), DigiTalWrite (relay9, HIGH); // Set resistance ladder at tab 9 (x ohms)
if (adjSensor>200) digitalWrite (relay8, HIGH); // Set resistance ladder at tab 8 (y ohms)
delay(2000); // Allow for reading
I don't think so...
The code still contains a lot of errors.
What is the purpose of these lines, for example?
and this is complete non-sense:
Please read in the manual, what the meanings of the argument and return value of the analogRead() function. This two values can't be the same variable.
Read the comments (//) in this to help you understand some programming syntax.
I have "commented-out" inaccurate lines of your original code and placed working code before, after or in-place-of your code. Learn (search for) why the replacements were needed.
int relay8 = 8; // define a pin (8) for a variable (relay8) type/size (int)
int relay9 = 9; // define a pin (9) for a variable (relay9) type/size (int)
int sensorInput = A0; // define a pin (A0) for a variable (sensorInput) type/size (int)
int adjSensor; // define a variable type/size - MAKE IT GLOBAL IN SCOPE - "GLOBAL" means "in every part of the program"
void setup() { // this opening brace must be here to surround your setup() code
// {Serial.begin(9600); } // this line is incorrect and must look like the following line
Serial.begin(9600); // this sets the baud rate (9600) of the Serial Monitor
// int relay8 (OUTPUT, LOW); // this is incorrect - REPLACE WITH THE FOLLOWING LINE
pinMode(relay8, OUTPUT); // set (pinMode) defined pin (relay8) direction (OUTPUT)
// int relay9 (OUTPUT, LOW); // this is incorrect - REPLACE WITH THE FOLLOWING LINE
pinMode(relay9, OUTPUT); // set (pinMode) defined pin (relay9) direction (OUTPUT)
// THE FOLLOWING LINES WILL SET THE RELAY PINS "LOW"
digitalWrite (relay8, LOW); // Set relay to low to disengage resistance if preiously used
digitalWrite (relay9, LOW); // Set relay to low to disengage resistance if preiously used
// int sensorInput = A0; // Read input voltage from tank sensor
// setting type (int) of variables (sensorInput) inside a function restricts the scope of the variable
// int adjSensor = sensorInput * 100; //Raise reading to better distinguish readings
// setting type (int) of variables (adjSensor) inside a function restricts the scope of the variable
// int adjvalue = 0;
// setting type (int) of variables (adjSensor) inside a function restricts the scope of the variable
// int sensorinput = 0;
// setting type (int) of variables (adjSensor) inside a function restricts the scope of the variable
}
void loop() {
// this line will show an error if you leave the scope of the variable inside loop();
sensorInput = analogRead(sensorInput); // Read sensor voltage
// this line will show an error if you leave the scope of the variable inside loop();
adjSensor = sensorInput * 100; // Raise reading to better distinguish readings
// Combine the two lines above so "sensorInput" is only a pin reading
adjSensor = analogRead(sensorInput) * 100;
Serial.print ("sensorInput :"); // split the one line into two
Serial.println (sensorInput); // debugging and tracking
Serial.print ("Adjusted Sensor Value: "); // split the one line into two
Serial.println (adjSensor); // debugging and tracking
// if (adjSensor > 100) && (adjSensor < 200), // remove the internal parnetheses ")(" and the comma
if (adjSensor > 100 && adjSensor < 200)
// DigiTalWrite (relay9, HIGH); // change the spelling of DigiTal to digital like following line
digitalWrite(relay9, HIGH); // Set resistance ladder at tab 9 (x ohms)
if (adjSensor > 200)
digitalWrite (relay8, HIGH); // Set resistance ladder at tab 8 (y ohms)
delay(2000); // Allow for reading
}
That comment is still wrong, and you still have this nonsense:
You really need two variables here:
The pin number that you want to read;
The value which you read from that pin.
eg,
int sensorInputPin = A0; // Pin connected to the input voltage from tank sensor
int sensorInputValue; // Value read from the sensor
:
:
sensorInputValue = analogRead( sensorInputPin ); // Read the sensor voltage from the defined pin
EDIT
Most likely, the pin number won't change - so you should define it as const:
const int sensorInputPin = A0; // Pin connected to the input voltage from tank sensor
Thank you for correcting. I changed editing course from commenting above/below the original lines and moving lines to a standard location to making it compile, then decided it would be better to leave it original with (double) comments... A lesson for me to pick one path and verify.
Thank you for your comments and help and for all the time you put into helping me better understand the code. I apprecIate it - I haven't work with any programmng code since the early 1970's when, as a hobby, I built a sym micro computer using a 6502 processor and programmed in Assembly language. I'm leaning that Jumping into an object oriented language from the Basic and Fortran languages i used in school is a big jump, but I'll keep plugging along, and with help like yours, I'll get there.
It is still a fun hobby. Now, pre-made modules are one-pin solutions to opto-isolation and motor movement. You eat the biggest apple in the world... one byte at a time. : )