Ping Arduino Led and Switch

Hi to all that read this.

What I am looking to do is, make A program that will use led,switch,arduino,and ping sensor. I have some of the program started, but I need some big help.

Here is what I am trying to do. The switch will be an input. The led will go off and on if the ping distance is > 10 and the switch in put is High.

(distance > 10)= ping500 (High)

ping500 and switchpin
digitalWrite(ledpin,HIGH)

This I what I want the Led to do...................
So switchpin=(High) and ping500=(High). Then Led is on.

If Switchpin=(Low) and ping500=(High). Then Led is off.

If switchpin=(Low and ping500=(Low). Then Led is off.

If switchpin=(Low and ping500=(Low) Then Led is off.

#define switchPin 2
#define ledPin 3
#define trigPin 13
#define echoPin 12

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
 
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

You've provided no information on how the switch is wired. You are not using the internal pullup resistor, so wiring the switch is more difficult than it needs to be.

It seems to me that you need not bother with reading the ping sensor if the switch is not pressed. All the situations where the switch is not pressed result in the LED pin being LOW, regardless of the ping distance, so you need not bother reading the sensor.

The switch is A pulldown resistor,( if the switch is pushed the pin is high), I was working on more of the code. I add one more pin. (Outputpingpin 4) to resistor and led. But I don't know how to get the (if (distance > 10). How do I make this A High or low. So
my if (High or low, from distance > 10 && SwitchPin) digitalWrite (3,HIGH)

#define trigPin 12
#define echoPin 13
#define ledPin 3
#define switchPin 2
#define outputpingPin 4



void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(outputpingPin, OUTPUT);
}

void loop() {

  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  
  {
  if (distance > 10) 
  digitalWrite (4,HIGH);
  
  else 
     digitalWrite (4,LOW);
  } 
  {
 if  (High or low, from distance > 10 &&  SwitchPin) 
       digitalWrite (3,HIGH)
 else
  
   digitalWrite (3,LOW); 
  }    
     delay(500);
}
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }

You're reading the sensor, even though you may not need to. Why?

You are not reading the switch state, even though you DO need to. Why not?

 if  (High or low, from distance > 10 &&  SwitchPin)

Pure gibberish. If you can't even phrase your question properly, you'll never get it coded correctly.

Hi

I will give it one more try, on what I am trying to do. It is hard to tell people what I am trying to do.
Here is the setup. Box1 (ping sensor, Arduino, xbee, and resistor led). Box2 (ping sensor, Arduino, xbee, and resistor led). The xbee's are set up as line passing, only.

If both ping sensor's read (distance > 10) then both Led's will be High one Box1 and Box2.
If ping1 sensor read's (distance < 10) and the ping2 sensor read's (distance < 10), Both led's will read low.
If Ping1 sensor read's (distance < 10) and the ping2 sensor read's (distance < 10), Both led's will read low.

There is A pic.pdf file.

pic.pdf (11.7 KB)

Box1 (ping sensor, Arduino, xbee, and resistor led). Box2 (ping sensor, Arduino, xbee, and resistor led). The xbee's are set up as line passing, only.

There is no mention of the switch that seemed to be the focus up to now. This is the first time that XBees have been mentioned.

There is no code to read, on one Arduino, what the XBee on the other Arduino sent.

Sending the distance ONLY makes sense. The only possible values for distance that you are concerned about are between 0 and 255, so, use Serial.write() to send the value, instead of Serial.print(), assuming that the XBees are on the serial port.

The print() method converts the value to a string, making reading more complicated. The write() method sends the value in binary. Read one byte to get the value sent, as a byte or int, rather than a string.

I have to give this one more try, as you can see, I am not that good telling you people what I want in words. Here We Go.

The first program, I have with the (one Arduino,two ping sensor's,two resistor's and led's). The program works great.

What I need your help is with, making all this wireless, with two xbee's.
Here is the set-up. == (Arduino,xbee,ping,resister and led) (ping,xbee,resister and led)
I want to make the xbee's do line passing, I do have that working.
Maybe Serial data world work to.

I am trying to save on the battery power, So I think line passing with xbee's is the best.

On the sec. program I have my program, On this program,
I am using two Arduino's. I tried line passing with the xbee's.
Here is the Set-up. Box1== (Arduino,xbee,ping,and resistor and led) and
Box2== (Arduino,xbee,ping,and resistor and led).

It needs help with code, because I don't know how to get line (else if (XbeeOutPin && (XbeeInPin,INPUT);//I can't get this line to work, needs some help?)

