I just began learning Arduino a month ago so bear with me for my etiquette infractions!
Ultimately, I am trying to create a table whose legs sequentially raise up and down (via 4x linear actuators) until a user inserts a coin into the table (via coin acceptor switch) which interrupts the loop for X seconds and afterwards returns to what it was doing. So far, I've modified the blinkwithoutdelay example code to the point where I can get the light to stop blinking for X seconds after a coin is inserted, but I'm having a hard time getting this code to operate the actuators.
I have a separate file that allows me to operate the actuators once sequentially when a coin is inserted, which feels great, but isn't getting me what I need since I can't use the delay function in the final code except for the result of a coin insertion.
How might I go about further modifying the blinkwithoutdelay code to allow operation of the actuators? Here is the code as it exists now, warts and all.
modified blinkwithoutdelay
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
#define button 2; //coin acceptor
int coinState = 0; //button is not pressed?
int pushButton = 2; // coin signal input
#define motor1in1 9 //motor drive forward
#define motor1in2 10 //motor drive reverse
int rotDirection = 0;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(pushButton, INPUT);
pinMode(motor1in1, OUTPUT);
pinMode(motor1in2, OUTPUT);
digitalWrite(motor1in1, LOW);
digitalWrite(motor1in2, HIGH);
}
void loop() {
int coinState = digitalRead(pushButton);
if (coinState == HIGH){ // the signal wire is always HIGH, so coin insertion will give me a LOW state
unsigned long currentMillis = millis();
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}else { //when signal wire is LOW
delay(5000); //amount of time coin disables the loop for
}
}
here is the actuator driving code
#define enA 11 //enable pwm
#define motor1in1 9 //motor drive forward
#define motor1in2 10 //motor drive reverse
#define motor2in1 7 //motor drive forward
#define motor2in2 8 //motor drive reverse
#define button 2 //coin acceptor
const long interval = 1000; // how many miliseconds to drive a motor for
unsigned long previousMillis = 0; // will store last time motor was updated
int rotDirection = 0;
int pressed = false;
void setup() {
pinMode(enA, OUTPUT); //not currently used
pinMode(motor1in1, OUTPUT);
pinMode(motor1in2, OUTPUT);
pinMode(motor2in1, OUTPUT);
pinMode(motor2in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(motor1in1, LOW);
digitalWrite(motor1in2, HIGH);
digitalWrite(motor2in1, LOW);
digitalWrite(motor2in2, HIGH);
}
void loop() {
//int potValue = analogRead(A0); // Read potentiometer value
//int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
//analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
// Read button - Debounce
if (digitalRead(button) == true) {
pressed = !pressed;
}
while (digitalRead(button) == true);
delay(10);
// If button is pressed - change rotation direction
if (pressed == true & rotDirection == 0)
{
digitalWrite(motor1in1, HIGH);
digitalWrite(motor1in2, LOW);
rotDirection = 1;
delay(1000);
digitalWrite(motor1in1, LOW);
digitalWrite(motor1in2, HIGH);
rotDirection = 0;
delay(1000);
digitalWrite(motor2in1, HIGH);
digitalWrite(motor2in2, LOW);
rotDirection = 1;
delay(1000);
digitalWrite(motor2in1, LOW);
digitalWrite(motor2in2, HIGH);
rotDirection = 0;
delay(1000);
}
// If button is pressed - change rotation direction
//if (pressed == false & rotDirection == 1)
{
// digitalWrite(in1, LOW);
// digitalWrite(in2, HIGH);
// rotDirection = 0;
// delay(20);
}
}