Hi all.
The project I'm working on is about controlling the speed of a motor connected to a Arduino working as Rx, through a potentiometer on a Arduino working as Tx, all this via the UART port.
The variation of the potentiometer sends a value between 0 and 100, and if this value exceeds 50, an audible alarm is activated.
The problem arises in the arduino Rx, since the value it receives messes it up, for example:
In the ideal world Arduino Tx sends a 20 and Arduino Rx receives a 20
But in reality it receives: 20, then 2, then 20, then 2, then 20, and so on.
At the moment I can't think of how to solve this in the serial reading.
I would appreciate any help with this issue.
Thanks.
My Arduino TX Code:
#define POT A0
long int potentiometer = 0;
void setup() {
Serial.begin(9600);
analogReference(DEFAULT);
}
void loop()
{
potentiometer = analogRead(POT);
int scaling = map(potentiometer, 0, 1023, 0, 100);
Serial.print(scaling);
delay(1000);
}
And my Arduino RX Code:
#define BUZZER 2
#define MOTOR 3
const unsigned int LONG_MAX_MSG = 3;
int number = 0;
void setup() {
Serial.begin(9600);
pinMode(BUZZER, OUTPUT);
pinMode(MOTOR, OUTPUT);
}
void loop() {
while (Serial.available() > 0) {
static char message[LONG_MAX_MSG];
static unsigned int pos_msg = 0;
char inByte = Serial.read();
//Checks if the message it is not a new line
if (inByte != '\n' && (pos_msg < LONG_MAX_MSG - 1)) {
message[pos_msg] = inByte;
pos_msg++;
delay(100);
}
//Mensaje received
else {
//Agrega un carácter null al string
message[pos_msg] = '\0';
//char to int
number = atoi(message);
Serial.println(number);
//Reinicia la posición del mensaje para la siguiente transmisión
pos_msg = 0;
delay(100);
//Sound alarm
if (number >= 50) {
tone(BUZZER, 250);
analogWrite(MOTOR, number);
delay(100);
}
else{
noTone(BUZZER);
analogWrite(MOTOR, number);
}
delay(100);
}
}
}
You're looking for a '\n' in the input but you never sent one. Try changing: Serial.print(scaling);
in your sending sketch to: Serial.println(scaling);
It's just a exercise in a postgraduate course.
I'm too proud to drop the exercise if it doesn't work at 100%, and also, this kind of issue teaches a lot.
Your input buffer only has room for two characters.
Back when you were sending "2020202020202020" half the time you would read "20" and throw away a '2' because it wouldn't fit in the buffer. The other half the time you'd read "02" and throw away a '0' because it wouldn't fit in the buffer.
Now that you are sending "20\n20\n20\n" half the time you are reading "20" and half the time you are reading "\n".
I would make the input buffer bigger so you can fit at least "100\n\0" before you run out of space.
You also need to earn how to do Serial receive WITHOUT long delay() calls. That is doing nothing but wasting millions of CPU cycles that could be doing useful work. You should read this page, and adopt all of the methods presented therein.
This is a rather special case, since you are only sending a single value, but you can simply send the binary value as a single byte, and not have to worry about line endings or ascii conversions.