so, i'm new to the arduino, but it's been pretty easy to learn so far. however, i ran into a problem. i searched the forum for an answer but i couldn't really find what i was looking for.
void setup()
{
Serial.begin(9600);
pinMode(5, OUTPUT);
}
void loop()
{
String read1 = getstring().toLowerCase();
if(read1 == "on")
{
digitalWrite(5, HIGH);
Serial.println("LIGHT: ON");
}
if(read1 == "off")
{
digitalWrite(5, LOW);
Serial.println("LIGHT: OFF");
}
if(read1 == "blink")
{
Serial.println("BLINKING...");
digitalWrite(5, LOW);
delay(500);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
delay(500);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
}
if(read1.indexOf("dim:") >=0)
{
String parsedstring = read1.replace("dim:", "");
Serial.println("Light Dimmed to ");
Serial.println(parsedstring);
analogWrite(5, atoi(parsedstring));
}
delay(300);
}
String getstring()
{
String result;
while(Serial.available() > 0)
{
result = result + byte(Serial.read());
}
return result;
}
Basically the intent of the code is to take data from serial and make an LED do stuff.
when you type on, it turns on, off it turns off, blink it blinks, and dim:4 it dims to 4 it's all worked out pretty well except the dim part
if(read1.indexOf("dim:") >=0)
{
String parsedstring = read1.replace("dim:", "");
Serial.println("Light Dimmed to ");
Serial.println(parsedstring);
analogWrite(5, atoi(parsedstring));
}
if i comment out analogWrite it sends the right value to the console, but i'm having trouble converting the String to an Integer
i keep getting
error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'