Different software approaches to the same hardware

Recently i have been trying to become better at writing microcontroller code.

So i built a small shield i can use to test various concepts in software.  It has an LDR to sense light levels,a PIR movement detector and an LED.

Over the years as I have tried to learn to program, i have tried lots of different approaches to writing code that is robust and simple. If the code doesnt run properly it can leave the hardware needing a reboot or feeling glitchy.

Usually it starts with a bunch of sensors that i have, with some sort of output device like a relay.

 

I am by no means a very good programmer but i know enough to get myself into trouble.  I know how to copy and modify others examples. I am always ready to have a go at it myself and write something from start to finish, even if i do make a mess of it.

I have read a book on programming once, it gave me two good tips; one was to write up an algorithm that described just what decision the program was expected to make and the flows of logic that derive from this. The second tip was to try to begin the coding by putting all the comment code in first. Re-describing with your comments the algorithm you previously came up with.

All good and well but how you treat the algorithim at the start makes a big impact on the way the software is written. For example;

Should i create a loop that reads a sensor then makes a decision whether to drive an output straight a way? only leaving the loop when predefined conditions are met? Meaning i can just sit there polling the sensor and wait for the event, while also unable to do anything else?

Or should i poll all of the sensors at some sort of refresh rate then make a decision based on which flags are set by the sensors? Using a lot of resources continuously but making sure i don’t miss anything and have all the data before i make a decision?

If its just one sensor and one output i would always write one loop to check the sensor then drive the output.  the trouble comes when you want to expand. What i find is that the more sensors there are and the more detailed the decisions then the polling with flags makes more sense.

Now its the hardware making the early decisions in the software. Will there be special IC’s with there own protocols? like IC2, SPI and serial just to name a few.  Some bits of gear I only want to use the library that i can get for it, because i can’t write the hard code to talk to it properly.

So i am going to try various software approaches to making a movement activated night light.

 

The various pictures are of my first go at the hardware and the listing given below is my attempt at a polling type program.

There is one delay used while waiting for dark.  I have noticed that if I poll a day night sensor too quickly, then as darkness approaches the sensor will oscillate back and forth between dark and light making for some crazy switching of the output.  I needed to write a routine that would take care of it.  I  guess the best way to go about it would be to sample the light and wait till another sample some time later shows a fall by a certain amount. But in this situation we cant do anything until its dark anyway so why not just put a short delay between looking at the light levels and switching the output, that stops the oscillation.

The trouble is, we cant do anything while delayed in the loop.  This becomes a problem when we try to expand the program or want to check on the other sensors.

 

/*
Day night IR sensor shield 11/08/12 Trobb
*/
#include <avr/wdt.h>
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting

int ir = 8; // IR movement sensor pin
int ldr = A0; // LDR light sensor
int led = 9; // Output for LED light

void setup() {

// initialize the IO.

pinMode(led, OUTPUT);
pinMode(ir, INPUT);
pinMode(ldr, INPUT);

digitalWrite(led, LOW); // start with LED off
}

void loop() {

wdt_enable(WDTO_2S); // start a watchdog in case we crash!

digitalWrite(led, LOW); // keep LED off

if(analogRead(ldr)<=250 && digitalRead(ir)== 1){ // so long as its dark and movement is detected

digitalWrite(led, HIGH); // turn led on
}
wdt_reset(); // made it through the loop OK reset watchdog
}

No Comments Yet.

Leave a comment