How do I fix the error that says 'ledpin' was not declared in this scope?

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

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

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!"); 
  }
}