Pages: [1]   Go Down
Author Topic: total newbie, can't seem to get a simple code working  (Read 144 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: June 09, 2014, 03:06:52 pm by needahobby » Logged

UK
Offline Offline
Tesla Member
***
Karma: 121
Posts: 6982
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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:
digitalWrite(pumpRelay, digitalRead(pumpSwitch));
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .........
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;
}

  
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: