Go Down

Topic: Help in adding a simple counter? (Read 412 times) previous topic - next topic


Apr 11, 2014, 08:54 am Last Edit: Apr 11, 2014, 12:17 pm by 1sennafan Reason: 1
OK so I finally got the LCD shield to read and display. But for the life of me I'm stuck on the simple task of adding a counter to keep track of how many times the shutter has been triggered. I tried to change the button push code to meet my needs but past printing out "Calibrate" for the first 30 seconds  I'm lost. I get numbers going so fast I cant even read them and just for a flash it prints out "Number of Frames" and back to numbers going so fast I cant even read them. I'm wanting it to read "Number of Frames" all the time and just change the frame number.
Can anyone point me in the right direction?

Code: [Select]

#include <multiCameraIrControl.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

// this constant won't change:
const int  FramePin = 12;    // the pin that the Framebutton is attached to
const int ledPin = 13;        // pin that the LED is attached to
// Variables will change:
int FramePushCounter = 0;   // counter for the number of Frame presses
int FrameState = 0;         // current state of the Framebutton
int lastFrameState = 0;     // previous state of the Framebutton
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const int sensor1Pin = A0;      //Red/Gray
const int sensor2Pin = A1;      // White/White  Yellow/Black=Ground
Olympus E510(12); //  (camera model E510) Pin 12 Hot IR LED Red Wire

int sensor1Value = 0;         // the sensor value
int sensor1Min = 1023;        // minimum sensor value
int sensor1Max = 0;           // maximum sensor value
int sensor2Value = 0;         // the sensor value
int sensor2Min = 1023;        // minimum sensor value
int sensor2Max = 0;           // maximum sensor value

void setup() {
   // initialize the button pin as a input:
   pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);   // turn on LED to signal the start of the calibration period:
lcd.begin(16, 2);
  while (millis() < 30000)  // calibrate during the first 30 seconds

    sensor1Value = analogRead(sensor1Pin);
    sensor2Value = analogRead(sensor2Pin);
    if (sensor1Value > sensor1Max) // record the maximum sensor value
      sensor1Max = sensor1Value;
if (sensor2Value > sensor2Max) {
      sensor2Max = sensor2Value;
    if (sensor1Value < sensor1Min)  // record the minimum sensor value
      sensor1Min = sensor1Value;
if (sensor2Value < sensor2Min) {
      sensor2Min = sensor2Value;
  digitalWrite(13, LOW);// signal the end of the calibration period
void loop()
{ lcd.println("on");
       lcd.print("Number of Frames:  ");
  sensor1Value = analogRead(sensor1Pin); // read the sensor:
sensor2Value = analogRead(sensor2Pin);
  sensor1Value = map(sensor1Value, sensor1Min, sensor1Max, 0, 255); // apply the calibration to the sensor reading
sensor2Value = map(sensor2Value, sensor2Min, sensor2Max, 0, 255);
  sensor1Value = constrain(sensor1Value, 0, 255);// in case the sensor value is outside the range seen during calibration
sensor2Value = constrain(sensor2Value, 0, 255);
if (sensor1Value == 0,sensor2Value == 0)

E510.shutterNow(); // trigger the shutter

FrameState = digitalRead(12); // read the  Framebutton input pin:
   if (FrameState != lastFrameState){ // compare the  FrameState to its previous state
     if (FrameState == HIGH){  // if the state has changed, increment the counter
       FramePushCounter++; // if the current state is HIGH then the button // went from off to on:
     else {
       // if the current state is LOW then the button
       // went from on to off:
   // save the current state as the last state,
   //for next time through the loop
   lastFrameState = FrameState;

   // turns on the LED every four button pushes by
   // checking the modulo of the button push counter.
   // the modulo function gives you the remainder of
   // the division of two numbers:
   if (FramePushCounter % 4 == 0) {
     digitalWrite(ledPin, HIGH);
   } else {
    digitalWrite(ledPin, LOW);


pinMode(12, OUTPUT);
FrameState = digitalRead(12); // read the  Framebutton input pin:

Do you think that reading from an output pin makes sense?

BTW: format your code, the IDE offers an automatic code formatter. Use it!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131