Wait when restart Uno R4

Hi,
I have an Arduino Uno R4 wifi that I want to control a motor that will lock a gate.
Right now I have a small problem that I think someone can help me with.
I haven't gotten that far but, I get the motor to go in the right direction at the right time.

My problem is that when restarting the program and my Pin5 is high, the program runs the lock statement, I want the program to wait for an event.
If my Pin5 is low when restarting, the program waits until I change the pin to high.

Can I get the program to wait for an event when restarting, regardless of the current state?

#include "CytronMotorDriver.h"

CytronMD motor(PWM_DIR, 3, 4);                            // PWM = Pin 3, DIR = Pin 4.

int PinLock = 5;                                          // Define Pin 5 as a variable
int PinLockValue;                                         // Define variable

int buttonState = 0;                                      // current state of the button
int lastButtonState = 0;                                  // previous state of the button

int Wait = 1000;                                          // Variable
int Runtime = 5000;                                       // Variable
int Stoptime = 500;                                       // Variable

//////////////////////////////////////////////////////////
void setup() {

  pinMode(PinLock, INPUT);                                // Pin 5 is input
  Serial.begin(9600);                                     // Start serial port
  delay(Wait);
  Serial.println("Program is runing!");
  }

//////////////////////////////////////////////////////////
void loop() {

  PinLockValue = digitalRead(PinLock);                    // Reading Pin 5 to variable
  buttonState = digitalRead(PinLock);                     // Reading Pin 5 to variable
    
  if (buttonState != lastButtonState) {                   // If variablen not equal to variable

      if (buttonState == 1) {
      motor.setSpeed(255);                                // Actuator is runing forward
      Serial.println("Locking the gate.");
      delay(Runtime);

      motor.setSpeed(0);                                  // Stop the actuator
      Serial.println("Stop");
        delay(Stoptime);
    }

    if (buttonState == 0) {
      motor.setSpeed(-255);                               // Actuator is runing bakward
      Serial.println("Unlocking the gate.");
      delay(Runtime);

      motor.setSpeed(0);                                  // Stop the actuator
      Serial.println("Stop");
      delay(Stoptime);
    }
  }
  lastButtonState = buttonState;                           // Variable equal to Pin 5
}

Best Regads
Thomas

What caused Pin 5 to be HIGH after restart?
Is this an emergency situation for your lock mechanism and if so, is it necessary to take any action to eliminate it?

My switch that I use to control the lock is either open or closed, so I can't eliminate this.

Think about how you are initializing lastButtonState.

This should not be the case, this is a sign of bad design.
The system must be designed so that upon restart the pin is always in a known position.

By the way, why do you need a duplicate variables for Pin5 state?

I made a slight change to the initiation but I don't see any difference in the outcome.

int lastButtonState;                                      // previous state of the button
//int lastButtonState = 0;                                // previous state of the button

Hint: if you just want to wait changing Pin5 state - it's simple. Read the state upon restart and do nothing until the state changed.

Global variables are by default initialized to zero, if I recall correctly. So you change would not have made any difference.

Think some more. It's literally a one line change to make it work if the button starts off LOW or HIGH.

Haha, I can hear you laughing at a beginner, I'll be back tomorrow. We'll see if you need to give me any more tips!
I might have to look a bit more at Arduino.cc
Thanks guys for your quick replies!

I didn't. My suggestion is a correct solution of the issue.

I don't doubt that you're right, it's just that I don't know how to implement what you write.

This is what I am trying to accomplish!

Shelly and R4 wifi. You need 2 wifi connected devices?

The initial value of 0 for lastButtonState means you've stored the value it has when the switch is open.

int latButtonState = 0;  // would be zero anyway

This suggestion

is to initialise that variabke in your setup()function instead, by actually reading the switch. So if the switch is closed, so is your idea of what is the last button state, it is adjusted to reflect the startup condition of the switch.

HTH

a7

Hey,
Now I want to show what I have managed to do, as the cut and paste coder I am, with the help of Microsoft Copilot I can also succeed! Now I have managed to do two things:

  1. My little project looks good and will most likely work.
  2. I have used Copilot for the first time, we became really good friends :smiley:

I understand that some of you do not think it is an achievement to use Copilot, but even though Copilot did a lot, I have also learned a little. I can recommend trying it if you are stuck due to lack of knowledge.

If anyone can benefit from this code, in whole or in parts, please do!

Best regards.

#include "CytronMotorDriver.h"                                       // Include the Cytron motor driver library

CytronMD motor(PWM_DIR, 3, 4);                                       // Create a motor object using PWM on pin 3 and DIR on pin 4

const int PinLock = 5;                                               // Pin for the lock signal
const int PinGate = 6;                                               // Pin for the gate status
const int PinLockStatusSwitch = 7;                                   // Pin for the lock status

int PinLockState;                                                    // Current state of the lock
int PinLockStateLast;                                                // Previous state of the lock
int PinGateState;                                                    // Current state of the gate
int PinGateStateLast;                                                // Previous state of the gate
int LockStatusState;                                                 // Current state of the lock status
int LockStatusStateLast = LOW;                                       // Previous state of the lock status

