HI Guys,
Here's my code
int main = 2;
int p1= 3;
int p2 = 4;
int p3 = 5;
int p4 = 6;
int up=8;
int down=9;
int front =10;
int back= 11;
void setup()
{
Serial.begin (9600);
pinMode (up, OUTPUT);
pinMode (down, OUTPUT);
pinMode (front, OUTPUT);
pinMode (back, OUTPUT);
pinMode (main, INPUT_PULLUP);
pinMode (p1 , INPUT_PULLUP);
pinMode (p2 , INPUT_PULLUP);
pinMode (p3 , INPUT_PULLUP);
pinMode (p4 , INPUT_PULLUP);
}
void loop()
{
if (digitalRead (main)==LOW)
{
digitalWrite (down,LOW);
Serial.println("down") ;
}
else
if (digitalRead (p1)==LOW)
{
digitalWrite (down,HIGH);
digitalWrite (up,LOW);
Serial.println("up") ;
}
else
if (digitalRead (p2)==LOW)
{
digitalWrite (up,HIGH);
digitalWrite (back,LOW);
Serial.println("front") ;
}
else
if (digitalRead (p3)==LOW)
{
digitalWrite (back,HIGH);
digitalWrite (front,LOW);
Serial.println("front") ;
}
else
if (digitalRead (p4)==LOW)
{
digitalWrite (front, HIGH);
digitalWrite (down,LOW);
Serial.println("front") ;
}
}
Problem is all the relays turn low or else board gets confused.
There is 5 input and 4 output.
I need to turn on 4 relays in an systematic order.
Whereas my main (pin=2) must be the start.
Followed by rest of the codes.
but the thing is all the buttons are working if the dont go in order also.
if i press p2 and then p4 if works.
but i want the process to be going in step by step. What needs to be done,
This is code for controlling small machines using relays.'
So help out guys.
Thanks and regards
Vinod Kumar
Welcome to the Arduino forum!
Did you test each part of your program as you wrote it? Did it all work correctly until the last change?
Paul
Not sure I understand what you are wanting to do. Do you want to force the buttons to be pushed in the following order? main, p1, p2, p3, p4. In other words if I push p1 before main then p1 would have no effect and so on.
I see a few potential issues in your code.
-
Since those are mechanical buttons you need to debounce them to make sure they are in a stable state. Search for debounce. There is a lot of discussion on the subject.
-
The way it is coded if you hold a button down it always executes the corresponding conditional. You may want to look for a HIGH to LOW transition.
-
You should probably initialize your outputs to a known state
You need to keep track of where you are in the sequence and check only the button that is supposed to move4 you to the next step:
const byte mainButtonPin = 2;
const byte p1 = 3;
const byte p2 = 4;
const byte p3 = 5;
const byte p4 = 6;
const byte up = 8;
const byte down = 9;
const byte front = 10;
const byte back = 11;
void setup()
{
Serial.begin (9600);
digitalWrite(up, HIGH);
pinMode (up, OUTPUT);
digitalWrite(down, HIGH);
pinMode (down, OUTPUT);
digitalWrite(front, HIGH);
pinMode (front, OUTPUT);
digitalWrite(back, HIGH);
pinMode (back, OUTPUT);
pinMode (mainButtonPin, INPUT_PULLUP);
pinMode (p1 , INPUT_PULLUP);
pinMode (p2 , INPUT_PULLUP);
pinMode (p3 , INPUT_PULLUP);
pinMode (p4 , INPUT_PULLUP);
}
void loop()
{
static byte state = 0;
switch (state)
{
case 0:
// make sure all the relays are off.
digitalWrite(up, HIGH);
digitalWrite(down, HIGH);
digitalWrite(front, HIGH);
digitalWrite(back, HIGH);
if (digitalRead (mainButtonPin) == LOW)
{
state = down;
digitalWrite (down, LOW);
Serial.println("down") ;
}
break;
case down:
if (digitalRead (p1) == LOW)
{
state = up;
digitalWrite(up, LOW);
digitalWrite(down, HIGH);
digitalWrite(front, HIGH);
digitalWrite(back, HIGH);
Serial.println("up") ;
}
break;
case up:
if (digitalRead (p2) == LOW)
{
state = back;
digitalWrite(up, HIGH);
digitalWrite(down, HIGH);
digitalWrite(front, HIGH);
digitalWrite(back, LOW);
Serial.println("back") ;
}
break;
case back:
if (digitalRead (p3) == LOW)
{
state = front;
digitalWrite(up, HIGH);
digitalWrite(down, HIGH);
digitalWrite(front, LOW);
digitalWrite(back, HIGH);
Serial.println("front") ;
}
break;
case front:
if (digitalRead (p4) == LOW)
{
state = down;
digitalWrite(up, HIGH);
digitalWrite(down, LOW);
digitalWrite(front, HIGH);
digitalWrite(back, HIGH);
Serial.println("down") ;
}
break;
}
}