0

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?

4
  • Why the delay(100) in the RC CAR part? And what happens when the buffer is full? Seems incomplete. BW: Please use a code tag. Dec 8, 2015 at 16:08
  • i was expecting this result S map_a, state1, state2, E S map_a, state1, state2, E S map_a, state1, state2, E .... after i receive this result, i would like to proceed more...and sorry, i didn't know how to use a code tag,,it is first time to ask a question here...
    – YOON
    Dec 8, 2015 at 16:24
  • Please post what you actually get in the serial monitor - one man's "trash" is another man's really-easy-answer. Dec 9, 2015 at 0:59
  • Wait, why are you casting to unsigned char ? Serial.print() very expects a 'regular' char. Dec 9, 2015 at 1:01

1 Answer 1

1

There are some fundamental problems in the code - bufferPosition never stops increasing (until it reaches 32768) and so buffer will overflow. However, neither of these two problems are causing the 'trash', as you are outputting the result of reading the serial, not anything from the buffer.

The problem lies in the casting of the result from BTSerial.read() to unsigned char.

Serial.print() does not like this. Actually it doesn't care, but it won't give you what you expect. It just prints out the value stored - so 'a', which is 97, will come out as '97' on the serial monitor. Serial.print() only outputs ASCII characters when you give it a char or char[].

1
  • thank you so much. I just started to study C language. So i've got confused with char or int or unsigned char. I'm trying to learn those things from some examples..thanks again. I will try to fix some codes you mentioned and post again later :)
    – YOON
    Dec 9, 2015 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.