9 Jan 2011

Arduino Lightmeter - the code

As you can see, the program is very short. an explanation follows the code:


#include LiquidCrystal lcd(13, 12, 5, 4, 3, 0);


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  pinMode(0,INPUT);
  pinMode(1,INPUT);
  // Print a message to the LCD.
}


int sensor1Value, sensor2Value;
void loop() {
  if(digitalRead(1)){sensor1Value = analogRead(A0);} // read the switch, overwrite stored LDR value
  if(digitalRead(2)){sensor2Value = analogRead(A0);} // read the switch, overwrite stored LDR value
  lcd.setCursor(0, 0); // set the cursor to first line
  lcd.print("s1: ");
  lcd.print((int) (sensor1Value*(10000.00/1023.00)), DEC);
  lcd.setCursor(0, 1); // set the cursor to next line
  lcd.print("s2: ");
  lcd.print((int) (sensor2Value*(10000.00/1023.00)), DEC);
  delay(100); // the delay slows down the refresh of the display (lcd.clear in the loop) to improve readability.
 } 


First of all, we need to include the LiquidCrystal library to give us access to the functions some other altruistic geek has written for us. Immediately following that - and outside of main() and loop(), so that it is in the global scope (it can be accessed from anywhere) - the instance of lcd is initialised with the output pins used on the Arduino, so we don't need to set them later. If you look at the numbers here, you'll see they correspond to the digital pins connected to on the Arduino board. There's no magic in which numbers to use - they're just like that to show that you can use whichever pins you want and that control lies within the sketch. 


In setup we call begin on lcd which defines the number of columns and rows in the display. Next we tell the Arduino that we'll be using pins 1 & 2 for input - these are the push-switches. Remember that the lcd library set the outputs for the display so we don't need to.


Again in the global scope, so that they don't get redefined by every iteration of loop(), we declare the variables sensor1Value and sensor2Value.

Now onto loop() - the centre of operations:

if(digitalRead(1)){sensor1Value = analogRead(A0);}
this instruction says, "If the switch on pin 1 is pressed, overwrite the value in sensor1Value with the new value read from analogue input 0 (A0) by function analogRead(). The following line does the same for the switch on pin 2.


The remainder of the sketch is concerned with writing the information to the display.
First we need to clear the display to avoid interference from previously written data. Then we construct the string to write for each line of the display. s1: and s2: correspond to the switches. The calculation converts the digital value into something vaguely meaningful. The Arduino can measure an analogue input in 1024 steps. I was playing with the pull-down value on the LDR, so I divided that by 1024 to give a value to each step. I don't remember what the meaning was, though.  Anyway, we multiply that by the measured input and print it to the display. Finally, I added a delay because I was getting some flicker.


Please try this out and let me know how you get on! If you're feeling nerdy you could even buy a shirt or hoody from RedBubble and make me a little money.

1 comment:

  1. Sorry about that, the code was wrong. I've corrected it now, but I'll soon be posting a new version anyway.

    ReplyDelete