Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Arduino ping sensor turning on led,

I have no clue how to code at all, the syntax has just never worked in my head lmfao anyhow I'm trying to make a Ping sensor light up LED's one by one as the person gets closer to this object, the issue for me is I want a button to turn this setting on in the first place, so if i had lets say an orb like object in the middle of my table cause i was some bullshit fortune teller, I would want to tap a button under the desk then 10 seconds later it's "on" and if a person gets closer to it.. it illuminates more? how is this done?
(Feb 15, 2016 07:06 PM)Coffee is love Wrote:  I have no clue how to code at all, the syntax has just never worked in my head lmfao anyhow I'm trying to make a Ping sensor light up LED's one by one as the person gets closer to this object, the issue for me is I want a button to turn this setting on in the first place, so if i had lets say an orb like object in the middle of my table cause i was some bullshit fortune teller, I would want to tap a button under the desk then 10 seconds later it's "on" and if a person gets closer to it.. it illuminates more? how is this done?

Hm. I have no idea how to do the code. But the basic concept can either be having the arduino wait to get a button press, then once it's pressed you turn on sensor and use a loop to keep checking. Or you use a loop and in each iteration check if the button is on, if it's on, you take the sensor data and affect the LED, else you just ignore the input. That's how I would think about it.
a switch and a infra red sensor should do the trick. or the ultrasonic audible sensor (hc-sr04). take your pick, actually have a look around and check youtube to find out which will suit best. i know there is a fair few guides out there and probably even some schematics and code for you too. i just remember there is and example for the ultrasonic audible sensor (hc-sr04) and i can tell you by just using that code and adding a few more lines of code you will get your result.
Basically this is what it looks like without the button,
Code
#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;


  if (distance <= 30) {
    digitalWrite(led, HIGH);
    }
  else {
    digitalWrite(led,LOW);
  }
  if (distance < 25) {
      digitalWrite(led2, HIGH);
    }
  else {
      digitalWrite(led2, LOW);
  }
  if (distance < 20) {
      digitalWrite(led3, HIGH);
      sound = 270;
}
  else {
    digitalWrite(led3, LOW);
  }

  if (distance > 30 || distance <= 0){
    Serial.println("Out of range");
   }
  else {
    Serial.print(distance);
    Serial.println(" cm");
      
  }
  delay(500);
}
(Feb 15, 2016 07:58 PM)Coffee is love Wrote:  Basically this is what it looks like without the button,
Code
#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;


  if (distance <= 30) {
    digitalWrite(led, HIGH);
    }
  else {
    digitalWrite(led,LOW);
  }
  if (distance < 25) {
      digitalWrite(led2, HIGH);
    }
  else {
      digitalWrite(led2, LOW);
  }
  if (distance < 20) {
      digitalWrite(led3, HIGH);
      sound = 270;
}
  else {
    digitalWrite(led3, LOW);
  }

  if (distance > 30 || distance <= 0){
    Serial.println("Out of range");
   }
  else {
    Serial.print(distance);
    Serial.println(" cm");
      
  }
  delay(500);
}

I think you just need to wrap the if statements in the loop up in a bigger if statement that checks if button is in a "on" state or not. Should be trivial.
Idk how to do that though, this is what I need help withw
Do you know how to check if an attached button is on or off?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Brotherhood] by itcorpJul 30, 2026 01:52 AM 4 181 Jul 30, 2026 01:52 AM
Last Post: HerrWinston
[Java] by Nemo2478Oct 28, 2019 01:42 AM 0 179 Oct 28, 2019 01:42 AM
Last Post: Nemo2478
[Java] by Drain.Feb 1, 2018 03:05 PM 2 173 Feb 1, 2018 03:05 PM
Last Post: Get Hit