The transmitter code works good and we can receive values on the receiver end. When we add in the cases for how the motors will respond to the input it will get stuck in one of the loops and fail to receive other values. The photoresistor also doesn't work with the transmitter. If there is a way to change cases while getting the photoresistor to work that'd be awesome! Here is the code for the Transmitter:
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
const int JoyYpin = A1;
const int JoyXpin = A0;
int JoyY;
int JoyX;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
JoyY = analogRead(JoyYpin);
JoyX = analogRead(JoyXpin);
//STRAIGHT FORWARD
if (JoyY>600 && 600>JoyX && JoyX>400){
const char *msg = "A";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
//STRAIGHT BACKWARD
else if (JoyY<400 && 600>JoyX && JoyX>400){
const char *msg = "B";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
//FORWARD RIGHT
else if (JoyY>600 && JoyX<400){
const char *msg = "C";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
//FORWARD LEFT
else if (JoyY>600 && JoyX>600){
const char *msg = "D";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
//BACKWARD LEFT
else if (JoyY<400 && JoyX>600){
const char *msg = "E";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
//BACKWARD RIGHT
else if (JoyY<400 && JoyX<400){
const char *msg = "F";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
//DO NOTHING
else{
const char *msg = "X";
rf_driver.send((uint8_t*)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(500);
}
}
Here is the Code for the receiver:
#include <ServoTimer2.h>
#include <RH_ASK.h>
#include <SPI.h>
// Ask Driver
RH_ASK rf_driver;
// Setup Horn + Interrupt function
const int Horn = 7;
void Horned() {
digitalWrite(Horn, HIGH);
}
// Setup the DC Motor
const int EN = 5; // Half Bridge enable
const int MC1 = 4; // Motor Control 1
const int MC2 = 3; // Motor Control 2
int motorSpeed;
// Functions for Motor
void Forward() {
digitalWrite(EN, HIGH);
digitalWrite(MC1, HIGH);
digitalWrite(MC2, LOW);
}
void Backwards() {
digitalWrite(EN, HIGH);
digitalWrite(MC1, LOW);
digitalWrite(MC2, HIGH);
}
void STOP() {
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
}
// Photoresistor
int LightPin = A0;
// headlights + Function
int Headlights = 9;
void LightsOn() {
digitalWrite(Headlights, HIGH);
}
void LightsOff() {
digitalWrite(Headlights, LOW);
}
//Setup for Servo Motor + Function for Right and Left
ServoTimer2 servo1;
int position = 90;
//Setup functions for Turning
void Left() {
servo1.write(0);
}
void Right() {
servo1.write(180);
}
void setup() {
//Ask Driver
rf_driver.init();
// Setup DC Motor
pinMode(EN, OUTPUT);
pinMode(MC1, OUTPUT);
pinMode(MC2, OUTPUT);
// Setup LEDs
pinMode(Headlights, OUTPUT);
//Setup Horn Horn
pinMode(Horn, OUTPUT);
// Setup Servo Pins
servo1.attach(10);
// Interrupt for Horn
attachInterrupt(digitalPinToInterrupt(Horn), Horned, RISING);
Serial.begin(9600);
}
void loop() {
uint8_t buf[1];
uint8_t buflen = sizeof(buf);
// Photosensor
int Light_Reading = analogRead(LightPin);
if (Light_Reading < 500) {
LightsOn();
}
else {
LightsOff();
}
//Receive message from transmitter
if (rf_driver.recv(buf, &buflen)) {
Serial.println((char*)buf);
// Control DC Motor and Sterring
if (buf[0] == 'A') { // Foward Control with not turning
Forward();
}
else if (buf[0] == 'C') { // Forward and Turn Right
Forward();
Right();
}
else if (buf[0] == 'D') { // Forward and Turn Left
Forward();
Left();
}
else if (buf[0] == 'B') { // Backwards Control with not Turning
Backwards();
}
else if (buf[0] == 'E') { // Backwards and Right
Right();
Backwards();
}
else if (buf[0] == 'F') { // Forward and Turn Left
Backwards();
Left();
}
else { // Forward and Turn Left
STOP();
}
}
}