I've got a strange problem. I had a sketch working perfectly until I transferred it to a soldered breadboard then it stopped working. It seems that the particular sketch won't accept serial input from the computer.
The project involves a diecemilla in slave configuration connected to a sensor. The master is a mega or in this case a desktop computer. The master would send a single character to the slave and if it was the slaves address it would send back data from the sensor.
Here is the original code
if(Serial.find('a')){ //Outputs data only if called for
Serial.println(data);
}
It didn't work, but this sketch works. Even if the arduino isn't removed from the circuit.
void setup() {
Serial.begin(19200); // opens serial port, sets data rate to 9600 bps
}
void loop() {
if(Serial.find('a')){ //Outputs data only if called for
Serial.println("asdf");
}
}
I don't get it
I'm sure the wiring is okay, with the if condition removed the arduino transmits accurate data to the serial monitor.
I've put a Serial.println("fdsa") in front of the if statement and it displays in the serial monitor so I know the code isn't being passed over. There is an interrupt that I thought that was the problem but it was working before.
I tried adding Serial.read to detect data transmitted from the master and it doesn't work in the broken sketch. But when added to the simple sketch it works.
Anybody encountered this before and if so how do I fix it?
I'm using an ATmega 328 and connected it via USB. Proper connections was the first thing I checked and those seem to be fine since I can transmit data out just fine. Perhaps soldering is finickier than I realize.
Here is the complete code, It's a bit long so I hesitated to post it
////////////////////////////////////////////////////////////////////////////////////////////////////////
// May 2015
// Author: Juan Jose Chong <juan.chong@analog.com>
////////////////////////////////////////////////////////////////////////////////////////////////////////
// ADIS16448.ino
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This Arduino project interfaces with an ADIS16448 using SPI and the accompanying C++ libraries,
// reads IMU data in LSBs, scales the data, and outputs measurements to a serial debug terminal (putty) via
// the onboard USB serial port.
//
// This project has been tested on an Arduino Duemilanove and Uno, but should be compatible with any other
// 8-Bit Arduino embedded platform.
//
// This example is free software. You can redistribute it and/or modify it
// under the terms of the GNU Lesser Public License as published by the Free Software
// Foundation, either version 3 of the License, or any later version.
//
// This example is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License along with
// this example. If not, see <http://www.gnu.org/licenses/>.
//
// Pinout for Arduino Uno/Diecimila/Duemilanove
// Gray = RST = 4
// Purple = SCLK = 13
// Blue = CS = 7
// Green = DOUT(MISO) = 12
// Yellow = DIN(MOSI) = 11
// Brown = GND
// Red = VCC [3.3V ONLY]
// White = DR = 2
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <ADIS16448.h>
#include <SPI.h>
// Initialize Variables
// Accelerometer
int AX = 0;
int AY = 0;
int AZ = 0;
float AXS = 0;
float AYS = 0;
float AZS = 0;
// Gyro
int GX = 0;
int GY = 0;
int GZ = 0;
float GXS = 0;
float GYS = 0;
float GZS = 0;
// Magnetometer
int MX = 0;
int MY = 0;
int MZ = 0;
float MXS = 0;
float MYS = 0;
float MZS = 0;
// Barometer
int BAR = 0;
float BARS = 0;
// Control Registers
int MSC = 0;
int SENS = 0;
int SMPL = 0;
// Temperature
// Data Ready Flag
boolean validData = false;
// Call ADIS16448 Class
ADIS16448 IMU(7,2,4); //ChipSelect,DataReady,Reset Pin Assignments
int control=3; //set control pin for RS485 communication
//turn high to transmit, low to recieve
char address[]="A";
void setup()
{
Serial.begin(19200); // Initialize serial output via USB
Serial.setTimeout(500);
IMU.configSPI(); // Configure SPI communication
pinMode(control,OUTPUT);
digitalWrite(control,LOW); // set RS485 to recieve
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
delay(100); // Give the part time to start up
//gyroscope callibration for 80 seconds, do not move during this time
/*IMU.regWrite(SENS_AVG,0x101);
delay(20);
IMU.regWrite(SMPL_PRD,0xF01); // Set Decimation on IMU
delay(85000); //Waits for gyroscopes to auto calibrate
IMU.regWrite(GLOB_CMD,0x0001);
delay(100); //give time for gyroscope values to be written to memory
*/IMU.regWrite(MSC_CTRL,0x06); // Enable Data Ready on IMU
delay(20);
IMU.regWrite(SENS_AVG,0x104); // Set Digital Filter on IMU to lowest level for maximum resolution
delay(20);
IMU.regWrite(SMPL_PRD,0x601); // Set Decimation on IMU ~136 SPS
delay(20);
attachInterrupt(0, setDRFlag, RISING); // Attach interrupt to pin 2. Trigger on the rising edge
}
// Function used to read register values via SPI and load them into variables in LSBs
void grabData()
{
// Put all the Data Registers you want to read here
//IMU.configSPI(); // Configure SPI before the read
GX = IMU.regRead(XGYRO_OUT);
GY = IMU.regRead(YGYRO_OUT);
GZ = IMU.regRead(ZGYRO_OUT);
AX = IMU.regRead(XACCL_OUT);
AY = IMU.regRead(YACCL_OUT);
AZ = IMU.regRead(ZACCL_OUT);
MX = IMU.regRead(XMAGN_OUT);
MY = IMU.regRead(YMAGN_OUT);
MZ = IMU.regRead(ZMAGN_OUT);
}
// Function used to scale all acquired data (scaling functions are included in ADIS16448.cpp)
void scaleData()
{
GXS = IMU.gyroScale(GX)/4; //Scale X Gyro
GYS = IMU.gyroScale(GY)/4; //Scale Y Gyro
GZS = IMU.gyroScale(GZ)/4; //Scale Z Gyro
AXS = IMU.accelScale(AX); //Scale X Accel
AYS = IMU.accelScale(AY); //Scale Y Accel
AZS = IMU.accelScale(AZ); //Scale Z Accel
MXS = IMU.magnetometerScale(MX); //Scale X Magnetometer
MYS = IMU.magnetometerScale(MY); //Scale Y Magnetometer
MZS = IMU.magnetometerScale(MZ); //Scale Z Magnetometer
}
// Data Ready Interrupt Routine
void setDRFlag()
{
validData = !validData;
}
// Main loop. Scale and display registers read using the interrupt
void loop()
{
if (validData) // If data present in the ADIS16448 registers is valid...
{
grabData(); // Grab data from the IMU
scaleData(); // Scale data acquired from the IMU
if(Serial.find('a')){ //Outputs data only if called for
digitalWrite(control,HIGH);
delay(20);
//Print scaled gyro data
Serial.println(GXS);
Serial.println(GYS);
Serial.println(GZS);
//Print scaled accel data
Serial.println(AXS);
Serial.println(AYS);
Serial.println(AZS);
//Print scaled magnetometer data
Serial.println(MXS);
Serial.println(MYS);
Serial.println(MZS);
Serial.flush(); //send out all data before closing communication module
digitalWrite(control,LOW);
}
}
validData = !validData;
}