else without previous if

so i have been getting help from some excellent users on here with this code. it has grown from just getting an input to operate an output, to having an alternating output, to having set and reset functions.
and now i have added analog inputs after running the code for them separately and this has caused a small headache.

the circuit has an alternating HIGH LOW digital output (ledPin), as well as another digital output for (alarmPin).
it also has an alarm acknowledge digital input.
in conjunction to this it also has 2 analog inputs which represent a threshold setpoint pot and a variable voltage output sensor, both of these are represented just as pots.

how this is supposed to work is that the ledPin will flash on and off, and if while it is off, if the sensor input supplies a value greater than the value of the threshold pot then the alarmPin will be set to HIGH.

if the value of the sensor pin is lower than the threshold value of the pot then the alarmPin will be LOW.

if the alarmPin goes high it will stay on until the alarmAcknowledge input is sent high.

i have managed to get the circuit to work with the analog inputs, just using digital inputs and having no threshold value.
and then i wrote another little bit of code to compare 2 analog values and give me a digital output if one val was higher than the other.
and then i combined them together and nowwwwwwww i am stuck.

i have gone over the arduino else if tutorials and changed, added,removed brackets and braces and am getting super frustrated.

unsigned long startMillis; //some global variables anywhere in the program
unsigned long currentMillis; 
const unsigned long period = 1000; //the value is a number in milliseconds
const byte ledPin = 13; // using the built in led
const int alarmPin =  12; // output for alarm
int analogPin1 = 0;        // threshold setpoint
int val = 0;
int analogPin2 = 0;         // IR sensor alarm
int val2 = 0; 
const int alarmAck = 10; //reset for alarm


void setup() {
  // put your setup code here, to run once:
Serial.begin (115200); //start serial in case we need to print debugging info
pinMode (ledPin,OUTPUT);
pinMode (alarmPin,OUTPUT);
pinMode (alarmAck, INPUT);
startMillis = millis (); //initial start time
}

void loop() {
  // put your main code here, to run repeatedly:
currentMillis = millis ();                          //get the current ''time'' (actually the number of milliseconds since the program started
if (currentMillis - startMillis >= period)         // test whether the period has elapsed
{digitalWrite (ledPin, !digitalRead(ledPin));     //if so, change the state of the led
startMillis = currentMillis; }                   // IMPORTANT to save the start time of the current led state

val = analogRead;                                   // get value of IR setpoint
val2 = analogRead;                                 // get value of IR sensor     

if 

(digitalRead(ledPin) == LOW && (val2 >= val));     // if led high and val2 greater than or eqaul to val

 {digitalWrite (alarmPin, HIGH);                // turn alarm on
 else                                           // this ELSE is where i am getting the else without previous if error.
  digitalWrite (alarmPin,Low);}
 

if (digitalRead (alarmAck) == HIGH && digitalRead (alarmPin) ==HIGH)  //if alarm acknowledge input is high and alarm output is high turn alarm output off.
 {digitalWrite (alarmPin, LOW);}



}
if 

(digitalRead(ledPin) == LOW && (val2 >= val));

get rid of the semi-colon

yeah i tried that, it gives me another error. expected } before else.

f111:
yeah i tried that, it gives me another error. expected } before else.

So it is expecting a }, that is because you missed one out. You also missed the curly braces around the else statement and you used Low instead of LOW.
That if statement should read:-

if(digitalRead(ledPin) == LOW && (val2 >= val)) {     // if led high and val2 greater than or equal to val
  digitalWrite (alarmPin, HIGH); // turn alarm on
  }
 else {                                         
  digitalWrite (alarmPin,LOW);
  }

Here is a suggestion for you.
In the Arduino IDE, press T.
This will auto format the code.
Then go through your code, line by line.
Make sure that every brace '{' '}' is on a line by itself.
Make sure that every time you see the word "if", there is something after the word (if should not be on a line by itself)
Make sure that every if statement is not terminated by a ; on the same line (as per the post above your last post).

Once you have done that, press T again.

Your errors should pop right out at you.

thank you for that Mike, my code now compiles but wont run. thats my issue though, i was just frustrated that i couldnt get it to even compile. so now i can try and make it work.

so does it actually matter which line the IF and ELSE are placed on? not just the location of them relative to the brackets and braces, but also which line.

It does matter, because getting too creative with formatting makes it difficult to debug, which, as you know, leads to frustration.

