DC motor turn on if sensor triggerd untril button pressed

Hi guys,

I have a question. I want to run a DC motor(inside the code i use a LED) with a delay after an ultrasonic sensors was triggerd this DC motor needs to stay on until a button is pressed. I have used the if inside the if but i cant get it to work.

int trig=8;
int echo=9;
int led=11;
int button=2;

int buttonState = 0;

void setup() {
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

void loop(){

buttonState == digitalRead (button);

long duration, distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;


if (distance < 10){
  delay (1000);
  digitalWrite(led,HIGH);}

  if (buttonState == HIGH);
  {digitalWrite (led,LOW);

else {
{digitalWrite(led,LOW);}

}}
  if (buttonState == HIGH);

What is that semicolon doing there ?

I thought when the button gets input then off, Right ?

The semicolon is the only code controlled by the state of buttonState. The code block that follows it is executed unconditionally no matter what state buttonState is in

Compare and contrast the other if statements in your code

Yes i fixed what you said but it stil doesnt work the 2 thing that concern me is that the led does not stay on after it was triggerd and the button does nothing

`int trig=8;
int echo=9;
int led=11;
int button=2;

int buttonState = 0;

void setup() {
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

void loop(){

buttonState == digitalRead (button);

long duration, distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;

if (distance < 10){
delay (1000);
digitalWrite(led,HIGH);}

if (buttonState == HIGH){
digitalWrite (led,LOW);}

else{
digitalWrite(led,LOW);}

}`

You need to take a look at that last if/else.

Please remember to use code tags when posting code

yes i understand but i tried many different ways but im stuck with the last if/else, the rest of the code is good so far i can see.

Take a real, good, close look at it.

Hi, @q_o
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

So

  1. Motor (LED) is stopped
  2. Ultrasonic triggers.
  3. Delay.. so many seconds
  4. Motor (LED) runs continuously
  5. Button is pressed
  6. Motor (LED) stops
  7. Loop back to start.

Have you got the ultrasonic working?
Have you got the button press working?
Have you got the motor (LED) start and stop?
Have you got a delay working?

What happens if the button is pressed after the trigger but before the motor start.

Have you got each of these bits of code working?

Tom... :grinning: :+1: :coffee: :australia:

Hi @TomGeorge
Thank you!

This situation i indeed correct

The ultrasonic sensor works!

The button does nothing at the moment but the circuit works.

The LED works if i get rid of this code.
if (buttonState == HIGH){ digitalWrite (led,LOW);}
and the LED doesnt stay on.

The delay works!

Normally that would be inpossible because the motor pushes agains the button.

Q-O.... thnx for you time :v: :netherlands:

Hi,

What? ? ?
So the motor when it runs, finally activates a "button" to turn itself off.

PLEASE what is your project?
What are you trying to do?

How do you restart the process, after the motor has hit the "button" and stopped itself?
What do you want to have happen when the distance goes back to > 10?

Please post your entire code, and please post a circuit diagram, the LED as the motor will be fine.

Tom... :grinning: :+1: :coffee: :australia:

Hi,
This

if (buttonState == HIGH);
  {digitalWrite (led,LOW);

else {
{digitalWrite(led,LOW);}

Is not formatted properly.
I think this is what you wanted;

if (buttonState == HIGH)
{
digitalWrite (led,LOW);
}
else 
{
digitalWrite(led,LOW);
}

That gets the structure correct, BUT not the logic.
If the button is HIGH, set the LED LOW, else, set the LED LOW.

Not really doing much, even if the button is LOW you still set the LED LOW.

Tom... :grinning: :+1: :coffee: :australia:

Hi,

The ''else'' was for the ultrasonic sensor so if the sensor doesnt see anything he does nothing.

I want to make a dispenser that works after the hand is gone.

  • So the ultrasonic sensor get triggerd
  • wait until hand is gone
  • DC motors spins
  • DC motor pushes agains a button that notifies that if has been enough spinning
  • DC motor stops

I was thinking to put a delay on the putton so that te motor spins a little bit past the button so that i can restart the proces.

This is my entire code

int trig=8;
int echo=9;
int led=11;
int button=2;

int buttonState = 0;

void setup() {
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

void loop(){

buttonState == digitalRead (button);

long duration, distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;


  if (distance < 10){
  delay (1000);
  digitalWrite(led,HIGH);}

    if (buttonState == HIGH){
    digitalWrite (led,LOW);}

else{
digitalWrite(led,LOW);}

  }

and this is the circuit

I hope its a little bit clear what im trying to do.

Q-O... :v: :netherlands:

If the else is supposed to belong to the first if then try

  if (distance < 10){
  delay (1000);
  digitalWrite(led,HIGH);}

  else if (buttonState == HIGH){
    digitalWrite (led,LOW);}

Steve

what is the power pin of the sonar sensor

Hi
@slipstick i put in the code you worte but it stil doesnt work. :grimacing:

@comshocks the vcc is the + to the solar sensor.

Q-O... :v: :netherlands:

Post your latest version so I don't have to guess what you've done and say exactly HOW it doesn't work.

Steve

@slipstick

int trig=8;
int echo=9;
int led=11;
int button=2;

int buttonState = 0;

void setup() {
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

void loop(){

buttonState == digitalRead (button);

long duration, distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;


    if (distance < 10){
  delay (1000);
  digitalWrite(led,HIGH);}

  else if (buttonState == HIGH){
    digitalWrite (led,LOW);}
    
else{
digitalWrite(led,LOW);}

  }


   if (buttonState == HIGH){
    digitalWrite (led,LOW);}
    
else{
digitalWrite(led,LOW);}

  }

Again, take a really close look at that final if/else

Hi,

Yes i understand what you mean (LOW, LOW) but the last else is supose for the ultrasonic sensor, i dont know how to do it difrent.

Q-O... :v: :netherlands: