Hello, I am working on a university project on vending machines.
Right now I have a motor that moves back and forward to a bar.
when you push a button the motor gets to a position, stops, activates the solenoid, which pushes a can and then gest back to a home position.
what I really can't do is to tell the motor to get back until the microswitch is hitten and then stop and start reading again the code from the beginning.
here is the code:
#define cw 0
#define ccw 1
int black = 11;
int orange = 10;
int yellow = 8;
int brown = 9;
int button1= 7;
int button2 =6;
int button3 = 5;
int solenoid= 3;
int microswitch= 4;
int currentStep = 0;
void setup() {
pinMode(black, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(brown, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(solenoid, OUTPUT);
pinMode(microswitch,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int state = digitalRead (button1);
if (state==LOW)
{letsStep (ccw,100,20);
delay(1000);
digitalWrite (solenoid,HIGH);}
delay (1000);
homebutton();
}
void homebutton(){
int state=digitalRead (microswitch);
while (state==1)
{letsStep (cw,1,20);
}
}
void letsStep(int direc, int steps, int time){
for(int i=0; i<steps; i++)
{
if (direc==cw)
{
currentStep-=1;
if (currentStep<0)
{
currentStep=3;
}
}
else if (direc==ccw)
{
currentStep+=1;
if (currentStep>3)
{
currentStep=0;
}
}
else
{
Serial.println("Error: Direction not set correctly");
}
if( currentStep==0)
{
digitalWrite(black,LOW);
digitalWrite(brown,HIGH);
digitalWrite(orange,HIGH);
digitalWrite(yellow,LOW);
}
else if( currentStep==1)
{
digitalWrite(black,LOW);
digitalWrite(brown,HIGH);
digitalWrite(orange,LOW);
digitalWrite(yellow,HIGH);
}
else if( currentStep==2)
{
digitalWrite(black,HIGH);
digitalWrite(brown,LOW);
digitalWrite(orange,LOW);
digitalWrite(yellow,HIGH);
}
else if( currentStep==3)
{
digitalWrite(black,HIGH);
digitalWrite(brown,LOW);
digitalWrite(orange,HIGH);
digitalWrite(yellow,LOW);
}
else
{
Serial.println("Error: out of step # range");
}
delay(time);
Serial.println(currentStep);
}
}
I am a novice so please forgive me for the many stupid errors that I might have written .
Thanks for you help