Please I need HELP !!!

Hello. I have 1 button and 2 leds, one of them I "use" it as a motor(rotate = ON/not rotate = OFF) and when I push the button the motor is ON, when i push it again the motor is OFF. All is good until here, but I want when the motor is OFF, the other led to blink every 500 milliseconds and when I push the button again, motor will be ON and led OFF, but I have a problem to that.
So ,I need your help to my code ,please....

const int led = 13;
const int button = 11;
const int motor = 12;
int motor_state;
int led_state;
int prev_button_state = LOW;
int time_led = 500;
int time_button = 50;
int zero_time = 0;
int prev_time = 0;

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

void loop()
{
unsigned long currentMillis = millis();

int button_state = digitalRead(button);
if (prev_button_state == LOW && button_state == HIGH)
{
if (motor_state == LOW)
{
motor_state = HIGH;
led_state = LOW;
}
else
{
motor_state = LOW;
if (currentMillis - zero_time >= time_led && motor_state == LOW)
{
zero_time = currentMillis;

if (led_state == LOW)
{
led_state = HIGH;
}
else
{
led_state = LOW;
}
digitalWrite(led, led_state);
}
}
digitalWrite(motor, motor_state);
}
if ((currentMillis - prev_time >= time_button) && (prev_button_state != button_state))
{
prev_time = currentMillis;
prev_button_state = button_state;
}
}

  if  ((currentMillis - prev_time >= time_button) && (prev_button_state != button_state))
  {
    prev_time = currentMillis;
    prev_button_state = button_state;
  }

Why does making the state of the pin for the next pass through loop() depend on time?

To have a delay of the button
Every 50 milliseconds I can push it ,not earlier...but this is not important
I have another problem

Create a short function to make an LED flash - there are examples in Several Things at a Time

Then call that function whenever the motor is OFF and don't call it when the motor is ON

...R

Every 50 milliseconds I can push it ,not earlier...but this is not important

Rubbish. You should be able to press the switch again nanoseconds later. The Arduino should read the state change, but see that it isn't time to use the change, and ignore it.

I have another problem

You have not said what that problem is. You have not said what the code actually does, or how that differs from what you want.

You REALLY should learn to create, and call, functions.

On any given pass through loop(), the switch has, or has not, changed state. If it has, you want to do something with the motor, depending on whether the motor is on or off, and you want to do something with the LED, depending on whether it is on or off, and whether the motor is on or off.

So, create a toggleMotorState() method that turns the motor on or off. Create a toggleLEDState() function that will make the LED blink.

It will be much easier to see what to do with respect to global flags (bool motorOn, bool blinking, etc.) in a separate function and it will be easier to NOT write if statements that depend on too much non-related data.

const int led = 13;
const int button = 11;
const int motor = 12;
int motor_state;
int button_state;
int led_state;
int prev_button_state;
int time_led = 500;
int zero_time = 0;

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

void LED()
{
unsigned long current_Millis = millis();

if (current_Millis - zero_time >= time_led)
{
zero_time = current_Millis;

if (led_state == LOW)
{
led_state = HIGH;
}

else
{
led_state = LOW;
}
digitalWrite(led, led_state);
}
}

void Motor()
{
if (motor_state == LOW)
{
motor_state = HIGH;
}
else
{
motor_state = LOW;
LED();
}
digitalWrite(motor, motor_state);
}

