Jump to content
43oh

Code for water heater monitor.


Recommended Posts

Hello.
This is my first Energia project and one of the first self written pieces of code for a micro controller. 
 
The code is a written for a battery powered water heater monitor that blinks a light every few seconds provided the pilot light is on.  The process will be to turn on a light sensor, wait to see a light and if on the device will go to sleep until the next time.  If no light after a test period, then the device will alarm via a piezo buzzer.  The light sensing and piezo circuit are TBD at this point, until the code works.
 
I am using a MSP430 Launchpad v1.5 board and programming an G2553 chip that came with the Launchpad.  The latest version of Energia on a Windows 7 Starter netbook is used for coding.
 
I The test code uses the green LED to indicate that the test period is on (mimics turning on the sense circuit).  It then waits for SW2 to be pressed (mimics sensor seeing a light).  If SW2 is pressed the micro turns off pins and goes to sleep.  If no input is sensed an alarm is triggered by flashing the red LED (mimics the piezo buzzer).  I use a SW2 ISR to toggle a flag and control the loop().
 
have several questions and I am open to optimizations suggestions.
 
1. I use a flag because sleep within the ISR does not work per Energia guide, and early experiments with the code.  Is there a better way to put the chip to sleep from inside the ISR?  Right now the ISR flag is set but the sensor stays on for the duration of the sense period.  This works, but does not optimize the design for lowest possible power.  The sense period will be set to capture an average of 2 light flashes with margin to ensure a light flash is seen.  This could be reduced if the code would sleep() right after a detection.  How can I do this?  (trying to not go write in C as I still don't quite grasp the C language and intrinsic functions cause me trouble.)
2.  What is a good way to trap the loop()  in the alarm loop?  Do I use a goto command?  (these are frowned upon I hear..)
3.  The PinsOn() function has to use a pinMode(pin, INPUT) function before switching to OUTPUT.  I found that I have to do this to disconnect the pulldown resistor, else I get dim LED's.  This is not entirely addressed in the Energia guide, and only pullup resistors are mentioned.  Is this normal?  Is there a better way to do it?
4. Any other optimization tricks that can be used?
5.  I would like to run the finished sensor from 2x AA batteries without regulation. Is this possible?  What other precautions do I need to take?  Can you point me to a project that did just that?
6.  Is there another good site that gives more examples of Energia?  Other than this site?  Books?
 
Thank you for your time.
Mark P.
 
PS Mods please move if in the wrong forum.
 
CODE==============

// Timer Guard v2
// turns on green led and waits for button push
// if pushed goes to sleep until next time
// else sounds the alarm (red light blinking)
// Sep 2016
// Mark P.


int Flag;  //Flag for determining if button was pressed
const int ledR = RED_LED;                 // Red LED on LaunchPad
const int ledG = GREEN_LED;                 // Green LED on LaunchPad
const int button = PUSH2;                 // Green LED on LaunchPad
unsigned long test_time = 5;                   //test time duration, enter in secons, converts to milis at startup
volatile unsigned long sleep_time = 10;                  //sleep time duration, enter in secons, converts to milis at startup


void setup() {
// put your setup code here, to run once:
 
//Pin Initialization

//set all pins to low power first then change the led pins and switch
for(uint8_t i;i<20;i++){
  pinMode(i,INPUT_PULLDOWN);   //minimize power loop, set all pins to input+pulldown
}

//Variable Setup
test_time *= 1000;             //convert from seconds to milis
sleep_time *= 1000;           //convert from seconds to milis

//ISR
attachInterrupt(button, Interval, FALLING);

}

void loop()   // put your main code here, to run repeatedly:
{

Flag = 0;
PinsOn();  //turn on sense and led pins
digitalWrite(ledG, HIGH);   // turn the LED on to indicate waiting for button press
sleep(test_time); //pause for button press


if (Flag == 0)  //if no button press in allowed time, proceeds to alarm
{
  digitalWrite(ledG, LOW);  //Turn off green LED
    for (int i=0; i <= 5; i++){
      digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)
      sleep(1000);               // wait for a second
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
      sleep(1000);               // wait for a second
     }
}
else {  // if button pressed take a long nap
  PinsOff();  //turn pins off to save power
  sleep(sleep_time);
  }
 
}

