Hello, this is my first query on this forum. I am trying to make a hand gesture controlled bot, for this I am trying to send the data of accelerometer from one arduino to other by using 433Mhz modules. I am using radiohead library for this purpose in my code.
This is the transmitter part code -
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK transmitter;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//pinMode(8,INPUT);
//pinMode(9,INPUT);
//pinMode(10,INPUT);
if(!transmitter.init())
{
Serial.println("init failed");
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("X=");
Serial.print(analogRead(A0));
unsigned int x = analogRead(A0);
Serial.print(" Y=");
Serial.println(analogRead(A1));
unsigned int y = analogRead(A1);
//transmitter.send('x',1);
byte x_high = highByte(x);
byte x_low = lowByte(x);
//transmitter.send((byte)x_high,1);
//transmitter.send((byte)x_low,1);
//transmitter.send('y',1);
byte y_high = highByte(y);
byte y_low = lowByte(y);
//transmitter.send((byte)y_high,1);
//transmitter.send((byte)y_low,1);;
byte send_data[6];
send_data[0] = 'x';
send_data[1] = x_high;
send_data[2] = x_low;
send_data[3] = 'y';
send_data[4] = y_high;
send_data[5] = y_low;
transmitter.send((byte*)send_data,6);
delay(500);
//Serial.println(digitalRead(9));
//delay(2000);
//Serial.println(digitalRead(10));
//delay(2000);
}
This is my receiver side code -
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
RH_ASK receiver;//(2000,11,12,10);
void setup()
{
//pinMode(11,INPUT);
Serial.begin(9600); // Debugging only
if (!receiver.init())
Serial.println("init failed");
//pwm enable pins 9 and 10 for motor 1 and 2
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
//in1 and in2 for motor 1
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
//in3 and in4 for motor 2
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
// accelerometer readings 270 to 400
//stop at 325 to 345
void loop()
{
//Serial.println("Running");
byte buf[6];
uint8_t buflen = sizeof(buf);
if (receiver.recv(buf, &buflen)) // Non-blocking
{
int x = (int)buf[1] << 8 | buf[2]; // getting integer values from bytes
int y = (int)buf[4] << 8 | buf[5];
// Message with a good checksum received, dump it.
//if(buf[0]=='x' && buf[3]=='y')
Serial.println(buf[0]);
Serial.println(buf[3]);
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.println(y);
//Serial.println((char*)buf);
/*if(((x<450) && (x>250)) && ((y<450) && (y>250))) */
if ((y < 450) && (y > 250)) { // ISSUE CREATING PART
if (y < 325) {
int pwmOutput = map(y,325,250,0,255);
analogWrite(9,pwmOutput);
analogWrite(3,pwmOutput);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
else if (y > 345) {
int pwmOutput = map(y,345,450,0,255);
analogWrite(9,pwmOutput);
analogWrite(3,pwmOutput);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}
else {
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
}
else {
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
}
}
So basically what I am doing here is that I am sending and receiving X and Y values. In the receiver part code I have marked "ISSUE CREATING PART", so if I am not putting up this if else clause, I am able to receive the X and Y values properly and they are also showing on the serial monitor. This if else clause is just basically using these received values for driving the motors. When I am adding this if else clause, I am not even able to receive the X and Y values. Which means this is interfering in some way with the code above it which was working fine in its absence. I am unable to figure out the issue.
This snapshot is with "ISSUE CREATING PART" uploaded in receiver, and here the reception stops like this on the left and correct reception without "ISSUE CREATING PART" on the right -
I request you to point out the possible issues here.
Thank you.