Hello All Gurus of Arduino.
I read several posts here and with help of all of you, I am putting together a code, Please help me find , why does pressing the push button doesn’t start the process.
Here’s the situation:
BladePos and BottlePos are sensors; the associated pins needs to be high to start a process. Once they are high, a green light needs to turn on. The code works until here.
Now, when the GreenPushButton is pressed nothing happens.I want it to start moving a stepper motor, servo motor and again stepper motor at interval of 2.5 seconds.
There is another input called ‘SelSwitch’ , if this was high, the process will also include turning on BlueLed.
I also want the process to stop immediately, if BladePos or BottlePos goes Low at any time.
Note: code includes calling the functions as mentioned in post, ‘planning and Implementing an Arduino program’. It also uses millis to time the functions
Stepper motor is being controlled using L293D IC and doesn’t use any library. The stepper code works on its own
#include <Servo.h>
//LEDs and inputs declaration.
const byte GreenledPin = 0;
const byte RedledPin = 1;
const byte A = 2;
const byte A_bar = 3;
const byte B = 4;
const byte B_bar = 5;
const int x = 5000;
const byte stepsPerRevolution = 200;
const byte GreenPushButtonPin = 6;
const byte BladePosPin = 7;
const byte BottlePosPin = 8;
const byte SelSwitchPin = 9;
const byte BlueledPin = 11;
byte GreenPushButtonState;
byte BladePosState;
byte BottlePosState;
byte SelSwitchState;
boolean Ready = false;
boolean Start = false;
unsigned long buttonPushedMillis; // when button was pushed.
unsigned long Delay1 = 2500; // wait
unsigned long Delay2 = 5000; // wait
unsigned long Delay3 = 7500; // wait
unsigned long currentMillis;
Servo BladeServo;
const byte bladeservoPin = 10;
const byte servoMin = 0;
const byte servoMax = 180;
byte servoPosition = servoMin;
void setup() {
// LEDs and Inputs Setup
pinMode(GreenledPin, OUTPUT);
pinMode(RedledPin, OUTPUT);
pinMode(BlueledPin, OUTPUT);
pinMode(GreenPushButtonPin, INPUT_PULLUP);
pinMode(BladePosPin, INPUT);
pinMode(BottlePosPin, INPUT);
pinMode(SelSwitchPin, INPUT);
// servos setup
BladeServo.attach(bladeservoPin);
BladeServo.write(servoPosition);
//stepper setup
pinMode(A, OUTPUT);
pinMode(A_bar, OUTPUT);
pinMode(B, OUTPUT);
pinMode(B_bar, OUTPUT);
delay(2000) ; //for servo to take position
}
void loop() {
unsigned long currentMillis = millis();
checkInputs();
Start_Process();
if (Start == true){
TurnBlade90Stepper();
if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay1) {
BladeExtractServo();
}
if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay2) {
TurnBlade90Stepper();
}
if(SelSwitchState){
if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay3) {
digitalWrite(BlueledPin, HIGH);
}
}
}
}
void checkInputs() {
GreenPushButtonState = digitalRead(GreenPushButtonPin);
BladePosState = digitalRead(BladePosPin);
BottlePosState = digitalRead(BottlePosPin);
SelSwitchState = digitalRead(SelSwitchPin);
}
void Start_Process(){
if ( BladePosState == HIGH && BottlePosState == HIGH )
{boolean Ready = true; digitalWrite(GreenledPin, HIGH);digitalWrite(RedledPin, LOW);
}
else {digitalWrite(RedledPin, HIGH);digitalWrite(GreenledPin, LOW);
}
if(Ready == true && GreenPushButtonState == LOW){
buttonPushedMillis = currentMillis;
boolean Start = true;
}
}
void TurnBlade90Stepper(){
for (int i = 0; i < (stepsPerRevolution/4) ; i++) {
digitalWrite(A, HIGH);
digitalWrite(A_bar, LOW);
digitalWrite(B, HIGH);
digitalWrite(B_bar, LOW);
delayMicroseconds (x);
digitalWrite(A, LOW);
digitalWrite(A_bar, HIGH);
digitalWrite(B, HIGH);
digitalWrite(B_bar, LOW);
delayMicroseconds (x);
digitalWrite(A, LOW);
digitalWrite(A_bar, HIGH);
digitalWrite(B, LOW);
digitalWrite(B_bar, HIGH);
delayMicroseconds (x);
digitalWrite(A, HIGH);
digitalWrite(A_bar, LOW);
digitalWrite(B, LOW);
digitalWrite(B_bar, HIGH);
delayMicroseconds (x);
}
}
void BladeExtractServo(){
BladeServo.write(servoMax);
BladeServo.write(120);
BladeServo.write(servoMax);
BladeServo.write(120);
BladeServo.write(servoMax);
BladeServo.write(120);
BladeServo.write(servoMax);
BladeServo.write(servoMin);
}