void loop()
{
button_state = digitalRead(button);
if (prev_button_state == LOW && button_state == HIGH)
{
Motor();
}

If you aren't going to tell us what the problem is, there is no point in your posting again.

I want to blink the led every 500 milliseconds when the motor is OFF and when the motor is ON ,led will be off

A description of the problem would be something like this:

The code I posted makes purple fairies hop around on the LCD connected to the Arduino.

I was expecting the code to make green fish swim upside down.

So far, all that we know about your situation is that the code you posted does something, and that that something isn't what you expected.

We still haven't a clue what the code actually does.

arduiNICK:
I want to blink the led every 500 milliseconds when the motor is OFF and when the motor is ON ,led will be off

What about the suggestion I made in Reply #3 ?

...R

I tried it but don’t work…Have a look and try to help me …

const int led = 13;
const int button = 11;
const int motor = 12;
int motor_state;
int button_state;
int led_state;
int prev_button_state;
int time_led = 500;
int zero_time = 0;

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

void LED()
{
unsigned long current_Millis = millis();

if (current_Millis - zero_time >= time_led)
{
zero_time = current_Millis;

if (led_state == LOW)
{
led_state = HIGH;
}

else
{
led_state = LOW;
}
digitalWrite(led, led_state);
}
}

void Motor()
{
if (motor_state == LOW)
{
motor_state = HIGH;
}
else
{
motor_state = LOW;
LED();
}
digitalWrite(motor, motor_state);
}

void loop()
{
button_state = digitalRead(button);
if (prev_button_state == LOW && button_state == HIGH)
{
Motor();
}
}

What is the intended purpose of all this code

void Motor()
{
    if (motor_state == LOW)
    {
        motor_state = HIGH;
    }
    else
    {
        motor_state = LOW;
        LED();
    }
    digitalWrite(motor, motor_state);
}

From your Original Post I have the impression that you want the motor state to change when the button is pressed, not when the motor runs.

I think it should be like this

void loop()
{
    button_state = digitalRead(button);
    if (prev_button_state == LOW && button_state == HIGH) {
        if (motor_state == LOW) {
            motor_state = HIGH;
        }
        else
        {
            motor_state = LOW;
        }
    }
    if (motorState == LOW) {
        LED();
    }
    Motor();
}


void Motor()
{
    digitalWrite(motor, motor_state);
}

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code looks like this

and is easy to copy to a text editor See How to use the Forum

PaulS:
Rubbish. You should be able to press the switch again nanoseconds later. The Arduino should read the state change, but see that it isn't time to use the change, and ignore it.
You have not said what that problem is. You have not said what the code actually does, or how that differs from what you want.

You REALLY should learn to create, and call, functions.

On any given pass through loop(), the switch has, or has not, changed state. If it has, you want to do something with the motor, depending on whether the motor is on or off, and you want to do something with the LED, depending on whether it is on or off, and whether the motor is on or off.

So, create a toggleMotorState() method that turns the motor on or off. Create a toggleLEDState() function that will make the LED blink.

It will be much easier to see what to do with respect to global flags (bool motorOn, bool blinking, etc.) in a separate function and it will be easier to NOT write if statements that depend on too much non-related data.

When pushing a button you usually need to debounce the switch which takes generally between 10-50 milliseconds. That is probably why OP is delaying.

notronrj:
When pushing a button you usually need to debounce the switch which takes generally between 10-50 milliseconds. That is probably why OP is delaying.

Interestingly, there is debounce code in the OPs program - perhaps, more accurately, state-change code, but it probably deals with the bounce problem.

...R

I’m not going to rewrite your code, there are plenty that will try...
But this caught my eye —-

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

Perhaps you meant to write

led_state = !led_state;
digitalWrite(led, led_state);

or perhaps you meant to write

digitalWrite(led, !digitalRead(led));

Only applicable if you don't need the value of led_state elsewhere in the program

Semantics!
There are many ways to plan that into the code - if it’s planned :wink:

Thank you guys ...I did it :slight_smile:

arduiNICK:
Thank you guys ...I did it :slight_smile:

Please share the code you fixed, it will help others on the learning curve.

UKHeliBob:
or perhaps you meant to write

digitalWrite(led, !digitalRead(led));

Only applicable if you don't need the value of led_state elsewhere in the program

This is never applicable. Extremely bad programming practice!!!

Remember, that digitalWrite expects either LOW or HIGH as second argument. Not 0 or 1, not false or true, but LOW or HIGH. Period.

Only LOW or HIGH. Nothing else is acceptable.

Remember, that new generation Arduino API are switching to enum types for such parameters. So, taking such liberties with API will blow into your face eventually.