Hi,
I’m currently on the project that getting data from the USB weather board which is controlled by Arduino.
Firstly, I have problem with serial1 then with the string function. Do i need any .h file in the same folder? I’m using library of Arduino 0021.
Please leave your advices. Much appreciated.
The following is the codes and message in detal.
A.
Source Code:
#include <SoftwareSerial.h>
//Created August 15 2006
//Heather Dewey-Hagborg
#include <ctype.h>
#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94
//#define Serial1
byte rx = 0;
byte tx = 1;
byte SWval;
String buffer = String(" "); // buffer to read data from the weather board
int ready = 0; // used to mark when we have a complete string to process
int val; // use to read a byte from the serial
unsigned long humidity=0, fahrenheit=0, celcius=0, SCPfahrenheit=0, presure=0;
void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
digitalWrite(tx,HIGH);
digitalWrite(13,HIGH); //turn on debugging LED
SWprint(‘h’); //debugging hello
SWprint(‘i’);
SWprint(10); //carriage return
Serial1.begin(9600); // starts the second hardware serial port at 9600 to communicate with the weather board
Serial.begin(9600); // starts the serial at 9600
pinMode(48, OUTPUT); // turn ON the board LED for diagnostics
digitalWrite(48, HIGH);
}
void SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx,LOW);
delayMicroseconds(bit9600Delay);
for (mask = 0x01; mask>0; mask <<= 1) {
if (data & mask){ // choose bit
digitalWrite(tx,HIGH); // send 1
}
else{
digitalWrite(tx,LOW); // send 0
}
delayMicroseconds(bit9600Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit9600Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit9600Delay);
for (int offset = 0; offset < 8; offset++) {
delayMicroseconds(bit9600Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit9600Delay);
delayMicroseconds(bit9600Delay);
return val;
}
}
void loop()
{
SWval = SWread();
SWprint(toupper(SWval));
while(Serial1.available() > 0) { // if data vailable from the weather board
val = Serial1.read(); // read it
if((val != ‘\n’)) { // if no end of line
buffer.append(char(val)); // add it to the buffer
}
else { // if end of line reached, readu to parse the buffer
ready = 1;
break;
}
}
if(ready == 1) { // parse the buffer
if(buffer.contains(“#”)) { // verify if it is aggod reading
// Serial.println(buffer); // just to see what we got
processReading(buffer.toCharArray()); // parse the buffer extracting the data
}
buffer = String(“”); // clean up the buffer for next reading
ready = 0;
}
}
// parse the buffer received extracting the data
void processReading(char packet) {
byte i;
char* endptr;
// start parsing
i = 0;
i++;
humidity = strtod(&packet*, &endptr); // next field: humidity*
while(packet[i++] != ‘,’); // next field: fahrenheit
fahrenheit = strtod(&packet, &endptr);
while(packet[i++] != ‘,’); // next field: celcius
celcius = strtod(&packet, &endptr);
while(packet[i++] != ‘,’); // next field: SCPfahrenheit
SCPfahrenheit = strtod(&packet, &endptr);
while(packet[i++] != ‘,’); // next field: presure
presure = strtod(&packet, &endptr);
while(packet[i++] != ‘,’); // next field: counter
while(packet[i++] != ‘$’); // next field: checksum
Serial.print("humidity: ");
Serial.print(humidity, DEC);
Serial.print(" fahrenheit: ");
Serial.print(fahrenheit, DEC);
Serial.print(" celcius: ");
Serial.print(celcius, DEC);
Serial.print(" SCPfahrenheit: ");
Serial.print(SCPfahrenheit, DEC);
Serial.print(" presure: ");
Serial.println(presure, DEC);
}
__________________________________________________________________________________
Error messages:
USB_weather_board:31: error: ‘Serial1’ was not declared in this scope
USB_weather_board.cpp: In function ‘void loop()’:
USB_weather_board:81: error: ‘Serial1’ was not declared in this scope
USB_weather_board:84: error: ‘class String’ has no member named ‘append’
USB_weather_board:92: error: ‘class String’ has no member named ‘contains’
USB_weather_board:94: error: no matching function for call to ‘String::toCharArray()’
C:\Users\Austin\Desktop\arduino-0021\hardware\arduino\cores\arduino/WString.h:80: note: candidates are: void String::toCharArray(char*, unsigned int)