Two digitalWrite

Hi

In this sketch I would like lines with digitalWrite.Unfortunately you cannot see
the number of the lines here but if you look at the void loop you where I wrote
two digitalWrite lines:

if((buttonState1==LOW)&&(temperature<=setpoint-1))
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin3,HIGH);
else
digitalWrite(ledPin1,LOW);

When I do so i get a error message: elsewithout previous if.

If I write like this it works as expected:

if((buttonState1==LOW)&&(temperature<=setpoint-1))
digitalWrite(ledPin1,HIGH);
else
digitalWrite(ledPin1,LOW);

Why does this happen?Hope you understand what I mean.

Regards
Jan Ingesson

#include <OneWire.h>
#include <LiquidCrystal.h>
int DS18S20_Pin = 10;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Temperature chip i/o
OneWire ds(DS18S20_Pin);
///////////////////////////////////
const int switch1 = A1;
const int switch2 = A2;
const int ledPin1 = 7;
const int ledPin2 = 8;
const int ledPin3 = 13;
byte setpoint = 23;

int buttonState1 = 0;
int buttonState2 = 0;

///////////////////////////////////

void setup() {
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);

///////////////////////////////////
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" ");
}

void loop() {
buttonState1=digitalRead(switch1);
buttonState2=digitalRead(switch2);
float temperature = getTemp();
Serial.println(temperature);
//////////////////////////////////
if((buttonState2==LOW)&&(temperature>=setpoint))

digitalWrite(ledPin2,HIGH);
else
digitalWrite(ledPin2,LOW);

if((buttonState1==LOW)&&(temperature<=setpoint-1))
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin3,HIGH);
else
digitalWrite(ledPin1,LOW);

//////////////////////////////////
lcd.setCursor(0, 1);
lcd.print(“Temp : “);
lcd.print(temperature);
lcd.print(” *C”);
delay(50); //just here to slow down the output so it is easier to read

}

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -50;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -50;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print(“Device is not recognized”);
return -50;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(50); /////////////////
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();
}
{

}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two’s compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}

if((buttonState1==LOW)&&(temperature<=setpoint-1))
{
  digitalWrite(ledPin1,HIGH); 
  digitalWrite(ledPin3,HIGH);
}
else
{
  digitalWrite(ledPin1,LOW);
}

Please remember to use code tags when posting code.

Nice code. Italics and smileys, we don't see that combination that often :D

Please edit your post and place ``` [code] ``` before your code and ``` [/code] ``` after your code.

So it looks like your code here This will prevent italics and so on and is easier to read and copy.

If you need to execute more than one statement in the event that a conditional is true, you need to enclose it in {}'s (as AWOL showed)

I recommend always putting in the {}'s even if there’s only one statement - it makes the code easier to read.

Thanks for your your help guys!Now I dont get a error message any longer.Ill try if the code works as I wish later today.I live in Sweden.Guess you live in USA If so there is a big time difference.Understand I have a lot more to learn in programming :)