#include <RH_ASK.h>
#include <SPI.h>
byte pwm;//pwm value for the mottors
RH_ASK driver(2000,12,12,10,true);//Data pin connected to pin 12
void setup(){
if(!driver.init());//Must not be left out, otherwise the library will not initialize
}
void loop(){
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];//array of received values are stored here
uint8_t buflen = sizeof(buf);//size of the array, needed by the library
if(driver.recv(buf, &buflen)){//if the signal is received
pwm=85*((float)buf[1]/255)+170;//buf[1]=sensorArray[1], values adjusted for specific motors on this vehicle(values range from 170 to 255);
while(buf[0]=='1'){//buf[0] in this case corresponds to send("1") in the transmitter
analogWrite(6,pwm);//forward
analogWrite(11,0.3*pwm);//adjusted for moving to a side
if(driver.recv(buf, &buflen)=='0')//buf[0] in this case corresponds to sensorArray[0] in the transmitter,when the button is released, pwm's are set to 0
break;
}
while(buf[0]=='2'){
analogWrite(11,pwm);//forward
analogWrite(6,0.3*pwm);//adjusted for moving to a side
if(driver.recv(buf, &buflen)=='0')
break;
}
while(buf[0]=='3'){
analogWrite(6,pwm);//move
analogWrite(11,pwm);//forward
if(driver.recv(buf, &buflen)=='0')
break;
}
while(buf[0]=='4'){
analogWrite(3,pwm);//move
analogWrite(5,pwm);//backward
if(driver.recv(buf, &buflen)=='0')
break;
}
analogWrite(3,0);//stop all pwm's
analogWrite(5,0);//when not in
analogWrite(6,0);//any of the loops
analogWrite(11,0);
}
}
Comments