Hello,
I have been learning arduino programming for the last days to make a roulette vending machine using an arduino UNO 3, LED's, relays and DC Motors (see schematic)
.
This is what I wan't my program to do:
Push button -> lights start alternating -> push button -> alternating stops on last active light -> corresponding motor starts spinning -> 4seconds later motor stops
For now my code works but I would like to know how to replace the delay function with a millis function in the rotatecoil() function that I made at the end, so I can run other processes in the background. I've been looking into "blinking without delay" and other millis uses, but I don't see how I could use it for a 1 time action.
int LEDState=0; //program on = 1 or off = 0
int motorState=0;
int buttonNew;
int buttonOld=1;
int activeLED = 1; //LED number that is activated
unsigned long previousMillis = 0;
int intervalLED = 400; //interval speed of LED's
int activetimeMotor = 4000; //rotation time motors
//defining pins
int BUTTON=13;
int L0=2; //L=LED
int L1=8;
int L2=9;
int L3=10;
int L4=11;
int M1=4; //M = MOTOR
int M2=5;
int M3=6;
int M4=7;
void setup() {
Serial.begin(9600);
//setting pins as an INPUT or OUTPUT
pinMode(BUTTON,INPUT);
pinMode(L0,OUTPUT);
pinMode(L1,OUTPUT);
pinMode(L2,OUTPUT);
pinMode(L3,OUTPUT);
pinMode(L4,OUTPUT);
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);
pinMode(M3,OUTPUT);
pinMode(M4,OUTPUT);
}
void loop() {
startstop();
if (LEDState==1) {
runningLED();
}
if (motorState==1) {
rotateCoil();
}
}
void startstop() {
buttonNew=digitalRead(BUTTON);
if(buttonOld==0 && buttonNew==1){
if (LEDState==0){
digitalWrite(L0,HIGH);
LEDState=1;
}
else{
digitalWrite(L0,LOW);
motorState=1;
LEDState=0;
}
}
buttonOld=buttonNew;
}
void runningLED() {
unsigned long currentMillis = millis(); //initialize currentMillis by a function millis()
if (currentMillis - previousMillis >= intervalLED) { //see if interval has been reached
previousMillis = currentMillis;
//activeLED will start at 1 then 2 for another loop and vise versa
if (activeLED == 1) {
digitalWrite(L1,HIGH);
digitalWrite(L2,LOW);
digitalWrite(L3,LOW);
digitalWrite(L4,LOW);
}
else if (activeLED == 2){
digitalWrite(L1,LOW);
digitalWrite(L2,HIGH);
digitalWrite(L3,LOW);
digitalWrite(L4,LOW);
}
else if (activeLED == 3) {
digitalWrite(L1,LOW);
digitalWrite(L2,LOW);
digitalWrite(L3,HIGH);
digitalWrite(L4,LOW);
}
else if (activeLED == 4) {
digitalWrite(L1,LOW);
digitalWrite(L2,LOW);
digitalWrite(L3,LOW);
digitalWrite(L4,HIGH);
activeLED=0;
}
activeLED++; //activeLED will increase by 1 every loop
}
}
void rotateCoil(){
if (activeLED == 1) {
digitalWrite(M1,HIGH);
delay(activetimeMotor);
digitalWrite(M1,LOW);
}
else if (activeLED == 2){
digitalWrite(M2,HIGH);
delay(activetimeMotor);
digitalWrite(M2,LOW);
}
else if (activeLED == 3) {
digitalWrite(M3,HIGH);
delay(activetimeMotor);
digitalWrite(M3,LOW);
}
else if (activeLED == 4) {
digitalWrite(M4,HIGH);
delay(activetimeMotor);
digitalWrite(M4,LOW);
}
motorState=0;
}
