system
September 7, 2011, 11:04am
1
void setup()
{
Serial.begin(9600); //Set serial baud rate to 9600 bps
pinMode (10,OUTPUT);
}
void loop ()
int (valGas , valLDR) ;
valGas = analogRead(0);
valLDR = analogRead(1);
Serial.println(valGas,DEC);
Serial.print (",");
Serial.println in(valLDR,DEC);
if(valGas<600) {
digitalWrite (10,High);
} else }
digitalWrite (10,Low);
}
if (valLDR<400) {
digitalWrite(11,High);
} else }
digitalWrite(11,Low);
}
delay (100) ;
}
i get error when i want to compile this code..can anybody help me to see what is wrong with my code?
mmcp42
September 7, 2011, 11:07am
2
void setup()
{
Serial.begin(9600); //Set serial baud rate to 9600 bps
pinMode (10,OUTPUT);
}
void loop ()
int (valGas , valLDR) ;
valGas = analogRead(0);
valLDR = analogRead(1);
Serial.println(valGas,DEC);
Serial.print (",");
Serial.println in(valLDR,DEC);
if(valGas<600) {
digitalWrite (10,High);
} else }
digitalWrite (10,Low);
}
if (valLDR<400) {
digitalWrite(11,High);
} else }
digitalWrite(11,Low);
}
delay (100) ;
}
makes it easier to read
try
int valGas;
int valLDR;
or
int valGas , valLDR ;
no brackets!
system
September 7, 2011, 11:12am
3
sorry, my bad.
i try both ways..but still error.
mmcp42
September 7, 2011, 11:14am
4
you also need
void loop
{
... your code
}
you left out the "{" and "}"
system
September 7, 2011, 11:17am
5
Serial.println(valgas,DEC);
Serial.print (",");
Serial.println(valLDR,DEC);
is this part right?
system
September 7, 2011, 11:29am
6
if(valGas<600) {
digitalWrite (10,High);
} else }
digitalWrite (10,Low);
}
if (valLDR<400) {
digitalWrite(11,High);
} else }
digitalWrite(11,Low);
}
delay (100) ;
}
is this the right way to write if-else statement?
system
September 7, 2011, 11:34am
7
is this part right?
If you want that extra carriage return/line feed in the middle, yes. Seems strange to me, though.
is this the right way to write if-else statement?
Sort of. The braces are not all facing the right way, and they are not all on separate lines where they belong (in my opinion).
if(valGas<600)
{
digitalWrite (10,High);
}
else
{
digitalWrite (10,Low);
}
system
September 7, 2011, 11:38am
8
void setup()
{
Serial.begin(9600); //Set serial baud rate to 9600 bps
pinMode (10,OUTPUT);
}
void loop ()
{
int valgas , valLDR;
valgas = analogRead(0);
valLDR = analogRead(1);
Serial.println(valgas,DEC);
Serial.print (",");
Serial.println (valLDR,DEC);
}
if(valGas<600)
{
digitalWrite (10,High);
}
else
{
digitalWrite (10,Low);
}
if (valLDR<400)
{
digitalWrite(11,High);
}
else
{
digitalWrite(11,Low);
}
delay (100) ;
}
this is my current coding,
however, i still got error at this part
if(valGas<600)
{
digitalWrite (10,High);
}
else
{
digitalWrite (10,Low);
}
if (valLDR<400)
{
digitalWrite(11,High);
}
else
{
digitalWrite(11,Low);
}
delay (100) ;
}
mmcp42
September 7, 2011, 11:46am
9
look at you code
you have { and } all over the place
you should have
void loop()
{
.. all of your code
if (condition)
{
.. true code
}
else
{
... false code
}
}
use indents to help you keep track
and check matching { and }
system
September 7, 2011, 11:48am
10
The IDE has a menu, Tools. On that menu, there is an item, Auto Format. Do your self a favor. Use that tool. Often. It will fix all your random indents. It will also line up all your curly braces, indenting in and out as required. When it gets to the end of a function, and there are still closing } left over, they will bot be indented, leaving you a big clue that the number of open and close braces don't match.
ALL code needs to be inside a function. Not all of yours is. Why not?