Show Posts
Pages: [1]
1  Topics / Home Automation and Networked Objects / One button control of multi state switch - CODE on: April 05, 2011, 04:48:36 pm
I am developing a sketch that will allow me to control my swamp cooler based on temperature, time of day and manual override.

Here is some code I have written (and tested) that controls the cooler with one button.

Some criteria I have included:

1 button to switch between 5 states (hi/lo speed with & without water pump plus off)

A delay so that the cooler does not switch with every button push, but instead, wait until I have finished pushing buttons to set the new cooler state.

Feedback on the cooler state with 3 LED's (Hi, Lo, Water)

The ability to set any cooler state by sending a char from the host computer via the serial line (USB)

There are several situations where this code/logic could come in handy..

Enjoy

Quote
#define swampon 4
#define swamphi 5
#define swampwa 6
#define button 7
#define redled 8
#define yelled 9
#define grnled 10
#define rlydly 500

int buttonState = LOW;
int coostat = 0;
// 0 -> Off
// 1 -> Low Air
// 2 -> Hi Air
// 3 -> Low Cool
// 4 -> Hi Cool 

void setup() {
  pinMode(swampon, OUTPUT);
  pinMode(swamphi, OUTPUT);
  pinMode(swampwa, OUTPUT);
  pinMode(button, INPUT); 
  pinMode(redled, OUTPUT);
  pinMode(grnled, OUTPUT);
  pinMode(yelled, OUTPUT);
  Serial.begin(9600);
}



void loop() {
  
  if (chkbuttonpress()) {
    coostat = coostat + 1;
    if ( coostat == 5 ) coostat = 0;
    setcooled(coostat);
    for (int i = 0 ; i < 800 ; i++) {
      delay(1);
      if (chkbuttonpress()) {
        coostat = coostat + 1;
        if (coostat == 5) coostat = 0;
        setcooled(coostat);
        i = 0;
      }
    }
    setcooler(coostat);
  }
  
  if (Serial.available() > 0) {
    char incomingByte = Serial.read();
    int myByte = int(incomingByte)-48;
    if (myByte >= 0 && myByte <= 4){
      setcooled(myByte);
      setcooler(myByte);
    }
  }
}


int chkbuttonpress() {
  int gothigh = 0;
  for (int cc = 0; cc < 6; cc++){
    delay(1);
    buttonState = digitalRead(button);
    if (buttonState == HIGH){
      gothigh = 1;
    } else {
      if (gothigh == 1){
        return HIGH;
      } else {
        return LOW;
      }
    }
  }
  return LOW;
}


void setcooled(int newsetting) {
  // All Off
  digitalWrite(redled,LOW);
  digitalWrite(grnled,LOW);
  digitalWrite(yelled,LOW);
  switch(newsetting){
    case 0:
    break;
    case 1:
      ledon(redled);
    break;
    case 2:
      ledon(yelled);
      ledon(redled);
    break;
    case 3:
      ledon(grnled);
      ledon(redled);
    break;
    case 4:
      ledon(grnled);
      ledon(yelled);
      ledon(redled);
    break;
  }
}

void setcooler(int newsetting) {
  // All Off
  digitalWrite(swampon,LOW);
  digitalWrite(swamphi,LOW);
  digitalWrite(swampwa,LOW);
  switch(newsetting){
    case 0:
    break;
    case 1:
      setpin(swampon);
    break;
    case 2:
      setpin(swamphi);
      setpin(swampon);
    break;
    case 3:
      setpin(swampwa);
      setpin(swampon);
    break;
    case 4:
      setpin(swampwa);
      setpin(swamphi);
      setpin(swampon);
    break;
  }
}

void setpin(int pin) {
  digitalWrite(pin,HIGH);
  delay(rlydly);
}

void ledon(int num){
  digitalWrite(num, HIGH);
}

void ledoff(int num){
  digitalWrite(num, LOW);
}



2  Using Arduino / Project Guidance / Re: Need some advice. Shutdown servers when environment changes. on: March 24, 2011, 05:13:09 pm
Thank you all for your helpful replies.  I will be happy to be more detailed, now that I have the answers (I know, I got it backwards, but I have found that providing too much detail up front, people tend not to read...

I have a server room on-site with a dozen servers.  Mostly Linux, but some Windows too. I can write some scripting to query the arduino on a per minute basis, and get temperature and power status.  The UPS will hold for about 20 minutes, so I know not to shutdown until the power has been out for about 10 minutes (hmm gotta think about 10 consecutive 9 minute outages too, I guess).  It is a small closet that is cooled by a standalone AC unit, hooked to another UPS (not my doing).  This UPS is a smart UPS, and the PBX guy stole the signal port for use with his PBX.  I could steal it back, but it will become a back and forth deal, so I will just let him have it.  He won't touch the arduino, because he won't know what it is...

My plan is to plug the arduino into a Linux USB port and let my script do the decision making.  When I decide a shutdown is  needed, I will send a command via ssh to the linux boxes, and a net shutdown command to the windows machines.  Of course, I will get an email and an SMS message long before any shutdown occurs.   I am not far away, so when I am ready, I can visit and bring everybody back up.


I will post the schematic, sketch and scripts when I get it working, so that others can share.

Thanks again for the advice.  I will order some optoisolators just for the heck of it.
3  Using Arduino / Project Guidance / Re: Need some advice. Shutdown servers when environment changes. on: March 24, 2011, 01:26:35 pm
I can get really into providing long explanations, which too often go unread, so I try to keep it short and simple, leaving out info that is not really necessary.

1) Can you provide a keyword I might search on to find this similar solution?  I have tried, and I am not finding it short of reading every post in the forum.

2) Yes there is a UPS, it does not sense temperature, and it is currently hooked up to the PBX, so that is not an option.

Thanks for your quick feedback.  Can you help?
4  Using Arduino / Project Guidance / Need some advice. Shutdown servers when environment changes. on: March 24, 2011, 12:46:13 pm
I have a little school knowledge of circuits, and some programming knowledge in real time systems, so I am not a complete noob.  But this is probably a noob type of question.  I have searched google, and not found a straightforward answer, so I am posting here in hopes that I can get some guidance before I fry my first UNO....

I wish to sense the environment in the server room, temperature and 110 A/C power, then shutdown servers (via USB comm) if the temp gets too hot or the power fails.  The temperature I got, thanks to a great example sketch and affordable parts.

My simple solution for the power question is:

Can I just purchase/scavange a 5 VDC wall wart, and plug it into an unprotected (no UPS) outlet, and wire directly to the arduino digital pins?  When the power fails, the wall wart will drop the pin from +5 to 0, and I can take action as I choose.  My question is should I use a current limiting resistor in the circuit?  Perhaps a 4.7K ohm?  Should I use an optoisolator even if my voltage is only 5VDC?

As an alternative, I thought I could just connect an external 9VDC wall wart to the arduino.  Is there a register I could test in the arduino that would tell me if I am running on USB or external power?  Does plugging in an external adapter to the arduino physically disconnect the USB power?  I see that the arduino power conector has 3 connections, does it disconnect USB power when you use the external connector?  Ideally, I would like the arduino to switch power sorce to USB when the external power fails.

Finally, if there is no register, and the USB power is not physically disconnected, I am thinking of using a voltage divider to drop the 9VDC to 4.5 VDC and sense on an analog pin.  Would this work?

Thanks for your help.  I hope this is not too noob or covered elsewhere.
Pages: [1]