a wee bit of code

Dependencies:   Arduino mbed

Committer:
Nick123
Date:
Wed Jul 29 21:00:51 2015 +0000
Revision:
0:1cc57e14d42f
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nick123 0:1cc57e14d42f 1 #include "mbed.h"
Nick123 0:1cc57e14d42f 2 #include "arduino.h"
Nick123 0:1cc57e14d42f 3 using namespace std;
Nick123 0:1cc57e14d42f 4 DigitalOut myled(LED1);
Nick123 0:1cc57e14d42f 5
Nick123 0:1cc57e14d42f 6 // constants:
Nick123 0:1cc57e14d42f 7 DigitalIn sensorPin1(p5);
Nick123 0:1cc57e14d42f 8 // variables:
Nick123 0:1cc57e14d42f 9 int counter = 0;
Nick123 0:1cc57e14d42f 10 // counter for the number of products passing through
Nick123 0:1cc57e14d42f 11 int sensorState1 = 0;
Nick123 0:1cc57e14d42f 12 // current state of the sensor
Nick123 0:1cc57e14d42f 13 int lastSensorState1 = 0;
Nick123 0:1cc57e14d42f 14 // previous state of the sensor
Nick123 0:1cc57e14d42f 15 // initialize serial communication:
Nick123 0:1cc57e14d42f 16 Serial pc(USBTX, USBRX);
Nick123 0:1cc57e14d42f 17 Serial.baud(9600);
Nick123 0:1cc57e14d42f 18
Nick123 0:1cc57e14d42f 19 int main()
Nick123 0:1cc57e14d42f 20 {
Nick123 0:1cc57e14d42f 21 while(1)
Nick123 0:1cc57e14d42f 22 {
Nick123 0:1cc57e14d42f 23 // read the pushbutton input pin:
Nick123 0:1cc57e14d42f 24 sensorState1 = digitalRead(sensorPin1);
Nick123 0:1cc57e14d42f 25 // compare the buttonState to its previous state
Nick123 0:1cc57e14d42f 26 if (sensorState1 != lastSensorState1)
Nick123 0:1cc57e14d42f 27 {
Nick123 0:1cc57e14d42f 28 // if the state has changed, check to see if it is a change from high to low or low to high
Nick123 0:1cc57e14d42f 29 if (sensorState1 == HIGH)
Nick123 0:1cc57e14d42f 30 {
Nick123 0:1cc57e14d42f 31 // if the current state is HIGH then increment the counter
Nick123 0:1cc57e14d42f 32 counter++;
Nick123 0:1cc57e14d42f 33 Serial.println(counter);
Nick123 0:1cc57e14d42f 34 }
Nick123 0:1cc57e14d42f 35 else
Nick123 0:1cc57e14d42f 36 {
Nick123 0:1cc57e14d42f 37 // if the current state is LOW then the button
Nick123 0:1cc57e14d42f 38 Serial.println(counter);
Nick123 0:1cc57e14d42f 39 }
Nick123 0:1cc57e14d42f 40 }
Nick123 0:1cc57e14d42f 41 // save the current state as the last state,
Nick123 0:1cc57e14d42f 42 //for next time through the loop
Nick123 0:1cc57e14d42f 43 lastSensorState1 = sensorState1;
Nick123 0:1cc57e14d42f 44 //pause to aviod spikes delay(20);
Nick123 0:1cc57e14d42f 45 }
Nick123 0:1cc57e14d42f 46 }