(((((First)))))))


#define trigPin 13
#define echoPin 12

#define trigPins 3
#define echoPins 2

#define led 7
#define ledd 8


void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPins, OUTPUT);
  pinMode(echoPins, INPUT);
  pinMode(led, OUTPUT);
  pinMode(ledd, OUTPUT);
 
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 

  long durations, distances;
  digitalWrite(trigPins, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPins, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPins, LOW);
  durations = pulseIn(echoPins, HIGH);
  distances = (durations/2) / 29.1;
  
  
  
if (distance >= 200  || distance <= 0)
  { 
    Serial.println("Out of range");
}
else if(distances >= 200  || distances <= 0)
{
          Serial.println("Out of range");
}
  else 
  {
    Serial.print(distance);
    Serial.println(" cm");
    Serial.print(distances);
    Serial.println(" cm");
    }
     
 
    
    
if (distances < 30 && distance < 30)
{
digitalWrite(led, HIGH);
digitalWrite(ledd, HIGH);
}
else
{
digitalWrite(led,LOW);
digitalWrite(ledd,LOW);
}
         
delay(50);
}
((((((Sec)))))))

#define trigPin 12
#define echoPin 13
#define LedPin 2
#define XbeeInPin 3
#define XbeeOutPin 4


void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LedPin,OUTPUT);
  pinMode(XbeeInPin,INPUT);
  pinMode(XbeeOutPin,OUTPUT);

  

}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  
 if (distance < 10); 
                    
     {
   digitalWrite(XbeeOutPin,HIGH); 
         }
    else if (XbeeOutPin && (XbeeInPin,INPUT);//I can't get this 
                                             // line to work, needs some help?
    {
      
      digitalWrite (LedPin,HIGH);
    }
    
    else
    {
      digitalWrite (LedPin,Low);
  delay(500);
}

I don't understand what you are doing on the second device. The first reads two ping sensors, poorly named, and sends the values and a bunch of other useless stuff to the serial port, where the XBee sends it over the air.

The second unit also appears to be reading a ping sensor. Then,

 if (distance < 10);

If the distance is less than 10, do nothing (;). Otherwise, do nothing (no else block). Why bother deciding whether to do nothing?

    else if (XbeeOutPin && (XbeeInPin,INPUT);//I can't get this 
                                             // line to work, needs some help?
    {
      
      digitalWrite (LedPin,HIGH);
    }

This else if statement makes no sense. What are XbeeOutPin and XBeeInPin for? If they are supposed to be SoftwareSerial port pins, then you are missing a lot of code to deal with a software serial port.

If they are for something else, what, exactly, are they for?

I'm nearly certain that (XbeeInPin, INPUT) is NOT doing what you think it is. But, since I can't tell what you think it is supposed to do, I can't be positive.

What, exactly, is supposed to make XbeeInPin HIGH or LOW? You've already declared that XbeeInPin is an INPUT pin. While there are ways to detect the direction of a pin, the easiest is for you to simply remember which direction you defined for the pin.

Instead of talking about what you think you need to do, perhaps what you need to describe is why each Arduino has a ping sensor, what each is supposed to collect data from, and what each is supposed to send data to the other for.

I am trying to save on the battery power, So I think line passing with xbee's is the best.

Saving battery power while using XBees hardly seems reasonable. Nothing in the code on either Arduino is putting the XBees to sleep or waking them up. What "line passing" means or how it relates to power savings is not at all clear.

It needs help with code, because I don't know how to get line (else if (XbeeOutPin && (XbeeInPin,INPUT);//I can't get this line to work, needs some help?)

Instead of talking about the code that you think might possibly do what you want, talk about what you want the code to do. We'll tell you what code to then use.

Line passing with xbee's is, sending (high or low) to I/O pin on the xbee;s. Let's say pin 10 goes high, on xbee1. The other xbee2, let's say pin 8, will go high. If pin 10 on xbee1 goes low, then xbee2 pin8 will go low. Then on xbee2 if you were to use pin 4 (high). Xbee1 pin 3 will go (high).
It's kind of like A momentary switch.

Here is what I am shooting for, use (Arduino,ping,xbee,resistor and led) will = Box1 and
(Arduino,ping,xbee,resistor and led) will = Box2.

Box1 (if ping1 > 10 will = (High or Low), that will all so send (High or Low) to Box2, with line passing to xbee2.

Box2 (if ping2 > 10 will = High or Low), that will all so send (High or Low) to Box1, with line passing to xbee1.

1 = High and 0 = Low

If box1 ping is 7 = 1 If box1 ping is 78 = 0
If box2 ping is 5 = 1 If box1 ping is 36 = 0

Box1 Box2
ping = 1 allso send 1 to box2 input xbee2 = 1
input from xbee1 ping = 1 allso send 1 to box1
H
If (inputxbee1 and pin =1) If (inputxbee2 and ping = 1)
digitalWrite led1 = 1 digitalWrite led1 = 1

====================================================================
Box1(ping1)(inputxbee1)(Led1)Box2(ping2)(inputxbee2)
(Led2)
if 1 0 0 0 1 0
if 0 1 0 1 0 0
if 1 1 1 1 1 1

This is how the program should work.

ping 13.jpg

You can't use AT mode to directly set/read XBee pins. That can only be done in API mode.

Just Help me with this program, I have the xbee's working, line passing working. AT mode,
has to be programed in the xbee, not the arduino.

I need A line in the code that reads (if (distance > 10) = HIGH == distanceHigh.
distanceHigh needs to be high if it is > 10
distanceHigh needs to be low if it is < 10

This is what I don't know how to program, it may need A distanceHigh = (distance >10)=HIGH)

if (distance > 10); 
                     {
   digitalWrite(OutPin,HIGH); 
         }
    else if (distanceHigh && (InPin,INPUT);
#define trigPin 12
#define echoPin 13
#define LedPin 2
#define InPin 3
#define OutPin 4


void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LedPin,OUTPUT);
  pinMode(InPin,INPUT);
  pinMode(OutPin,OUTPUT);

  

}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  
 if (distance < 10); 
                     {
   digitalWrite(OutPin,HIGH); 
         }
    else if (OutPin && (InPin,INPUT);
                                             
    {
      
      digitalWrite (LedPin,HIGH);
    }
    
    else
    {
      digitalWrite (LedPin,Low);
  delay(500);
}
if(distance > 10)
{
   // Distance is greater than 10. Note that there is no semicolon on the if statement
}

PaulS

Thank you so much for trying to give me A big Big hand in all of this. I think I got it, working.

The inpin and the outpin will be hooked up to I/O pins on one xbee. Line passing only... On one box=Arduino,xbee,resistor,and led.

#define trigPin 12
#define echoPin 13
const int InPin = 3; 
#define OutPin 4
#define LedPin 5



int buttonState = 0;
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 
  pinMode(OutPin, OUTPUT);
  pinMode(LedPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(InPin);

  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
 
 if (distance < 10)
 {
   digitalWrite(OutPin, HIGH);
 }
 else 
 {
 digitalWrite(OutPin, LOW);
 }
  
 if  (buttonState == HIGH && distance < 10) 
 {
        digitalWrite(LedPin, HIGH);
 }
         else
  {
       digitalWrite(LedPin, LOW);
  }
    delay(500);
}

Thank you..............PaulS

Hi

One last thing, how would I go about sending the I/O pin data, using Serial data. Using xbee's Dout and Din. Using Baud 9600. So I don't have to use Line Passing.

const int InPin = 3
#define OutPin 4
#define trigPin 12
#define echoPin 13
const int InPin = 3; 
#define OutPin 4
#define LedPin 5



int buttonState = 0;
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 
  pinMode(OutPin, OUTPUT);
  pinMode(LedPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(InPin);

  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
 
 if (distance < 10)
 {
   digitalWrite(OutPin, HIGH);
 }
 else 
 {
 digitalWrite(OutPin, LOW);
 }
  
 if  (buttonState == HIGH && distance < 10) 
 {
        digitalWrite(LedPin, HIGH);
 }
         else
  {
       digitalWrite(LedPin, LOW);
  }
    delay(500);
}