Back:
#include <SPI.h>
#include <ADXL362.h>
#include <avr/io.h>
#include <avr/interrupt.h>
ADXL362 accel; //accelerometer object
long Duration,inches; //for PW prox info
const int slaveSelectPin = 10; //accelerometer pin
int accel_old, accel_new; //for accelerometer data comparison
bool leftBlink, rightBlink; //turn signal variables
int XValue, YValue, ZValue, Temperature;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //baud rate is 9600
pinMode(2, OUTPUT); //controls sleep mode of proximity sensor
pinMode(3, OUTPUT); //controls the brake ttl relay
digitalWrite(3, LOW); //brake lights should be off when loading
pinMode(4, OUTPUT); //555 timer for ttl relays, left blinker
digitalWrite(4, HIGH); //Keep high so blinker doesn't blink
pinMode(5, OUTPUT); //555 timer for ttl realys, right blinker
digitalWrite(5, HIGH); //Keep high so blinker doesn't blink
pinMode(7, INPUT); //reads PW info from proximity sensor
accel_old, accel_new = 0; //no acceleration data yet
leftBlink, rightBlink = false; //no blinkers should be on yet
noInterrupts();
//setup timer interrupt
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// set compare match register to desired timer count:
OCR1A = 15624;
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
interrupts();
//Setup accelerometer
pinMode(slaveSelectPin, OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE0); //CPHA = CPOL = 0 MODE = 0
delay(1000);
// Activity threshold
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write
SPI.transfer(0x20); // Reg 20
SPI.transfer(0xFA); // Reg 20 = dec 250
SPI.transfer(0x00); // Reg 21 = 0x00
digitalWrite(slaveSelectPin, HIGH);
delay(100);
// Inactivity threshold, and inactivity timer
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write
SPI.transfer(0x23); // Reg 23
SPI.transfer(0x96); // Reg 23 = dec 150
SPI.transfer(0x00); // Reg 24 = 0x00
SPI.transfer(0x1E); // Reg 25 = dec 30
digitalWrite(slaveSelectPin, HIGH);
delay(100);
// motion detection, activity/inactivity detection
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write
SPI.transfer(0x27); // Reg 27
SPI.transfer(0x3F);
digitalWrite(slaveSelectPin, HIGH);
delay(100);
// map awake bit to Int2
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write
SPI.transfer(0x2B); // Reg 2B
SPI.transfer(0x49);
digitalWrite(slaveSelectPin, HIGH);
delay(100);
// begin measurement
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write
SPI.transfer(0x2D); // Reg 2D
SPI.transfer(0x0A); // begin meanusre
digitalWrite(slaveSelectPin, HIGH);
delay(100);
delay(500); //arbitrary delay to allow hardware to power up (probably not needed)
}
void loop() {
// put your main code here, to run repeatedly:
char getData = ' '; //character for serial data input
if(Serial.available()) //is there something being sent to serial?
{
getData = Serial.read(); //yes, so read it
}
accel.readXYZTData(XValue, YValue, ZValue, Temperature); //read the z-acceleration from the accelerometer
accel_old = accel_new;
accel_new = ZValue;
int change = accel_old - accel_new ;
if(abs(change) <= 5) //is the bicycle stopped?
{
digitalWrite(2,HIGH); //no, so allow the prox sensor to read data
delay(1); //delay for a very short time so prox sensor can turn on
}
Duration = pulseIn(7, HIGH); //Read the duration of the pulse coming from the prox sensor in microseconds
inches = Duration / 147; //target is one inch farther away per 147 microseconds
if(inches <= 48) //is the target within 4 feet?
{
Serial.print('p'); //yes, so send appropriate character wirelessly to the front
delay(250); //delay for a bit so the other microcontroller can recieve wireless signal and possibly send out it's own
}
digitalWrite(2,LOW); //turn off prox sensor until it is needed again
//Rider is experiencing 50 mg's of force forward, meaning he is braking
if(accel_new > 50)
{
digitalWrite(3, HIGH); //turn on brake lights when braking
}
else
{
digitalWrite(3, LOW); //turn off lights when not braking
}
/*
switch-case for input from serial. This takes care of all of the blinker systems.
Character meanings:
'l' means the left blinker switch was pushed
'r' means the right blinker switch was pushed
'z' means both blinkers should be off
*/
switch (getData) {
case 'l':
leftBlink = true;
break;
case 'r':
rightBlink = true;
break;
case 'z':
leftBlink, rightBlink = false;
break;
default:
break;
}
}
//This interrupt routine will pulse the 555 timer to turn off and on the respective blinker every second
ISR(TIMER1_COMPA_vect)
{
if(leftBlink)
{
digitalWrite(4,LOW);
digitalWrite(4,HIGH);
}
if(rightBlink)
{
digitalWrite(5,LOW);
digitalWrite(5,HIGH);
}
}
I'm not receiving on either XBee, but I can see the serial monitor printing out the right characters. Are there any settings I can change in X-CTU, such as the baud rate or the amount of packets re-sent, to overcome this problem? Or is this merely a code issue?
I do have the power connected to the 3.3V pin of the arduino, and I'm using 470/220 Ohm resistors as voltage dividers for the output pins from the arduino to the XBee, so I don't think voltage is an issue here.
Also, I have tried disconnecting the arduino from the computer. I powered it via the USB-A to USB-B cord -> 13000 mAh 5V DC rechargeable battery, so there should be no interference with the serial data lines. Pin 2 (RX) of the XBee is connected to pin D0 (RX) of the arduino, and pin 3 (TX) of the XBee is connected to pin D1 (TX) of the arduino. Still nothing.
Thanks for your help.