There are varients of the 'every-brace-on-a-line' style, but the idea is the same - use the code blocks to group code logically. Above all, be consistent.

my code now compiles but wont run.

If it compiles and is uploaded then it is running. Maybe not doing what you want, but running all the same

Please post the latest version here and describe why you think that it is not running

Hi,
Can you post your now compiling code please?

Thanks.. Tom... :slight_smile:

Hi,

int analogPin1 = 0;        // threshold setpoint
int val = 0;
int analogPin2 = 0;         // IR sensor alarm
int val2 = 0;

Analog pins are A0 and A1.

val = analogRead;                                   // get value of IR setpoint
val2 = analogRead;                                 // get value of IR sensor

Is not how you read an analog input.

Tom.... :slight_smile:

Analog pins are A0 and A1.

No that bit is right. A0 and A1 are used when you use analogue pins for digital I/O

From
https://www.arduino.cc/en/Tutorial/AnalogInputPins

The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH:

pinMode(A0, OUTPUT ) ;
digitalWrite(A0, HIGH ) ;

Hi,
The OP is using them for analog input, not digital, and A0 and A1 make a much nicer and readable code.

In this case both inputs are being designated 0, not 0 and 1, or A0 and A1.

We need to see the OPs new code.

Tom... :slight_smile:

f111:
thank you for that Mike, my code now compiles but wont run. thats my issue though, i was just frustrated that i couldnt get it to even compile. so now i can try and make it work.

so does it actually matter which line the IF and ELSE are placed on? not just the location of them relative to the brackets and braces, but also which line.

The compiler has rules and whitespace does not matter. This works --

byte test = 12;
if ( test ) test = 0; else test = 1;
Serial.println( test );

I do the braces on the same indent level because it's much easier&faster for my eyes to match them up. It's easier to see mismatches.

if ( test ) // looks at the value of test, computer false is 0 and true is not 0. ( test > 0 ) evaluates to 0 or 1 as false or true.

if ( test )
{
test = 0;
}
else
{
test = 1;
}

All of that spread-out is simply to make it easier for me to read. It also makes it easier to add or change lines.

so thanks to grumpy Mike for helping me get the ELSE to compile, and now i dont need it haha.
i have built this code one block at a time and had help with each piece and slowly joined them all together, the issue with this is that the different codes do not work the way i think they do, so back to the drawing board.

for those that are interested here it is atm, i will more than likely be ripping it apart today and trying to reinforce what i have learnt so far and then trying to build this again.

unsigned long startMillis; //some global variables anywhere in the program
unsigned long currentMillis; 
const unsigned long period = 1000; //the value is a number in milliseconds
const byte ledPin = 13; // using the built in led
const byte alarmPin =  12; // output for alarm
int analogPin1 = 0;        // threshold setpoint
int val = 0;
int analogPin2 = 1;         // IR sensor alarm
int val2 = 0; 
const byte alarmackPin = 10; //reset for alarm


void setup() {
  // put your setup code here, to run once:
Serial.begin (115200); //start serial in case we need to print debugging info
pinMode (ledPin,OUTPUT);
pinMode (alarmPin,OUTPUT);
pinMode (alarmackPin, INPUT);
startMillis = millis (); //initial start time
}

void loop() {
  // put your main code here, to run repeatedly:
currentMillis = millis ();                          //get the current ''time'' (actually the number of milliseconds since the program started
if (currentMillis - startMillis >= period)         // test whether the period has elapsed
{digitalWrite (ledPin, !digitalRead(ledPin));     //if so, change the state of the led
startMillis = currentMillis; }                   // IMPORTANT to save the start time of the current led state

val = analogRead;                                   // get value of IR setpoint
val2 = analogRead;                                 // get value of IR sensor     

if (digitalRead(ledPin) == LOW && (val2 <= val)) {     // if led high and val2 greater than or eqaul to val
  digitalWrite (alarmPin, HIGH);                // turn alarm on
}
 


if (digitalRead (alarmackPin) == HIGH && digitalRead (alarmPin) ==HIGH)  //if alarm acknowledge input is high and alarm output is high turn alarm output off.
 {digitalWrite (alarmPin, LOW);}



}
startMillis = millis ();   ——— usually see = millis()
currentMillis = millis ();

val = analogRead;   ——-- usually see = analogRead(analogPin)
val2 = analogRead;

Those might be a start..

Hi,
@f111 did you read post #9, your analog read is not correct.

Thanks.. Tom..