I ran into a problem with a beginner code that I found online:
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}
void loop()
{
val=Serial.read();
if(val==‘R’)
{
digitalWrite(ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
delay(500);
Serial.println(“HelloWorld!”);
}
}
Any idea? Thanks
system
April 4, 2018, 11:44am
#2
I can’t see your picture, but I can’t see where you define ‘val’ or ‘ledpin’
//declare global variables up here
int ledpin = 13;
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}
void loop()
{
if(Serial.available() > 0 ){
val=Serial.read();
}
if(val=='R')
{
digitalWrite(ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
delay(500);
Serial.println("HelloWorld!");
}
}
You're trying to use a variable called ledpin and you haven't told it anything about ledpin. I'd guess you haven't copied the complete program. Have another look at wherever you got the program from, there should be an extra line at the top declaring ledpin.
Steve
ardly
April 4, 2018, 11:54am
#5
The compiler message is completely accurate.
You are calling pinMode with a variable 'ledpin'
pinMode(ledpin,OUTPUT);
You need to declare the variable so that the compiler knows what type it is and you need to assign a value to it.
See #2
and just for completeness, use the </> code tags when posting.
A little indenting never hurt either.
//declare global variables up here
int ledpin = 13;
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}
void loop()
{
if(Serial.available() > 0){
val=Serial.read();
}
if(val=='R')
{
digitalWrite(ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
delay(500);
Serial.println("HelloWorld!");
}
}