Hi,
So I am creating a 2 project simulation using proteus and Arduino ide. A controller and the device. What I am trying to do is to transmit and received a string to the controller and device. When I did a one-way communication, it works fine. However, if I did a 2-way it doesn't work. I have tried all sorts of ways in what I am capable of but I'm still having a hard time solving the problem any suggestions. I tried using the software serial along with the hardware serial but it still doesn't work Here's what I have so far sorry if it's a bit messy. Any help would be appreciated. Thanks.
I am using the Atmega328P microcontroller. But I am making it behave like an arduino uno
I wrote the code here in the post and included in the attachment
I also included in the attachment the simulation circuit for the device and controller
REMOTE:
/*
* TestingRemote_5
*
*/
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial extra(2,3); //RX,TX
//joystick
int X = A0;
int Y = A1;
int servopin=6;
Servo servo1;
//Variables for servo and motor control
int servoctrl;
int motorctrl;
//sending
char motor;
char servo;
char sw='1';
//receiving
const byte numchars = 3;
char rccvd[numchars];
char temprccvd[numchars];
float batt=0.0;
//checking
//int batt1[numchars]={'H','M','L'};
bool newdata = false;
void setup() {
Serial.begin(9600); //XBEE
extra.begin(9600); //For ////debugging purposes
//debug.println("REMOTE DEBUG");
pinMode(X,INPUT);
pinMode(Y,INPUT);
servo1.attach(servopin);
}
void loop() {
joystick();
senddata();
rccvdchars();
if (newdata == true)
{
strcpy(temprccvd,rccvd);
parsedata();
//battery();
//debugdata();
newdata = false;
}
}
void rccvdchars()
{
char start = '<';
char ended = '>';
static byte index = 0;
char data;
static bool inProg = false;
while (extra.available() >= 0)
{
data = extra.read();
if (inProg == true)
{
//debug.println("Start");
if ( data != ended)
{
rccvd[index] = data;
index++;
if ( index >= numchars)
{
index = numchars - 1;
}
}
else
{
//debug.println("data == ended");
rccvd[index] = '\0';
inProg = false;
index = 0;
newdata = true;
}
}
else if ( data == start)
{
//debug.println("data==start");
inProg = true;
}
}
}
void joystick()
{
motorctrl = analogRead(X);
servoctrl = analogRead(Y);
motorctrl >>= 1;
if ( motorctrl > 230 && motorctrl < 276)
{
motor = 'N';
}
else if ( motorctrl < 255)
{
motor = 'B';
}
else if ( motorctrl > 255)
{
motor = 'F';
}
if ( servoctrl >= 461 && servoctrl <= 553) //center
{
servo = 'C';
servo1.write(180);
}
else if (servoctrl < 451) //left
{
servo1.write(68);
servo = 'L';
}
else if (servoctrl > 563 ) //right
{
servo = 'R';
servo1.write(117);
}
}
void parsedata()
{
char *ptr;
ptr = strtok(temprccvd,";");
batt = atof(ptr);
ptr = strtok(NULL,";");
}
void senddata()
{
Serial.println((String)"<"+motor+";"+servo+";"+sw+">");
delay(15);
}
void debugdata()
{
// debug.println((String)"<"+motor+";"+servo+";"+sw+">");
delay(100);
}
void battery()
{
// debug.println(batt);
delay(100);
}
Here's the Device Code:
/*
* DeviceTesting_5
*/
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial extra(2,3); //RX,TX
//motors
int in1 = 10;
int in2 = 11;
int in3 = 4;
int in4 = 5;
int servopin = 6;
int pos1 = 0;
int pos2 =0;
Servo servo;
//receving
const byte numchars = 7;
char rccvd[numchars];
char temprccvd[numchars];
char motor[numchars]={0};
char smotor[numchars]={0};
char sw[numchars]={0};
//checking
int chkt = 0;
int pos = 0;
char motor1[numchars]={'F','B','N'};
char smotor1[numchars]={'L','R','C'};
char sw1[numchars]={'H','O'};
bool newdata = false;
//sending
char batts;
//other variables
float batt = A0;
float V;
void setup() {
Serial.begin(9600);
extra.begin(9600);
//debug.begin(9600);
//debug.println("DEVICE //debug");
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(batt,INPUT);
servo.attach(servopin);
}
void loop() {
senddata();
rccvdchars();
if (newdata == true)
{
strcpy(temprccvd,rccvd);
parsedata();
//debugdata();
newdata = false;
}
chkt = check();
if (chkt == 2)
{
movemotors();
pos2 = servomo();
servo.write(pos2);
//delay(15);
}
}
void senddata()
{
extra.println((String)"<"+V+">");
//delay(15);
}
void battery()
{
float val = analogRead(batt);
V = val*(5/1023.00)*2;
}
int servomo()
{
if (smotor[0] == smotor1[0])
{
pos1 = 50;
}
else if (smotor[0] == smotor1[1])
{
pos1 = 100;
}
else if (smotor[0] == smotor1[2])
{
pos1 = 90;
}
return(pos1);
}
void rccvdchars()
{
char start = '<';
char ended = '>';
static byte index = 0;
char data;
static bool inProg = false;
int A =9;
while (Serial.available() > 0)
{
data = Serial.read();
if (inProg == true)
{
////debug.println("Start");
if ( data != ended)
{
rccvd[index] = data;
index++;
if ( index >= numchars)
{
index = numchars - 1;
}
}
else
{
////debug.println("data == ended");
rccvd[index] = '\0';
inProg = false;
index = 0;
newdata = true;
}
}
else if ( data == start)
{
////debug.println("data==start");
inProg = true;
}
}
}
void parsedata()
{
char *ptr;
ptr = strtok(temprccvd,";");
strcpy(motor,ptr);
ptr = strtok(NULL,";");
strcpy(smotor,ptr);
ptr = strtok(NULL,";");
strcpy(sw,ptr);
}
void debugdata()
{
//debug.print("Motor(NON):");
//debug.println(motor[0]);
//debug.print("Servo(NON):");
//debug.println(smotor[0]);
//delay(1000);
/*//debug.print("Switch:");
//debug.println(sw);*/
}
void movemotors()
{
if (motor[0] == motor1[0])
{
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
}
else if (motor[0] == motor1[1])
{
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
}
else
{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
}
}
int check()
{
int total,chk1=0,chk2=0;
if (motor[0] == motor1[0])
{
chk1 = chk1 + 1;
}
if (motor[0] == motor1[1])
{
chk1 = chk1 + 1;
}
if (motor[0] == motor1[2])
{
chk1 = chk1 + 1;
}
if (smotor[0] == smotor1[0])
{
chk2 = chk2 + 1;
}
if (smotor[0] == smotor1[1])
{
chk2 = chk2 + 1;
}
if (smotor[0] == smotor1[2])
{
chk2 = chk2+ 1;
}
//debug.print("TOTAL:");
total = chk1 + chk2;
//debug.println(total);
return (total);
}
TestingRemote_5.ino (2.54 KB)
DeviceTesting_5.ino (3.74 KB)

