I am working on a project that has two separate Arduinos. I have an UNO paired with an IMU (ADXL345/ITG3200) that is running the freeIMU library and a Due that is connected to two quadrature encoders and adafruit's SD shield. I have both of the Arduinos working separately but would like to take the Pitch and Roll information from the UNO and pass it to the Due to be able to store Time, Encoder1, Encoder2, Pitch, and Roll as often as possible.
I have not been able to accomplish this and am struggling to find the best method to pass the pitch roll information from the DUE to the UNO. The UNO is using it's I2C communications to interact with the IMU and the DUE uses the Serial function as part of the SD library.
My current strategy is to use the SoftwareSerial library on the UNO to communicate with one of the 3 Serial ports on the DUE but I have been unsuccessful to get anything but gibberish out of the serial com on the DUE.
In this scenario I have the UNO reading in the rotation of an encoder and writing the number of pulses to the DUE over a serial connection.
SENDER
//UNO reading encoder and writing number of pulses to arduino
const int clkPinA = 2;
const int dirPinA = 4;
volatile int encoderACount = 0;
volatile boolean changeFlag = false;
void setup() {
Serial.begin(9600);
Serial.println("Encoder Reading");
pinMode(clkPinA, INPUT);
pinMode(dirPinA, INPUT);
attachInterrupt(0, encoderIntA, RISING);
}
void loop() {
if (changeFlag) {
Serial.write(encoderAcount);
changeFlag = false;
}
}
//Encoder Function
void encoderIntA() {
if (digitalRead(dirPinA) == HIGH)
encoderACount++;
else
encoderACount--;
changeFlag = true;
}
RECEIVER
#include <SPI.h>
#include <SD.h>
const int chipSelect = 8;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
Serial1.begin(9600);
}
void loop()
{
// make a string for assembling the data to log:
long encoderAcount;
encoderAcount = Serial1.read();
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(encoderAcount);
dataFile.close();
// print to the serial port too:
Serial.println(encoderAcount);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
This is the simplest case I could think of, please let me know your thoughts.
Sorry to post again so quickly, but with the code I have posted above the serial monitor on the DUE shows "-1" repeatedly until I spin the encoder connected to the UNO then it displays seemingly random values from 10 to 53 then after the spinning stops the serial monitor redisplays "-1" I have copied the values as they settle back to -1.
O's are displayed before spinning the encoder wheel, the random values from 10-53 while the encoder is spinning, and 13 after I stop spinning the encoder.
Looks like carriage-return, linefeed to me.
So do this mean that the DUE (receiver) is receiving the beginning and end of each transmission, but not the data I am hoping to send?
So do this mean that the DUE (receiver) is receiving the beginning and end of each transmission, but not the data I am hoping to send?
Why don't you go look at an ASCII table and find out what the rest of those numbers mean. It has already been pointed out that 10 and 13 are the carriage return/line feed combination. I'm willing to bet that you'll find meaning in those other numbers, too.
Thank you to you both! I got it working and the values are coming through correctly. I will mark this as solved.
The code I ended going with was :
RECEIVER:
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
void setup ()
{
Serial.begin (57600);
Serial1.begin (57600);
Serial.println ("Starting ...");
} // end of setup
void processNumber (const long n)
{
Serial.println (n);
} // end of processNumber
void processInput ()
{
static long receivedNumber = 0;
static boolean negative = false;
byte c = Serial1.read ();
switch (c)
{
case endOfNumberDelimiter:
if (negative)
processNumber (- receivedNumber);
else
processNumber (receivedNumber);
// fall through to start a new number
case startOfNumberDelimiter:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
} // end of switch
} // end of processInput
void loop ()
{
if (Serial1.available ())
processInput ();
// do other stuff here
} // end of loop