Engineering Projects/InkJetHack/button


/*
  InkJetEncoderCount
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when moving inkjet printhead.  This light is blurry,  so
 added counter and serial port monitor output.
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * one of the outputs of the inkjet encoder is attached to pin 2
 
 created 2012
 by Scott Foerster 
 based upon the Button Example
 This example code is in the public domain.
 
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the encoder output is pushed into
const int ledPin =  13;      // the number of the LED pin
int Cnt = 0; //counting number of shadows

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
   // initialize the serial communications:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the encoder output:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the encoder output
  buttonState = digitalRead(buttonPin);

  // check if the encoder is HIGH.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    Cnt++;
    // print the counter value:
    Serial.print(Cnt);
    Serial.println();
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}