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
}
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?
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!
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.
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:
My little project looks good and will most likely work.
I have used Copilot for the first time, we became really good friends
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
}
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.