void Interval()  //Sleep interval between sensing, activated by button push ISR
{
  Flag = 1;
}

//Function for turning OFF sense pins

void PinsOff() {
pinMode(ledR, INPUT_PULLDOWN);  //red led
pinMode(ledG, INPUT_PULLDOWN);  //green led
pinMode(button, INPUT_PULLDOWN);  //SW2 button
}

//Function for turning on sense pins

void PinsOn(){
  pinMode(ledR,INPUT);  //disconnect pull down resistor
  pinMode(ledR,OUTPUT);  //red led
  pinMode(ledG,INPUT);  //disconnect pull down resistor
  pinMode(ledG,OUTPUT);  //green led
  pinMode(button,INPUT_PULLUP);  //SW2 button
}

Link to post
Share on other sites

HI @@686plus and welcome to 43oh.  Here are some suggestions.

 

  • I'm not sure what happens when you pull GND, 3V3,  etc. low but I wouldn't do it.  Pull P1.0 - P1.7 low and P2.0 - 2.7 low instead.  See the pin map for pin numbers, e.g. for the G2553 the pins are 2,3,4,5,6,...15, 18, and 19.  There are also some hidden pins (port 3) but you probably don't have to worry about them.  Search the site and  you will find more information.  There is also this.
  • You can use suspend() and then inside your ISR use wakeup() instead of a flag. See this example for an understanding of how it works.
  • Don't use goto.  After wakeup() control will return to the first statement after suspend().
  • I would just keep the LED pins as output and turn on and off using digitalWrite().
  • You can run the G2 LaunchPad from batteries without a problem.  After you get everything debugged and flashed, pull the jumper from VCC on the header labelled J3 and then power using JCC and GND on the header labelled J6.  Keep it to less than 3.6 V.
  • There is a book on Energia but I don't have it.  In addition to this site there are examples and projects at energia.nu.  As well, many Arduino projects are easily ported to Energia by just reading the pin map and using the proper pins.
Link to post
Share on other sites

686Plus;

 

Sounds like an interesting project.  I have to admit, though, I'd be tempted to bypass looking for a blinking light (LED? wavelength?) and use a thermocouple or similar, heated by the pilot. I believe the logic would be simpler - use the ADC to check the voltage on your thermocouple. Still put the system on a timer to wake up and test the temp.  FWIW, there are three TI examples for using a thermocouple or thermistor.  Might be worth a look...

 

My very first (non-'430) electronics project was an indoor/outdoor thermometer using a pair of LM35s. It's been up and running for five years now.  Helps me decide at 04:00 if I'm taking the car or the motorcycle to work...

Link to post
Share on other sites

I see I bungled the description, so here is a clarification.

We have a new gas water heater that uses a thermopile(?) to keep the pilot light actuated. To indicate the pilot light is on, the water heater uses excess thermopile electic energy to blink a light on the control that it is running. Light blinks, pilot light on. Light off, pilot light off.

In the rare instance that the pilot light goes off, and the light stops blinking, I want to be notified of this. (Otherwise we have cold water and have to wait an hour to give the kids baths)

 

I want the msp to check that the light is on every few minutes. If light is on, go back to sleep. If light is off, sound the alarm.

 

Fmilburn, I checked on the suspend/wakeup function but I don't think it will work. Based on the guide, suspend can only be canceled by an external input. If the light on the heater goes out, the msp will never see an external input. Correct me if I am not understanding this correctly.

 

NurseBob, I would prefer not to mess with the sealing and function of the gas water heater by adding a thermocouple next to the pilot.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...