Hello-
I'm fairly new the Arduino and looking for a small amount of code help to get me over this stumbing block- Please!
Here's what I'm trying to make:
1.An RF transmitter circuit with a START and STOP button to remote control a receiver
2.An RF receiver circuit that runs some servos, starting when it receives the RF "start" message and stopping via an interrupt whenever it receives an RF "stop" command.
The part I'm struggling with is the "stop" command. I don't know how to implement it in my code. Right now I've tried a while loop, but realized it only checks for that "stop" message at the beginning of the main routine.
Here is my RECEIVER code:
#include <VirtualWire.h>
int startLEDPin = 11;
int stopLEDPin= 12;
int runLEDPin = 4;
void setup()
{
// initialize input and output remote pins
pinMode(startLEDPin, OUTPUT);
pinMode(stopLEDPin, OUTPUT);
pinMode(runLEDPin, OUTPUT);
digitalWrite(startLEDPin, LOW);
digitalWrite(stopLEDPin, LOW);
digitalWrite(runLEDPin, LOW);
Serial.begin(9600); // Debugging only
Serial.println("setup");
vw_setup(1000); // Bits per sec- can go up to 2400
vw_set_rx_pin(2);
vw_rx_start(); // Start the receiver PLL running
}
void MainRoutine()
{
digitalWrite(runLEDPin, LOW);
delay(5000); // this represents the main routine (motor) stuff
}
void ShutDown()
{
digitalWrite(startLEDPin, LOW);
digitalWrite(runLEDPin, LOW);
digitalWrite(stopLEDPin, HIGH);
delay(2000) // this represents the motor shutdown stuff
digitalWrite(stopLEDPin, LOW);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// beginning of RF check
while (vw_get_message(buf, &buflen) && buf[0] != '1') {
}
// while msg is avail (good checksum or not) copy it to buffer and do nothing else
while (vw_get_message(buf, &buflen) && buf[0] != '2')
{
// now that "1" was received (for START) do this stuff...
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
delay(500);
digitalWrite(13, false);
Serial.print("Got: "); // take this out later
Serial.print(buf[0]); // print the content of the msg recvd
Serial.println("");
MainRoutine();
}
// once "2" (for STOP) is received go into shutdown mode
ShutDown();
// then start over at the top waiting for another start button press
}
For reference, here is the TRANSMITTER code w/debounce check. It seems to be working o.k.:
#include <VirtualWire.h>
int startPin = 5;
int stopPin = 6;
int startLEDPin = 11;
int stopLEDPin= 12;
boolean lastStartButton = LOW;
boolean currentStartButton = LOW;
boolean lastStopButton = LOW;
boolean currentStopButton = LOW;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
vw_setup(1000); // Bits per sec- can go up to 2400
vw_set_tx_pin(3);
// initialize input and output remote pins
pinMode(startPin, INPUT);
pinMode(stopPin, INPUT);
pinMode(startLEDPin, OUTPUT);
pinMode(stopLEDPin, OUTPUT);
digitalWrite(startLEDPin, LOW);
digitalWrite(stopLEDPin, LOW);
}
boolean debounceStart(boolean lastStart)
{
boolean currentStart = digitalRead(startPin); // determine the current value of the START switch
if (lastStart != currentStart) // if switch changed states, delay by 5 ms so switch can stop debouncing
{
delay(5);
currentStart = digitalRead(startPin); // read it again after 5 ms
}
return currentStart; // return the value of the switch read after the 5 ms delay
}
boolean debounceStop(boolean lastStop)
{
boolean currentStop = digitalRead(stopPin); // determine the current value of the STOP switch
if (lastStop != currentStop) // if switch changed states, delay by 5 ms so switch can stop debouncing
{
delay(5);
currentStop = digitalRead(stopPin); // read it again after 5 ms
}
return currentStop; // return the value of the switch read after the 5 ms delay
}
void loop()
{
char *msg;
lastStartButton = LOW; // reseting if this isn't the first loop
currentStartButton = LOW; // reseting if this isn't the first loop
lastStopButton = LOW; // reseting if this isn't the first loop
currentStopButton = LOW; // reseting if this isn't the first loop
// START of routine for transmitting message
currentStartButton = debounceStart(lastStartButton); // run debounce function, pass it LastButton
if(lastStartButton == LOW && currentStartButton == HIGH) // if START button actually changed, do stuff below
{
char *msg = "1"; // a message sent of "1" means START
lastStartButton = currentStartButton; // resetting the last stop back to the current state
digitalWrite(startLEDPin, HIGH);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // Turn off light after transmission
digitalWrite(startLEDPin, LOW);
}
currentStopButton = debounceStop(lastStopButton);
if(lastStopButton == LOW && currentStopButton == HIGH) // if STOP button actually changed, do stuff below
{
char *msg = "2"; // a message sent of "2" means STOP
lastStopButton = currentStopButton; // resetting the last stop back to the current state
digitalWrite(stopLEDPin, HIGH);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // Turn off light after transmission
digitalWrite(stopLEDPin, LOW);
}
// END of routine for transmitting message
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.