Hey guys, I'm pretty new to arduino and coding so I was wondering if you can help me out...
Project:I want two 12 V pumps to turn on and then off after a couple seconds
The problem here is, I want the pumps to turn on for a couple seconds and turn back off. The timedelay command does not work for some reason...
Here is my code, but my code also does this: Motor 1 rotates one direction, the thing rotating hits a microswitch 1, now Motor 1 changes direction and hits microswitch 2 and STOPS. After microswitch 2 is pushed and Motor 1 stops, Motor 2 starts to rotate one direction until it hits Microswitch 3, now the microswitch 3 will cause the Motor 2 to rotate the other direction and hit Microswitch 4 causing it to stop. (I HAVE FIGURED THIS OUT ALREADY)....now after microswitch 4 is pressed, I want two pumps to work for a couple seconds and then turn off..How can i do this?
```
**const int switchpin3 = 9;// the number of the input pin
const int switchpin4 = 8;
const int switchpin1 = 10;// the number of the input pin
const int switchpin2 = 11;
const int ledpin3 = 4; // controls the direction of ice motor
const int ledpin4 = 5; // controls the on/off switch of ice motor
const int ledpin1 = 6; // the number of the output pin
const int ledpin2 = 7;
const int controlpin1 = 31;
const int controlpin2 = 29;
const int controlpin3 = 25;
const int controlpin4 = 27;
const int enablepin1 = 33;
const int enablepin2 = 23;
const int potpin1 = A1;
const int potpin2 = A0;
const int pump1pin = 53;
int state1 = HIGH; // the current state of the output pin
int state2 = HIGH;
int state3 = HIGH;
int state4 = HIGH;
int reading1;
int reading2; // the current reading from the input pin
int reading3;
int reading4;
int previous1 = HIGH;// the previous reading from the input pin
int previous2 = HIGH;
int previous3 = HIGH;// the previous reading from the input pin
int previous4 = HIGH;
int motorspeed1 = 0;
int motorspeed2 = 0;
long time = 0; // the last time the output pin was toggled
long debounce = 300; // the debounce time, increase if the output flickers
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(switchpin1, INPUT);
pinMode(switchpin2, INPUT);
pinMode(switchpin3, INPUT);
pinMode(switchpin4, INPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(ledpin3, OUTPUT);
pinMode(ledpin4, OUTPUT);
pinMode(controlpin1, OUTPUT);
pinMode(controlpin2, OUTPUT);
pinMode(controlpin3, OUTPUT);
pinMode(controlpin4, OUTPUT);
pinMode(enablepin1, OUTPUT);
pinMode(enablepin2, OUTPUT);
pinMode(pump1pin, OUTPUT);
digitalWrite(enablepin1, HIGH);
digitalWrite(controlpin1, LOW);
digitalWrite(controlpin2, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
reading1 = digitalRead(switchpin1);
reading2 = digitalRead(switchpin2);
reading3 = digitalRead(switchpin3);
reading4 = digitalRead(switchpin4);
motorspeed1 = analogRead(potpin1)/4;
motorspeed2 = analogRead(potpin2)/4;
analogWrite(enablepin1, motorspeed1);
Serial.println(state1);
if (reading1 == LOW && previous1 == HIGH && millis() - time > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time = millis();
}
if (reading2 == LOW && previous2 == HIGH && millis() - time > debounce) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time = millis();
}
if (reading3 == LOW && previous3 == HIGH && millis() - time > debounce) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time = millis();
}
if (reading4 == LOW && previous4 == HIGH && millis() - time > debounce) {
if (state4 == HIGH)
state4 = LOW;
else
state4 = HIGH;
time = millis();
}
digitalWrite(ledpin1, state1);
digitalWrite(ledpin2, state2);
digitalWrite(ledpin3, state3);
digitalWrite(ledpin4, state4);
if (state1 == HIGH){
digitalWrite(controlpin1, LOW);
digitalWrite(controlpin2, HIGH);
}
else{
digitalWrite(controlpin1, HIGH);
digitalWrite(controlpin2, LOW);
}
if (state2 == LOW){
digitalWrite(controlpin1, HIGH); //* Motor rotates back and hits the push button to stop
digitalWrite(controlpin2, HIGH); //*
analogWrite(enablepin2, motorspeed2); //
digitalWrite(controlpin3, HIGH); // Ice motor starts rotating here, when cup dispenser stops
digitalWrite(controlpin4, LOW); // change the direction, if it is the wrong one
digitalWrite(pump1pin, HIGH);
}
if (state3 == HIGH){
digitalWrite(controlpin3, HIGH); // Motor hits the push button and changes direction
digitalWrite(controlpin4, LOW); //
}
else{
digitalWrite(controlpin3, LOW); // Motor hits the push button and changes direction
digitalWrite(controlpin4, HIGH); //
}
if (state4 == LOW){
digitalWrite(controlpin3, HIGH);
digitalWrite(controlpin4, HIGH);
}
// if (reading1 == HIGH && previous1 == HIGH && millis() - time > debounce){
// digitalWrite(controlpin1, HIGH);
// digitalWrite(controlpin2, LOW);
// Serial.println(switchpin1);
// }
// else if (reading2 == 1 && previous1 == HIGH && millis() - time > debounce){
// digitalWrite(controlpin1, HIGH);
// digitalWrite(controlpin2, HIGH);
//
// }
// time = millis();
previous1 = reading1;
previous2 = reading2;
previous3 = reading3;
previous4 = reading4;
}**
```