Hi. I'm trying to build an object counter using an infrared detector. I have a mega 2560 r3 plus, a 16x2 lcd, and an infrared detector. I have it all wired up with the push button (IR detector) connected to pin 22, and I have used this code: arduino-counter-with-display/arduino_uno_counter_switch.ino at master · eesur/arduino-counter-with-display · GitHub.
Its code for a push button counter using a 7 segment led display. I have substituted the push button for the infrared detector, and the led segment display for my lcd display.
You'll notice I have both lines of the display showing the count. This is because eventually I want to connect two reset buttons and be able to reset each line individually. But I'll worry about that later.
Right now the problem is that it works, but it won't count past 2. I'm a complete novice at coding and I can't for the life of me figure out why.
This is my code:
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int i=0;
void setup() {
lcd.begin(16, 2);
}
// this constant won't change:
const int buttonPin = 22; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void loop() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
// show output on the digital display
lcd.setCursor(1,0);
lcd.print(buttonPushCounter);
lcd.setCursor(1,2);
lcd.print(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
//write somthing to the digital display
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// 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 (buttonPushCounter % 1 == 0) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
}/code]
if (buttonState == LOW) {
// if the current state is HIGH then the button
// wend from off to on:
Even if I change "wend" to "went", the comment does not match the code.
void loop() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Why is this code in loop() instead of in setup() ?
I realize that none of this solves the original problem, but it is annoying.
vaj4088:
Even if I change "wend" to "went", the comment does not match the code.
I believe that is referring to line of code from the original push button counter I started with.
vaj4088:
Why is this code in loop() instead of in setup() ?
I realize that none of this solves the original problem, but it is annoying.
Honestly I have no idea, I think thats how it came from the original code I borrowed. I really need to watch a few c++ tutorials. Would it make a difference if it was in setup()?
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 1 == 0) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
This has little to do with C++, and everything to do with Arduinos.
Anything in setup() gets executed exactly once and at the beginning, so this is useful for initializing pins as inputs, pins as outputs, and serial communications.
Anything in loop() gets executed repeatedly, possibly thousands of times per second depending upon what is coded inside of loop(). This is useful for doing input, doing output, and computing outputs from inputs.
Computing things modulo one is rarely useful.
Using the modulo operator doesn't need a comment. Computing modulo one REALLY needs a comment explaining the purpose of the computation.
Honestly I have no idea, I think thats how it came from the original code I borrowed.
No way. the structure of your code is all wrong and has mangled the basic "StateChangeDetection" example from the IDE at examples>02.Digital>StateChangeDetection. Study the differences between your program and the example. The %1 is actually %4. You may need to add some debounce.
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// 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 (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
cattledog:
No way. the structure of your code is all wrong and has mangled the basic "StateChangeDetection" example from the IDE at examples>02.Digital>StateChangeDetection. Study the differences between your program and the example. The %1 is actually %4. You may need to add some debounce.
I have studied the difference between my code and the example. I changed %4 to %1. I was trying to see if that made any difference to the outcome. It doesn't. It doesn't matter what number I put there, the counter only counts to two.
Forgive my ignorance, but what is debounce and what difference would it make?
PaulMurrayCbr:
What's the remainder of dividing a number by 1?
1 goes into 57 57 times. Remainder zero.
I changed it to %4 to %1 to see if it made any difference, which it doesn't. I've changed it to a bunch of different numbers and it doesn't seem to make any difference, my counter still only counts to two.