Go Down

Topic: total newbie, can't seem to get a simple code working (Read 181 times) previous topic - next topic

needahobby

Jun 09, 2014, 09:17 pm Last Edit: Jun 09, 2014, 10:06 pm by needahobby Reason: 1
I am trying to set up a pump relay using arduino.  All i want to do is flip a relay on and off using a momentary switch.

my code:

int  pumpRelay   = 13;
int  pumpSwitch  = 2;


void setup()
{
 

 

pinMode (2, INPUT);
pinMode (13, OUTPUT);






}

void loop()
{
 
 
 if(digitalRead(pumpRelay)==LOW && digitalRead(pumpSwitch) == HIGH)
   {
   digitalWrite(pumpRelay, HIGH);
   }
   
   else
   
   if(digitalRead(pumpRelay)==HIGH && digitalRead(pumpSwitch) == HIGH)
   {
   digitalWrite(pumpRelay, LOW);
   }
   

 
 }


Any help is appreciated

Robin2

Why are you reading the relay pin. Surely you can remember whether it is  high or low?

All you need to read is the switch pin.

It may be useful if you draw a diagram of how everything is connected and post a photo of the drawing. An Arduino can't supply enough power for a relay (unless you have a solid state relay).

...R

Delta_G

The code you have written will continuously toggle the pumpRelay on-off-on-off-on-off for a few microseconds at a time as long as the switch is HIGH.  Is that what you wanted?

Maybe more appropriate would be something along the lines of...

Code: [Select]


if(digitalRead(pumpSwitch) == HIGH {
    digitalWrite(pumpRelay, HIGH);
}
else {
    digitalWrite(pumpRelay, LOW);
}




Which could further be condensed to get rid of the if statement entirely as ...

Code: [Select]

digitalWrite(pumpRelay, digitalRead(pumpSwitch));

needahobby

As i indicated, i am using a momentary switch.  When i hit the switch, i want the pump relay pin to go to HIGH. When i release the switch i want the relay pin to remain on HIGH.   Now, if i hit the switch again, I want to pump relay pin to go to LOW.

Basically every time i hit the switch, I want the pump relay pin to toggle from HIGH to LOW to HIGH to LOW .........

Delta_G

In that case, you also need to keep up with the state of the pump.  How about something like this.

Code: [Select]

int pumpRelay = 13;
int pumpSwitch = 2;
int pumpState = LOW;
int lastButtonState = LOW;

void setup(){
 pinMode(pumpRelay, OUTPUT);
 pinMode(pumpSwitch, INPUT);
}

void loop(){
 
 int buttonState = digitalRead(pumpSwitch);
 
 if (buttonState != lastButtonState){
   if (buttonState == HIGH){    // if button is pressed
     pumpState = !pumpState;
   }                           // else buttonState LOW, do nothing
 }
 digitalWrite(pumpRelay, pumpState);
 lastButtonState = buttonState;
}

 

PeterH


When i hit the switch, i want the pump relay pin to go to HIGH. When i release the switch i want the relay pin to remain on HIGH.   Now, if i hit the switch again, I want to pump relay pin to go to LOW.

Basically every time i hit the switch, I want the pump relay pin to toggle from HIGH to LOW to HIGH to LOW .........


In that case you need to look at the state change example - it shows you how to take action when an input changes state, which is what you want here. (The action in this case will be to invert the state of the output pin every time the input changes from 'not pressed' to 'pressed'.)
I only provide help via the forum - please do not contact me for private consultancy.

Go Up