int Wait = 500;                                                      // Startup delay
int Runtime = 5000;                                                  // Duration the motor should run
int Stoptime = 50;                                                   // Short delay after stopping the motor

void setup() {
  pinMode(PinLock, INPUT_PULLUP);                                    // Set lock signal pin as input with pull-up
  pinMode(PinGate, INPUT_PULLUP);                                    // Set gate status pin as input with pull-up
  pinMode(PinLockStatusSwitch, INPUT_PULLUP);                        // Set lock status switch pin as input with pull-up

  Serial.begin(9600);                                                // Start serial communication
  delay(Wait);                                                       // Wait for system to stabilize
  Serial.println("Program is running!");                             // Print startup message

  PinLockState = digitalRead(PinLock);                               // Read initial lock signal state
  PinLockStateLast = PinLockState;                                   // Store initial lock signal state
  PinGateState = digitalRead(PinGate);                               // Read initial gate state
  PinGateStateLast = PinGateState;                                   // Store initial gate state
  LockStatusState = digitalRead(PinLockStatusSwitch);                // Read initial lock status switch state
  LockStatusStateLast = LockStatusState;                             // Store initial lock status switch state

  Serial.print("Initial Lock State: ");                              // Print label for lock state
  Serial.println(PinLockState == LOW ? "LOCKED" : "UNLOCKED");       // Print initial lock state

  Serial.print("Initial Gate State: ");                              // Print label for gate state
  Serial.println(PinGateState == HIGH ? "OPEN" : "CLOSED");          // Print initial gate state

  Serial.print("Initial Lock Status Switch: ");                      // Print label for lock status
  Serial.println(LockStatusState == LOW ? "LOCKED" : "UNLOCKED");    // Print initial lock status
}

void loop() {
  PinLockState = digitalRead(PinLock);                               // Read current lock signal state
  PinGateState = digitalRead(PinGate);                               // Read current gate state
  LockStatusState = digitalRead(PinLockStatusSwitch);                // Read current lock status switch state

  if (PinGateState != PinGateStateLast) {                            // If gate state has changed
    Serial.println();                                                // Print blank line for clarity
    Serial.print("Door is ");                                        // Print label
    Serial.println(PinGateState == HIGH ? "OPEN" : "CLOSED");        // Print gate state
  }

  if ((PinLockState != PinLockStateLast) && PinGateState == LOW) {   // If lock signal changed and gate is closed
    Serial.println();                                                // Print blank line for clarity
    Serial.print("Lock signal is now: ");                            // Print label
    Serial.println(PinLockState == LOW ? "LOCKED" : "UNLOCKED");     // Print lock signal state

    if (PinLockState == LOW) {                                       // If signal is LOW (lock)
      Serial.println("Locking the gate.");                           // Print locking message
      motor.setSpeed(255);                                           // Run motor forward to lock
      delay(Runtime);                                                // Wait for motor to complete
      motor.setSpeed(0);                                             // Stop the motor
      Serial.println("Stop");                                        // Print stop message
    } else {                                                         // If signal is HIGH (unlock)
      Serial.println("Unlocking the gate.");                         // Print unlocking message
      motor.setSpeed(-255);                                          // Run motor in reverse to unlock
      delay(Runtime);                                                // Wait for motor to complete
      motor.setSpeed(0);                                             // Stop the motor
      Serial.println("Stop");                                        // Print stop message
    }
    delay(Stoptime);                                                 // Short delay after motor stops
  }

  if (LockStatusState != LockStatusStateLast) {                      // If lock status switch state has changed
    Serial.println();                                                // Print blank line for clarity
    Serial.print("Lock status changed: ");                           // Print label
    Serial.println(LockStatusState == LOW ? "LOCKED" : "UNLOCKED");  // Print lock status
  }

  PinLockStateLast = PinLockState;                                   // Update previous lock signal state
  PinGateStateLast = PinGateState;                                   // Update previous gate state
  LockStatusStateLast = LockStatusState;                             // Update previous lock status switch state

  delay(100);                                                        // Small delay to reduce loop speed
}


1 Like

So… I don't have enough whatever left today to figure out what your code does.

It will lock and unlock on your signal if the gate is closed. But what opens and closes the gate?

Where are the roller switches in the mechanism that uses the linear actuator? Is that actuator running the lock or the gate or what?

As for how you did it, I say success is hard to argue with. At the very least you managed to keep Copilot focused long enough to succeed, but I must point out you needed some real help as well.

I've been trying AI just to see how it goes; mixed results and I can def say I learn less when I drag an idea all the way through letting someone else do the actual coding.

But it is amazing to watch, even if it gets confused. Then it's fun.

I find for most tech stuff like coding, it is quite good at things I could do myself if I weren't so lazy. Or had the least interest in learning, like programming in Java.

a7

1 Like