Inexplicable serial problem
hey, guys, i'm faced problem can not seem take care of - or find what's causing it.
basically there's 1 arduino sending serially string !a18 , other arduino (this one) receiving it.
here's second arduino's code:
here's expected output:
and here's actual output:
this think happening:
1. arduino receives string , follows code end.
2. arduino receives string , not follow code end.
3. arduino receives string , follows code end.
4. arduino receives string , not follow code end.
5. arduino receives string , follows code end.
6. etc.
so, can see problem? :/
oh, , happens 9600 baud rate.
i read in thread this:
so thought "hey, why hell not?" , turned baud rate up.
does higher baud rate offer more problems lower baud rate? master arduino , several slave arduinos supposed talk serially @ distance of approximately 300m (through rs485)...
thank you!
basically there's 1 arduino sending serially string !a18 , other arduino (this one) receiving it.
here's second arduino's code:
code: [select]
/*
* temperature , humidity server
* version 1.0a
* pedro tome'
*
* how works:
* basically, 1 master arduino receives data
* various slave arduinos have temperature
* , humidity sensors, uploaded to
* pachube.com account.
*
* code for:
* slave :: temperature , humidity
*/
/* temperature sensor's pre-commands */
#include <onewire.h>
onewire ds(7); // temperature sensor's pin 2 (output) connected arduino's pin d7
/* humidity sensor's pre-commands */
int humsensor_pin = 0; // humidity sensor's pin 2 (output) connected arduino's pin a0
float max_voltage = 3.27; // humidity sensor's output when: 100% humidity @ 0ºc
float zeropercentvoltage = 0.8; // humidity sensor's output when: 0% humidity
/* other pre-comands */
int statusled = 2; // status led connected pin d2 show whether sketch running or not
int rx_pin = 0; // serial input pin d0
int tx_pin = 1; // serial output pin d1
char selfaddressletter = 'a'; // letter 'a'. every arduino has different letter address (duh!).
void setup() {
serial.begin(115200);
pinmode(statusled, output);
pinmode(rx_pin, input);
pinmode(tx_pin, output);
}
void loop() {
serial.flush();
boolean stringvalid = false; // initialize variable holds validity status of received strings
byte receivedaddressletter[1] = {'/0'}; // initalize variable contains received address letter
char receivedaddresscrc[2]; // initalize array contains received address crc, 2 characters long
memset(receivedaddresscrc, '/0', 2); // set addresscrc array null characters
byte startbyte = '/0'; // initialize startbyte, first relevant character of received messages
unsigned long starttime = millis(); // sets starttime = now
int timeout = 1000; // sets timout 1 second
while (millis() - starttime < timeout && startbyte != '!') {
startbyte = serial.read();
}
if (startbyte == '!') { // if arduino has received serial message started character '!'
unsigned long starttime = millis(); // set starttime = now
int timeout = 1000; // set timout 1 second
while (millis() - starttime < timeout && serial.available() < 3) {
; // wait buffer filled 3 characters while doing nothing
}
/* read received address , crc */
receivedaddressletter[0] = serial.read();
for (int i=0; < 2; i++) {
receivedaddresscrc[i] = serial.read();
}
/* check validity of received content generating , comparing crcs */
unsigned int intreceivedaddresscrc;
intreceivedaddresscrc = htoi(receivedaddresscrc[0])*16+htoi(receivedaddresscrc[1]); // convert crc array int
if (intreceivedaddresscrc == generatecrc8(receivedaddressletter, 1)) {
stringvalid = true;
serial.println("the received string valid.");
}
else {
stringvalid = false;
serial.println("the received string not valid.");
}
/* check if receivedaddressletter matches arduino's selfaddressletter
and, if so, collect data sensors , send master */
if (stringvalid == true && receivedaddressletter[0] == selfaddressletter) {
/*
* temperature sensor code :: start
*/
int highbyte, lowbyte, treading, signbit, tc_100;
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//serial.println("no more addresses.");
ds.reset_search();
delay(250);
return;
}
if ( onewire::crc8( addr, 7) != addr[7]) {
serial.println("crc not valid!");
return;
}
if ( addr[0] != 0x28) {
serial.println("device not ds18b20 family device.");
return;
}
// dallastemperature library can work you!
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, parasite power on @ end
delay(900); // maybe 750ms enough, maybe not
// might ds.depower() here, reset take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xbe); // read scratchpad
for ( = 0; < 9; i++) { // need 9 bytes
data[i] = ds.read();
}
// converting hex readable data...
lowbyte = data[0];
highbyte = data[1];
treading = (highbyte << 8) + lowbyte;
signbit = treading & 0x8000; // test sig bit
if (signbit) // if negative
{
treading = (treading ^ 0xffff) + 1; // 2's comp
}
tc_100 = (6 * treading) + treading / 4; // multiply (100 * 0.0625) or 6.25
float floated_tc_100 = tc_100; // floats can't divide ints, convert int float
float ctemperature = floated_tc_100 / 100;
if (signbit) // if it's negative
{ctemperature = -1 * ctemperature;}
//serial.print("tc_100: "); serial.println(tc_100); // make sure ctemperature correct
serial.print("temperature (celsius): "); serial.println(ctemperature);
/*
* temperature sensor code :: end
*/
//--------------------------------------------------------------------------------------
/*
* humidity sensor code :: start
*/
float humsensor_rawread = analogread(humsensor_pin);
//serial.print("raw humidity value: "); serial.println(humsensor_rawread);
max_voltage = (3.27-(0.006706*ctemperature)); // max voltage value drops 0.006705882 volts each ºc on 0ºc
float rh = ((((humsensor_rawread/1023)*5)-zeropercentvoltage)/max_voltage)*100;
serial.print("relative humidity (%): "); serial.println(rh);
/*
* humidity sensor code :: end
*/
serial.println("loop end -------------------");
serial.println();
} //end if (stringvalid == true && receivedaddressletter[0] == selfaddressletter)
} //end if (startbyte == '!')
} //end void loop()
here's expected output:
code: [select]
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 84.38
loop end -------------------
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 83.76
loop end -------------------
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 83.92
loop end -------------------
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 83.61
loop end -------------------
and here's actual output:
code: [select]
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 84.38
loop end -------------------
the received string valid.
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 83.76
loop end -------------------
the received string valid.
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 83.92
loop end -------------------
the received string valid.
the received string valid.
temperature (celsius): 16.62
relative humidity (%): 83.61
loop end -------------------
this think happening:
1. arduino receives string , follows code end.
2. arduino receives string , not follow code end.
3. arduino receives string , follows code end.
4. arduino receives string , not follow code end.
5. arduino receives string , follows code end.
6. etc.
so, can see problem? :/
oh, , happens 9600 baud rate.
i read in thread this:
quote
code: [select]serial.begin(9600);
this limiting factor. processing , arduino can talk faster this. try higher speed, 115200
so thought "hey, why hell not?" , turned baud rate up.

does higher baud rate offer more problems lower baud rate? master arduino , several slave arduinos supposed talk serially @ distance of approximately 300m (through rs485)...
thank you!
quote
does higher baud rate offer more problems lower baud rate?
it can more prone line noise, , corruption due cable capacitance.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Inexplicable serial problem
arduino
Comments
Post a Comment