Pages: [1]   Go Down
Author Topic: Reading 2 characters of serial  (Read 1022 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I currently have 14 buttons connected to one arduino. When one is pressed, it sends the 2 character number of the button (00-13) to the other arduino via softwareSerial on the analog port.

The transmitter is working properly but i'm having trouble on the receiving side. I'm using this code:
Code:
#include <DigitalToggle.h>

#include <NewSoftSerial.h>


int serial = 0;

NewSoftSerial mySerial(A5, A4); // RX, TX

void setup(){

int pin = 0;

while(pin < 14){ pinMode(pin, OUTPUT); pin = pin + 1; }
mySerial.begin(9600);
Serial.begin(9600);

digitalWrite(1, HIGH);
digitalWrite(0, LOW);
}

void loop(){

if (mySerial.available() > 0) {
int no1 = mySerial.read() -48;
int no2 = mySerial.read() -48;

if (no1 == 1)
{
no1 = no1 +9;
serial = no1 + no2;
} else if (no1 == 0) {
serial = no2;
}

Serial.println(serial);

digitalToggle(serial);
delay(1000);

}

}



But all it sends to my PC is rubbish, often -49 or -39.

I think i need to do everything with a buffer but i don't know how...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25671
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Usual problem - see if there is at least one character available, then read both of them.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(pin < 14){ pinMode(pin, OUTPUT); pin = pin + 1; }
A while loop is typically used to loop an indeterminate number of times. A for loop is typically used to loop a defined number of times. This would make more sense as a for loop.

Code:
Serial.begin(9600);

digitalWrite(1, HIGH);
digitalWrite(0, LOW);
Why are you diddling with the Serial port pins?

Code:
if (mySerial.available() > 0) {
int no1 = mySerial.read() -48;
int no2 = mySerial.read() -48;
If there is at least one byte available, read them both. How's that working for you? Oh, never mind, you told us already.

Why not use:
Code:
int no1 = mySerial.read() - '0';
Much more obvious, don't you think?

Code:
if (no1 == 1)
{
no1 = no1 +9;
serial = no1 + no2;
} else if (no1 == 0) {
serial = no2;
}
Or
Code:
serial = no1 * 10 + no2;
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why are you diddling with the Serial port pins?
That's because i have LED's on them (with jumpers that i remove when programming) but i need to toggle the LED's for them to be in the correct state



Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Usual problem - see if there is at least one character available, then read both of them.

I already stated, i have no idea on how to read 2 characters over serial..
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Usual problem - see if there is at least one character available, then read both of them.

I already stated, i have no idea on how to read 2 characters over serial..
Wait until there is at least two character and read twice.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wait until there is at least two character and read twice.

Thanks, will try now...
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Allright, now i am using this code:
Code:
if (Serial.available() > 1) {
int no1 = Serial.read();
int no2 = Serial.read();

if (no1 == 1) // to clarify: This is to combine the two values, if it's 10 or more, it will make no1 10 and add no2 and if it's less than 10 it will just make serial no2. This helps with the digitalToggle.
{
no1 = no1 +9;
serial = no1 + no2;
} else if (no1 == 0) {
serial = no2;
}

Serial.println(serial);

digitalToggle(serial);
delay(1000);

}

in my loop.


I've uploaded the code but no matter what the arduino receives, it always responds with '0'.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int no1 = Serial.read();
int no2 = Serial.read();
Suppose you send '2', '3'. no1 will be 50, and no2 will be 51.

Code:
if (no1 == 1) // to clarify: This is to combine the two values, if it's 10 or more, it will make no1 10 and add no2 and if it's less than 10 it will just make serial no2. This helps with the digitalToggle.
{
no1 = no1 +9;
serial = no1 + no2;
} else if (no1 == 0) {
serial = no2;
}
So, if no1 is not 1, serial never gets assigned a value.

Are you reading these replies?
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's because when you type "10" into the serial monitor, you're receiving '1' and '0', not 1 and 0. If you want to get the actual value of the number that you typed in, subtract '0' from the character you read. This will convert '2' to 2, '7' to 7, etc.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are using pins A4 and A5 to communicate in UART style. Maybe it's an opportunity for you to use the same two pins and communicate via I2C? This way you can add more devices without losing more pins.

You are currently trying to communicate between the two Arduinos using an ASCII based protocol. To transfer the state of 14 buttons a binary one may be more efficient, you need just one byte then and you don't have to deal with decoding ASCII characters back to binary values again.

The sending part would be then (still in SoftwareSerial style):

Code:
mySerial.write(pinNumber);


the receiving part:

Code:
byte pin = 255;
if (mySerial.available()) {
  int c = mySerial.read();
  if (c >= 0) {
    pin = c;
  }
}

You can choose any other base (I used 0 as the number of the first pin), you just have to add the value on the sending side and subtract it on the receiving side.
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 156
It was all digital
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Others may search the forum for answers

Code:
void setup(){
  Serial.begin(9600);
}

void loop(){
  if (Serial.available() > 0) {
    Serial.println (serialNumber());
  }
}

long serialNumber(){             //any '-' will render the number negativ
  long number = 0;
  int sign = 1;
  while (Serial.available() > 0){
    char inc = Serial.read();
    if (inc == 45) sign = -1; // negativ number
    inc = inc - '0';
    if ((inc >= 0) and (inc <= 9)){
      number = number * 10 + inc;
    }
    delay(10);
  }
  return sign*number;
}

-Fletcher
Logged

Pages: [1]   Go Up
Jump to: