At first, I tried to make whole code for an RC car and remote controller, but I realized that it's difficult for me since I have few ideas about C++. So, I changed a few things like turning on the LEDs with switches and servomotors with potentiometer. Here are my code.
Remote controller
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3);
int sw1=13;
int sw2=12;
void setup() {
BTSerial.begin(9600);
Serial.begin(9600);
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
}
void loop() {
int a = analogRead(A0);
int map_a=map(a,0,1023,0,255);
int state1=digitalRead(sw1);
int state2=digitalRead(sw2);
BTSerial.print('S');
BTSerial.print(map_a);
BTSerial.print(',');
BTSerial.print(state1);
BTSerial.print(',');
BTSerial.print(state2);
BTSerial.print('E');
delay(100);
}
< RC CAR >
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3);
byte buffer[255];
int bufferPosition;
void setup() {
BTSerial.begin(9600);
Serial.begin(9600);
bufferPosition=0;
}
void loop() {
{
if(BTSerial.available()>0){
unsigned char data = (unsigned char)BTSerial.read();
buffer[bufferPosition++]=data;
Serial.println(data);
delay(100);
}
}
}
When I checked the result from Serial.println(data)
from RC car code,
it was trash...
Can you instruct me what's wrong with this code?
unsigned char
?Serial.print()
very expects a 'regular'char
.