im going lesson by lesson. right now im on reading data from arduino serial monitor. everythings going great except that when it says how many times do you want the red led blink I put command in serial port then when it asks for the yellow for some reason it doesn't switch LED's it just stays on the same LED for both commands. can anyone help?
int redLedPin=9; //Declaring red led as an int, and set to 9:
int yellowLedPin=10; // Declaring yellow led as an int, and set to 10:
int redOnTime=250; //Red led is on 250:
int redOffTime=250; // Red led is off 250:
int yellowOnTime=250; // Yellow led is on 250:
int yellowOffTime=250; // Yellow led is off 250:
int numRedBlink; // declaring a variable
int numYellowBlink; // declaring a variable
String redMessage= "the red led is blinking"; // declaring a string variable
String yellowMessage= "the yellow led is blinking"; // declaring a string variable
void setup() {
Serial.begin(9600);
String wm1 ="hello nicholas "; // declare string variable
String wm2="its your program";
String wm3; // declare a sting variable
wm3=wm1+wm2; //concatenation take wm1+wm2=wm3
Serial.println(wm3);
pinMode(redLedPin,OUTPUT);
pinMode(yellowLedPin,OUTPUT);
int numRedBlink;
}
void loop() {
Serial.println("How Many Times I Want Red LED To Blink"); // asking
while (Serial.available()==0) // wait for input.
numRedBlink=Serial.parseInt();
int numYellowBlink;
Serial.println("How Many Times I Want Yellow LED To Blink"); // asking
while (Serial.available()==0) { } // wait for input
numYellowBlink=Serial.parseInt();
int c=c+1;
Serial.println(redMessage);
for (c=1; c<=numRedBlink; c=c+1){ // starting for loop
Serial.print("I am Awsome");
Serial.println(c);
digitalWrite(redLedPin,HIGH); // turn the red led on:
delay(redOffTime); // wait:
digitalWrite(redLedPin,LOW); // turn the red led off:
delay(redOffTime); //Wait:
}
Serial.println(" ");
Serial.println(yellowMessage);
for (int c=1; c<=numYellowBlink; c=c+1){
Serial.print("i am awsome");
Serial.println(c);
digitalWrite(yellowLedPin,HIGH); //turn the yellow led on:
delay(yellowOffTime); //Wait:
digitalWrite(yellowLedPin,LOW); //turn the yellow led off:
delay(yellowOffTime); //Wait:
Serial.println(" ");
}
}
void loop() {
Serial.println("How Many Times I Want Red LED To Blink"); // asking
while (Serial.available()==0) // wait for input. <<<<<<<<<<<<<<
numRedBlink=Serial.parseInt();
int numYellowBlink;
Serial.println("How Many Times I Want Yellow LED To Blink"); // asking
while (Serial.available()==0) { } // wait for input <<<<<<<<<<<<<<
numYellowBlink=Serial.parseInt();
int c=c+1;
They are subtly different but I suspect they should be the same?
im rewriting my lesson code so I can go back and twiddle with it and gain more exp. but ive run across an error code ive never seen before. its error is the last delay in code. can anyone help?
ITS exit status 1
expected constructor, destructor, or type conversion before '(' token
heres my code.
int yellowLedPin=10;
int redOnTime=250;
int redOffTime=250;
int yellowOnTime=250;
int yellowOffTime=250;
int numRedBlink=3;
void setup() {
pinMode(redLedPin,OUTPUT);
pinMode(yellowLedPin,OUTPUT);
Have a look at this way to do it (without waiting, without any delay).
const byte redLedPin = 9; // this is a small never changing number, treat it like that
const byte yellowLedPin = 10;
const int redOnTime = 250;
const int redOffTime = 250;
const int yellowOnTime = 250;
const int yellowOffTime = 250;
int numRedBlink;
int numYellowBlink;
unsigned long yellowStartTime;
unsigned long redStartTime;
void setup() {
Serial.begin(115200);
Serial.println(F("Hello Nicholas, it's me, your program!"));
Serial.println();
Serial.println(F("Enter 'r 10' to blink red 10 times, 'y 4' to blink yellow 4 times, one command per line."));
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
}
void loop() {
unsigned long topLoop = millis();
if (numYellowBlink) {
if (digitalRead(yellowLedPin)) {
if (topLoop - yellowStartTime >= yellowOnTime) {
digitalWrite(yellowLedPin, LOW);
yellowStartTime = topLoop;
}
} else {
if (topLoop - yellowStartTime >= yellowOffTime) {
yellowStartTime = topLoop;
if (--numYellowBlink) {
digitalWrite(yellowLedPin, HIGH);
}
}
}
}
if (numRedBlink) {
if (digitalRead(redLedPin)) {
if (topLoop - redStartTime >= redOnTime) {
digitalWrite(redLedPin, LOW);
redStartTime = topLoop;
}
} else {
if (topLoop - redStartTime >= redOffTime) {
redStartTime = topLoop;
if (--numRedBlink) {
digitalWrite(redLedPin, HIGH);
}
}
}
}
serialHandler();
}
void blinkMsg(const __FlashStringHelper* myColor, int myNumber) { // __FlashHelper*
Serial.print(F("The "));
Serial.print(myColor);
Serial.print(F(" led will blink "));
Serial.print(myNumber);
Serial.println(F(" times."));
}
void checkLine(const char* iLine) {
switch (tolower(*iLine)) {
case 'y':
numYellowBlink = atoi(++iLine);
blinkMsg(F("yellow"), numYellowBlink);
if (numYellowBlink) {
digitalWrite(yellowLedPin, HIGH);
yellowStartTime = millis();
} else {
digitalWrite(yellowLedPin, LOW);
}
break;
case 'r':
numRedBlink = atoi(++iLine);
blinkMsg(F("red"), numRedBlink);
if (numRedBlink) {
digitalWrite(redLedPin, HIGH);
redStartTime = millis();
} else {
digitalWrite(redLedPin, LOW);
}
break;
default:
Serial.print(F("cmd '"));
Serial.print(iLine);
Serial.println(F("' not found"));
}
}
void serialHandler()
{
byte inChar;
bool doCheck = false;
const byte sCBMax = 30;
static char sCBuffer[sCBMax];
static byte buffIndex = 0;
while (Serial.available()) {
inChar = Serial.read();
if (inChar == 10) {
continue;
} else if (inChar == 13) {
doCheck = buffIndex;
} else {
if ((buffIndex == 0) && isWhitespace(inChar)) {
continue;
}
sCBuffer[buffIndex++] = inChar;
doCheck = (buffIndex == (sCBMax - 1));
}
if (doCheck) {
sCBuffer[buffIndex] = 0;
checkLine(sCBuffer);
buffIndex = 0;
}
}
}
Hello Nicholas, it's me, your program!
Enter 'r 10' to blink red 10 times, 'y 4' to blink yellow 4 times, one command per line.
The red led will blink 12 times.
The yellow led will blink 9 times.