So, I am doing a small test project on some automated doors involving 2 boards, one to transmit, the other to receive.
I'm running into issues with the board that's receiving. Certain lines of my codes are probably bugging it out, but I really need those lines to work so that I can get the end result.
So, here are the codes.
#include <VirtualWire.h>
#include <ServoTimer2.h>
// a buffer to store the incoming messages
byte message[VW_MAX_MESSAGE_LEN];
// the size of the message
byte messageLength = VW_MAX_MESSAGE_LEN;
//int ledPin = 5;
int ServoPin = 3;
ServoTimer2 servo1;
const int buzzer = 9;
int redPin = 7;
int greenPin = 5 ;
int bluePin = 6 ;
//int ServoPin = 3;
//ServoTimer2 servo1;
void setup()
{
// Initialize the serial monitor
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
// set led pin as an output
//pinMode(ledPin, OUTPUT);
servo1.attach(ServoPin);
pinMode(redPin, OUTPUT);
//Serial.println ("Check 1");
pinMode(greenPin, OUTPUT);
//Serial.println ("Check 2");
pinMode(bluePin, OUTPUT);
//Serial.println ("Check 3");
pinMode(buzzer, OUTPUT);
}
void setColor(int red, int green, int blue)
{
digitalWrite(redPin, red);
digitalWrite(greenPin, green);
digitalWrite(bluePin, blue);
}
void loop()
{
//setColor(255, 0, 0); // red
//delay(1000);
//setColor(0, 255, 0); // green
//delay(1000);
//setColor(0, 0, 255); // blue
//delay(1000);
/*setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000); */
if (vw_get_message(message, &messageLength))
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
if(message[i]=='J')
{
//digitalWrite(ledPin,HIGH);
digitalWrite(redPin, HIGH);
Serial.write(", RED LED ON");
setColor(255, 0, 0);
servo1.write(750);
delay(1000);
}
if(message[i]=='K')
{
//digitalWrite(ledPin,LOW);
digitalWrite(greenPin,HIGH);
Serial.write(", GREEN LED ON");
setColor(0, 255,0 );
servo1.write(2250);
//tone(buzzer, 440);
delay(1000);
//noTone(buzzer);
//servo1.write(2250);
//delay(1000);
}
if (message [i] == 'L')
{
digitalWrite(bluePin,HIGH);
Serial.write(", BLUE LED ON");
setColor(0, 0, 255);
//servol.write (1250);
delay(1000);
}
if (message [i] == 'M')
{
digitalWrite(bluePin,HIGH);
Serial.write(", AQUA LED ON");
setColor(0, 255,255 );
//servol.write(900);
delay(1000);
}
}
Serial.println();
}
}
Do ignore the 'L' and 'M' sections as they were testers.
What I'm running issues into is the 'K' section. Currently, without the buzzer on, the code runs smoothly. e.g. when the condition is met, the rgb led turns green and the servo rotates as instructed. when condition 'J' is met, the led turns red and the servo turns back (i need to work on how much the servo rotates).
However, when I put the codes for the buzzer, my servo only rotates ONE time for both the red and green, aka, it doesn't keep rotating back and forth when the conditions are met.
The leds still work and so does the buzzer. I feel that the codes for the buzzer are causing the issue, how can I fix this?