I want to have a yellow LED start blinking when say case ‘r’: is pressed. This will represent a turn signal. The issue i run into is the LED will turn on then turn off but then it will not loop back and keep blinking. I get one blink. Any ideas?
That’s right, you can’t use the delay() function because it behaves like a busy wait: busy waits are not always good because the program can’t do anything else while it is waiting. If you don’t want the blinking to affect the key commands that are received, we can’t use delay().
Similarly, the
while (Serial.available() < 1) {}
line found at the beginning of the while() loop is another busy wait: the program can’t do anything until a key is received. If you want your blinking to occur regularly, independent of when keys are received, we’ll have to change that too.
I would first recommend changing the while (Serial.available() < 1) to instead by an if statement, where everything in the loop() function that follows that line is included in the true-block (e.g. put the closing curly brace after the end of the switch block). That portion of your code would then loop something like this:
void loop(void) {
digitalWrite(led, HIGH);
if (Serial.available() >= 1) { // Wait until a character is received
char ch = Serial.read();
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
int slowspeed = 0;
int nospeed = 0;
switch(ch) // Perform an action depending on the command
{
case 'w'://Move Forward
forward (leftspeed,rightspeed);
Serial.println("Moving Forward Sir");
break;
case 's'://Move Backwards
...]
default:
stop();
break;
}
}
delay(300); // to limit the looping rate
}
Note that I changed the if condition slightly, and added a delay(300) after the if-statement. This delay will limit the overall speed of the looping of your program. In our case, since the blinking will be the fastest thing running, we will use 300 (the rate of the blinking) as the delay.
So far, we’ve only changed the WASD program to remove the busy wait on Serial.available(). Now we will need to add the code for flashing. From what I understand, you want the flashing to start when the ‘r’ button is pressed, and then might want it to stop when the ‘r’ is pressed again. Is this correct?
In this case, we should add a new case to our switch block like so:
case 'r':
blinkingR = !blinkingR;
break;
This blinkingR variable is then a flag to indicate if the blinking state is currently active. We will need to define this variable earlier, before the setup() function like so: int blinkingR = 0;
Finally, we need to add the code that will do the actual blinking. We will need to add it outside the if-statement, before the delay:
if (blinkingR) { // If blinking, alternate the state every time we loop
if (stateR == LOW) {
stateR = HIGH;
} else {
stateR = LOW;
}
} else { // If not blinking, turn the state LOW
stateR = LOW;
}
digitalWrite(ledR, stateR);
This stateR variable is another flag, this time to indicate the current state (on/off) of the LED. We will also need to define this variable earlier, before the setup() function like so: int stateR = LOW;
This should be the basics of what you need to do. If you want to repeat it for another LED, I imagine the left, you should be able to just copy-paste, and rename a the variables. Let me know how it goes!
Great, we’re glad it worked!
Right, that would be the ledR variable that you have to define above the start() function, and should set its pinMode to output inside the start() function.
Can you post the code you have so far? If your main loop repeats at the same rate as the flashing rate, then it should be easy to do, but if you have different rates it will be harder depending on the specific implementation…
Where do I assign the LED which needs to blink? Where do I put a pin number in the code?
I realized this once I started looking at the code further. I have everything hooked up and it is working perfectly, Thank you so much!
Here is the very simple code I am testing it in:
int led = 2;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.print("Robot is ready ");
}
void loop() {
while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
switch(val)
{
case 'r':
digitalWrite(led, HIGH);
delay(300);
digitalWrite(led, LOW);
delay(300);
break;
default:
break;
}
}
I will post the code I want to put it in eventually in a separate post.
Here is the code I want to incorporate it into but I have done some research and it says I wont be able to use delays because it might cause it to miss a key input. I have been thinking of maybe using millis. I have never used millis though. I don’t even know how they work. I tried going off this: http://arduino.cc/en/Tutorial/BlinkWithoutDelay but I just got confused.
[code]
#include <Servo.h>
Servo servo1;
int led = 13;
int headlight = 5;
int headlight2 = 6;
int spotlight = 3;
const int Motor1Pin1 = 8;
const int Motor1Pin2 = 9;
const int Motor2Pin1 = 10;
const int Motor2Pin2 = 11;
int startServo;
int pulseWidth;
void setup(void) {
pinMode(led, OUTPUT);
pinMode(headlight, OUTPUT);
pinMode(headlight2, OUTPUT);
pinMode(spotlight, OUTPUT);
pinMode(14,OUTPUT);
servo1.attach(14); //analog pin 0
servo1.write(140);
Serial.begin(9600);
Serial.println(“Robot Is Ready Sir!”);
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
pinMode(Motor1Pin1, OUTPUT);
pinMode(Motor1Pin2, OUTPUT);
pinMode(Motor2Pin1, OUTPUT);
pinMode(Motor2Pin2, OUTPUT);
}
void loop(void) {
digitalWrite(led, HIGH);
while (Serial.available() < 1) {} // Wait until a character is received
char ch = Serial.read();
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
int slowspeed = 0;
int nospeed = 0;
switch(ch) // Perform an action depending on the command
{
case ‘w’://Move Forward
forward (leftspeed,rightspeed);
Serial.println(“Moving Forward Sir”);
break;
case ‘s’://Move Backwards
reverse (leftspeed,rightspeed);
Serial.println(“Moving Backwards Sir”);
break;
case ‘a’://Turn Left
left (leftspeed,rightspeed);
Serial.println(“Turning Left Sir”);
break;
case ‘d’://Turn Right
right (leftspeed,rightspeed);
Serial.println(“Turning Right Sir”);
break;
case ‘f’: //Stop
halt (slowspeed,nospeed);
Serial.println(“We Have Halted Sir”);
break;
case ‘e’:
servo1.write(1);
Serial.println(“Door Open Sir”);
break;
case ‘q’:
servo1.write(140);
Serial.println(“Door Closed Sir”);
break;
case ‘z’:
digitalWrite(headlight, HIGH);
digitalWrite(headlight2, HIGH);
Serial.println(“Headlights are ON Sir”);
break;
case ‘x’:
digitalWrite(headlight, LOW);
digitalWrite(headlight2, LOW);
Serial.println(“Headlights are OFF Sir”);
break;
case ‘c’:
digitalWrite(spotlight, HIGH);
Serial.println(“Spotlight is ON Sir”);
break;
case ‘v’:
digitalWrite(spotlight, LOW);
Serial.println(“Spotlight is OFF Sir”);
break;
default:
stop();
break;
}
}
void stop(void) //Stop
{
digitalWrite(Motor1Pin2,LOW);
digitalWrite(Motor1Pin1,LOW);
digitalWrite(Motor2Pin2,LOW);
digitalWrite(Motor2Pin1,LOW);
}
void forward(char a,char b)
{
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor1Pin1, HIGH);
digitalWrite(Motor2Pin2, LOW);
digitalWrite(Motor2Pin1, HIGH);
}
void reverse (char a,char b)
{
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor1Pin2, HIGH);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, HIGH);
}
void left (char a,char b)
{
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor1Pin2, HIGH);
digitalWrite(Motor2Pin2, LOW);
digitalWrite(Motor2Pin1, HIGH);
}
void right (char a,char b)
{
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor1Pin1, HIGH);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, HIGH);
}
void halt (char a, char b)
{
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, LOW);
}
̂[/code]