Proximity sensor....LED's are not lighting

Hello,

I’m having a small challenge identifying the reason for my LED’s not lighting.

The Xbee’s are communicating because the RX(receiving) light becomes lit once the sensor sends a ping from the alternate Arduino.
The problem for me is having the board acknowledge the LED’s.

I’ve tried other entry level Arduino examples that involved LED’s and they’ve worked. I’d love to get this example completed so that I can learn a bit more.

Below is a link to Dropbox of my current setup.
https://www.dropbox.com/sh/weg7hs2szkuz … tbL7VogYca

These are the components:

2 x XBee 802.15.4 OnChip
2 x Arduino Uno
5 x 5 mm yellow LED
2 x 5 mm Red LED
1 x Breadboard
1 x HRXL-MaxSonar

(deleted)

Thanks for the heads up! I’ve attached both diagrams and included the code below for both boards

  1. The Sensor
  2. The LED’s

Here’s the code…

// 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;

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);
Serial.print(“s”);
}

int val = 0;
int count = 0;

void loop(){

val = Serial.read();
LED_level(val-48);

if (blink_state == 1){
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
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);
}

delay(5000);
}

void LED_level(int value){
switch (value){
case 0:
digitalWrite(LED0,LOW);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 1:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 2:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 3:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 4:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 5:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,LOW);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 6:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,LOW);
blink_state = 0;
break;
case 7:
digitalWrite(LED0,HIGH);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
digitalWrite(LED6,HIGH);
blink_state = 0;
break;
case 8:
blink_state = 1;
}
}

// Arduino_2_sensor

int sensor = 0;
int value = 0;
int val = 0;
int meassure = 0;

void setup(){
Serial.begin(9600);
}

void loop(){

while(Serial.available()<0){
}

val = Serial.read();
if (val ==‘s’){
meassure = 1;
}
if (val == ‘q’){
meassure = 0;
}

if (meassure == 1){
value = analogRead(sensor);
if ((value <=140) && (value >= 10)){
if((value > 43)) Serial.print(0);
if((value <=39) && (value > 35)) Serial.print(1);
if((value <=35) && (value > 31)) Serial.print(2);
if((value <=31) && (value > 27)) Serial.print(3);
if((value <=27) && (value > 23)) Serial.print(4);
if((value <=23) && (value > 19)) Serial.print(5);
if((value <=19) && (value > 15)) Serial.print(6);
if((value <=15) && (value > 13)) Serial.print(7);
if((value <=13)) Serial.print(8);

}

delay(1000);
}

}

Looks like in your loop you should check if there is any serial data before you try to read it, otherwise the data could be invalid.

// 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;

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);
  Serial.print("s");
}

int val = 0;
int count = 0;

void loop(){
 
  val = Serial.read();
  LED_level(val-48);
 
  if (blink_state == 1){
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED4,HIGH);
      digitalWrite(LED5,HIGH);
      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); 
  }
 
  delay(5000);
}

void LED_level(int value){
  switch (value){
    case 0:
      digitalWrite(LED0,LOW);
      digitalWrite(LED1,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);
      digitalWrite(LED4,LOW);
      digitalWrite(LED5,LOW);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 1:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);
      digitalWrite(LED4,LOW);
      digitalWrite(LED5,LOW);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 2:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);
      digitalWrite(LED4,LOW);
      digitalWrite(LED5,LOW);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 3:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,LOW);
      digitalWrite(LED4,LOW);
      digitalWrite(LED5,LOW);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 4:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED4,LOW);
      digitalWrite(LED5,LOW);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 5:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED4,HIGH);
      digitalWrite(LED5,LOW);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 6:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED4,HIGH);
      digitalWrite(LED5,HIGH);
      digitalWrite(LED6,LOW);
      blink_state = 0;
      break;
    case 7:
      digitalWrite(LED0,HIGH);
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED4,HIGH);
      digitalWrite(LED5,HIGH);
      digitalWrite(LED6,HIGH);
      blink_state = 0;
      break;
    case 8:
      blink_state = 1;
  }
}
// Arduino_2_sensor

int sensor = 0;
int value = 0;
int val = 0;
int meassure = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){
 
  while(Serial.available()<0){
  }
 
  val = Serial.read();
  if (val =='s'){
    meassure = 1;
  }
  if (val == 'q'){
    meassure = 0;
  }
 
  if (meassure == 1){
    value = analogRead(sensor);
    if ((value <=140) && (value >= 10)){
      if((value > 43)) Serial.print(0);
      if((value <=39) && (value > 35)) Serial.print(1);
      if((value <=35) && (value > 31)) Serial.print(2);
      if((value <=31) && (value > 27)) Serial.print(3);
      if((value <=27) && (value > 23)) Serial.print(4);
      if((value <=23) && (value > 19)) Serial.print(5);
      if((value <=19) && (value > 15)) Serial.print(6);
      if((value <=15) && (value > 13)) Serial.print(7);
      if((value <=13)) Serial.print(smiley-cool;
     
    }

    delay(1000);
  }
 
}