Hello all,
I have been tasked with creating a part counter. They way in which I have made it so far is by using an arduino with an Ultrasonic sensor, along with a keypad and an LCD. Right now, I am using state detection to tell whether or not something is in the range or not in the range of the sensor. I want it to add to my count by one every time an object is detected in its range. Right now sometimes the count will go up by more than one when an object enters the range. How do I fix this? It almost feels as though the sensor doesn't know what to detect. Would an IR sensor work for this instead?
#include <Keypad.h> //libraries
#include <Adafruit_LiquidCrystal.h>
static int count = 0; //variable used as first number
Adafruit_LiquidCrystal lcd_1(0);
bool sensorState = true;
//used for the 4x4
const byte ROWS = 4;
const byte COLS = 4;
bool blinking = false; // if TRUE, this will turn blinking on
//Pins for UltraSonic Sensor
const int TRIG = 13;
const int ECHO = 12;
long duration; // sets time used for UltraSonic Sensor
int cmdistance; //Gives the distance from sensor in cm
//used in state detection for the sensor to trigger countUp()
int buttonState=0;
int lastButtonState=0;
int buttonPushCounter=0;
//tells the location the part number on the LCD
void partLoc(){
lcd_1.setCursor(7,1);
}
//tells the location of the fab cell number on the LCD
void fabLoc(){
lcd_1.setCursor(11,0);
}
//clears the lcd screen of numbers
void clearScreen(){
lcd_1.print(" ");
}
//counts up the part number
void countUp(){
++count;
partLoc();
clearScreen();
partLoc();
lcd_1.print(count);
for (int i = 7; i <= 16; i++) { //turns off blinking in LCD spots 7-16
blinking=false;
lcd_1.setCursor(i,1);
}
}
//counts down the part number
void countDown(){
if(count!=0){
--count;
partLoc();
clearScreen();
partLoc();
lcd_1.print(count);
partLoc();
lcd_1.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd_1.setCursor(i,1);
}
}
}
//tells which buttons are the inputs for the 4x4
char hexaKeys [ROWS][COLS] ={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//tells which pins are used for the 4x4
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{ pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
lcd_1.print("Fab Cell #");
lcd_1.setCursor(0,1);
lcd_1.print("Part #");
lcd_1.setCursor(11,0);
lcd_1.blink();
}
void loop(){
char customKey = customKeypad.getKey();
// state detection system, detects how often an object is removed from the point of view
//of the sensor. Triggers count up every other time an input is read.
if (cmdistance ==10){
buttonState = 1;
}else{
buttonState=0;
}
if (buttonState != lastButtonState) {
if (buttonState == 1) {
buttonPushCounter++;
//Serial.println("on");
//Serial.print("number of button pushes: ");
//Serial.println(buttonPushCounter);
} else {
//Serial.println("off");
if (buttonPushCounter % 1 == 0){
countUp();
Serial.println(count);
}
;
}
lastButtonState = buttonState;
delay(50);
}
//determines the distance away an object is, used in the state detection system
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
cmdistance = duration * (0.034 / 2)+1;
//Serial.print("Distance in cm: ");
//Serial.print (cmdistance);
//Serial.println();
if (blinking){
lcd_1.blink();
}
//Switchcase Statements
switch (customKey){
case 'C':
countUp();
break;
case 'D':
countDown();
break;
case '#':
fabLoc();
clearScreen();
fabLoc();
blinking = true;
break;
}
// Allows button input from buttons 0 - 9
if ((customKey>='0') && (customKey <= '9')){
lcd_1.print(customKey);
for (int i = 7; i <= 16; i++) {
blinking=false;
lcd_1.setCursor(i,1);
}
}
//When B is pressed the count resets to zero
if (customKey=='*'){
count=0;
partLoc();
lcd_1.print(count);
lcd_1.setCursor(8,1);
lcd_1.print(" ");
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd_1.setCursor(i,1);
}
Serial.print("good");
}
}