Speed and recognition

Hello All,

I am new to the forum and currently seeking advice/tweak suggestions on my project. I am building a proximity sensor mounted to a moving object. I'd like to have the Arduino and Xbee's communicate instantaneously at 5 - 10mph . I currently have the code written for PM not Analog and wonder if that's the key element in the speed factor. The current speed sweet spot is around 1.5 seconds.

As I am also new to coding, I am a little lost when coding to Analog.

Here are the Items being used:

2x - http://store.arduino.cc/product/A000065
2x - http://store.arduino.cc/product/A000066
2x - https://www.sparkfun.com/products/8665
1x - PING))) Ultrasonic Distance Sensor - Parallax
1x - MB7369 HRXL-MaxSonar-WRM – MaxBotix (as a backup)

Here's my code for both the receiver and the sensor:

// Arduino_1_leds

int LED0 = 12;
int LED1 = 11;
int LED2 = 10;
int LED3 = 9;
int LED4 = 8;
int LED5 = 7;
int LED6 = 6;

int blink_state = 0;
char val = ' ';

void setup(){

Serial.begin(9600);

pinMode(LED0,OUTPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(LED5,OUTPUT);
pinMode(LED6,OUTPUT);

delay(2000);
//light test to make sure all LEDs are fuctioning
LED_on();
delay(3000);
LED_off();
delay(100);
LED_on();
delay(250);
LED_off();
Serial.print("LED test done, starting to reading sensor output ");
}

void loop(){

if (Serial.available() > 0) {
val = Serial.read(); //query output from sensor
Serial.print("value from sensor: ");
Serial.println(val);
//Serial.println (" case");

if (val == '0'){
Serial.println(" case 0 ");
case0();
}
if (val == '1'){
Serial.println(" case 1 ");
case1();
}
if (val == '2'){
Serial.println(" case 2 ");
case2();
}
if (val == '3'){
Serial.println(" case 3 ");
case3();
}
if (val == '4'){
Serial.println(" case 4 ");
case4();
}
if (val == '5'){
Serial.println(" case 5 ");
case5();
}
if (val == '6'){
Serial.println(" case 6 ");
case6();
}
if (val == '7'){
Serial.println(" case 7 ");
case7();
}
if (val == '8'){
Serial.println(" case 8 ");
case8();
}
Serial.println("pause .35 seconds");
delay(350); // seconds between readings

}
else{
Serial.println("Serial input not working...probably Janneh's fault");
Serial.println("pause .35 seconds");
delay(350); //350 milliseconds yields no delay with transmission set at 1000
}
}

void case0(){
digitalWrite(LED0,LOW);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case1(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case2(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case3(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case4(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case5(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case6(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,LOW);
blink_state = 0;
}

void case7(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
blink_state = 0;
}

void case8(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,LOW);
digitalWrite(LED6,HIGH);
//delay(500);
//digitalWrite(LED0,LOW);
//digitalWrite(LED1,LOW);
//digitalWrite(LED2,LOW);
//digitalWrite(LED3,LOW);
//digitalWrite(LED4,LOW);
//digitalWrite(LED5,LOW);
//digitalWrite(LED6,LOW);
//delay(500);
}

void LED_on(){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
}

void LED_off(){
digitalWrite(LED0,LOW);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
}

// Arduino_2_sensor

// this constant won't change. It's the pin number

const int pingPin = 7;

void setup(){
// initialize serial communication:
Serial.begin(9600);
Serial.println('0'); //turn on the LED
delay(1000);
Serial.println('1');//turn off the LED
delay(1000);
Serial.println('2'); //turn on the LED
delay(1000);
Serial.println('3');//turn off the LED
delay(1000);
Serial.println('4'); //turn on the LED
delay(1000);
Serial.println('5');//turn off the LED
delay(1000);
Serial.println('6'); //turn on the LED
delay(1000);
Serial.println('7');//turn off the LED
delay(1000);
Serial.println('8');//turn off the LED
delay(1000);
Serial.println('0'); //turn on the LED
delay(1000);
}

void loop(){
// establish variables for duration of the ping and the value
long duration, inches, cm, value;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT) ;
digitalWrite(pingPin, LOW) ; // init sensor to ensure clean HIGH pulse
delayMicroseconds(2) ;
digitalWrite(pingPin, HIGH) ; // make the sensor send a pulse
delayMicroseconds(5) ;
digitalWrite(pingPin, LOW) ; // Set LOW again

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT) ; // Get ready to capture the duration of the resulting pulse
duration = pulseIn(pingPin, HIGH) ;

if (duration == 0) {
//DEBUG
//Serial.println ("No Pulse received from the sensor") ;
}

else{

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
value = inches;
//DEBUG
//Serial.print ("Distance : ") ;
//Serial.print(value);
//Serial.println (" inches");
if ((value <=140) && (value >= 1)){
if((value > 43)){
Serial.println('0');
}
else if ((value <=39) && (value > 35)){
Serial.println('1');
}
else if ((value <=35) && (value > 31)){
Serial.println('2');
}
else if ((value <=31) && (value > 27)){
Serial.println('3');
}
else if ((value <=27) && (value > 23)){
Serial.println('4');
}
else if ((value <=23) && (value > 19)){
Serial.println('5');
}
else if ((value <=19) && (value > 15)){
Serial.println('6');
}
else if ((value <=15) && (value > 11)){
Serial.println('7');
}
else{
Serial.println('8');
}
}

delay(1500); //seems to be working better than 1000, need to find sweet spot
}

}

long Convert_Time_Space(const long fnDuration )
{
// This function could be more precise by using floats
// and taking into account temperature and humidity
// I used 29 microseconds per cm.
return fnDuration / 29 / 2 ;
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

I am not sure i fully understand this question.

What is it you are trying to communicate between the device using xbees

how are you measuring the speed to get your 5-10 MPH window

what is the sweet spot that occurs in 1.5 seconds ?

Analogue/digital it is all the same in programming world. since the ADC takes your analogue signal and turns it into a digital representation anyway.

Can you try and re-explain what it is you are trying to do

Cheers Pete.

Suggest you get rid of the delays at the receiver in the serial port handling code. They complete defeat your objective of fast communication. You should process the input as soon as it is available.

Your processing of the received characters is extremely repetitive and looks as if it could be cut down to a single function taking a mask of LED states as an argument. The mask could easily be defined by a const byte array indexed by the number received over the serial port.

You seem to be writing serial trace messages to the same serial port you are receiving data on. Having more that one device writing to the same end of a serial connection is generally regarded as a bad idea since the two devices with be fighting against each other.

Because you are calling println on the sending side instead of print(), you are sending three times as much serial data as you need.

Throttling the sender to avoid exceeding the throughput of the serial line is good, but doing that using a 1.5 second delay seems excessive. If you want minimum latency it would be better to send the next value as soon as the serial transmit buffer is clear. Even better would be to only send an update if the current value changes, or some time has passed since the previous transmission.

If you are feeling adventurous you can also adjust the size of the serial receive buffer in the support classes. Something i have done a few times when i had large bursts of data with time to read and clear the buffer between bursts.

Cheers Pete.