hello every one
I am new here and in electronic, please forgive my english, or repost.
I have a project with arduino nano. wiring scheme:
and the sketch:
// Counters of goods stored on the conveyor slot. Each slot contains 1 pair
#include <Button.h>
#include <SevenSegmentExtended.h>
#include <SevenSegmentTM1637.h>
// Nano pin assignment
#define CLK 3
#define DIO 4
#define BtnUpPin 5
#define BtnDnPin 6
#define BtnResPin 7
#define BtnSetPin 8
#define BuzzPin 9
// button parameter
#define PULLUP true
#define INVERT true
#define DEBOUNCE_MS 20
// declare variable
int pair = 0;
int dozen = 0;
int total = 0;
/* To give a warning (beep) every time reach setberrier
* then empty the goods slot.
*/
int setBerrier = 8;
int berrier = 0;
bool setState = false;
SevenSegmentExtended display(CLK, DIO);
Button BtnUp(BtnUpPin, PULLUP, INVERT, DEBOUNCE_MS);
Button BtnDn(BtnDnPin, PULLUP, INVERT, DEBOUNCE_MS);
Button BtnRes(BtnResPin, PULLUP, INVERT, DEBOUNCE_MS);
Button BtnSet(BtnSetPin, PULLUP, INVERT, DEBOUNCE_MS);
void setup() {
// put your setup code here, to run once:
// init display
display.begin();
display.setBacklight(50);
pinMode(BuzzPin, OUTPUT);
display.setPrintDelay(200);
display.print("DOZEN COUNTER -88-");
display.blink(500,2);
display.clear();
display.setColonOn(true);
display.printDualCounter(dozen, pair); // normal/counting mode
beep(50);
beep(50);
delay(100);
}
void loop() {
// put your main code here, to run repeatedly:
BtnUp.read();
BtnDn.read();
BtnRes.read();
BtnSet.read();
/* Reset button is used every 1 hour of work to display a dozen values back to 0
and save a dozen to total.
*/
if (BtnRes.wasPressed()) {
if (setState == false) { // counting mode
total = total + dozen;
dozen = 0;
beep(50);
display.setColonOn(false);
display.clear();
display.print(total); // show total for a moment
display.blink(500,5);
delay(2000);
display.setColonOn(true);
display.printDualCounter(dozen, pair);
}
else { // set mode
display.setColonOn(false);
display.clear();
display.print(setBerrier);
display.setCursor(0,2);
display.print("01");
display.blink(500,3);
display.setColonOn(true);
display.printDualCounter(dozen, pair);
display.blink(500,3);
setState = false; // back to counting mode
}
}
if (BtnUp.wasPressed()) {
if (dozen <=99) {
if (setState == false) {
berrier+=1;
pair+=1;
if (pair == 12){ // if dozen reach
pair = 0;
dozen+=1;
display.printDualCounter(dozen, pair);
if (berrier != setBerrier) beep(50);
}
else display.printDualCounter(dozen, pair);
if (berrier == setBerrier) {
beep(50);
beep(50);
berrier = 0;
}
}
else {
if (setBerrier <= 11) {
setBerrier+=1;
display.clear();
display.print(setBerrier);
}
}
}
}
if (BtnDn.wasPressed()) {
if (setState == false) {
if (dozen != 0) {
if (pair == 0) {
pair = 11;
dozen-=1;
}
else pair-=1;
}
else {
if (pair != 0) pair-=1;
}
display.printDualCounter(dozen, pair);
}
else {
if (setBerrier >= 6) {
setBerrier-=1;
display.clear();
display.print(setBerrier);
}
}
}
if (BtnSet.wasPressed()) {
if (setState == false) {
setState = true;
display.setColonOn(false);
display.clear();
display.print(setBerrier);
}
else {
display.setColonOn(false);
display.clear();
display.print(total);
display.blink(500,5);
delay(1000);
display.setColonOn(true);
display.printDualCounter(dozen, pair);
setState = false;
}
}
}
void beep(unsigned char delayms) {
digitalWrite(BuzzPin, HIGH);
delay(delayms);
digitalWrite(BuzzPin, LOW);
delay(delayms);
}
My Problem:
due to long term use, I need power adapter to power my project. I have a used adapter with 9v 600mA output.
But if using this adapter, the btnUp button often presses itself (if (btnUp.wasPressed())) randomly. The problem is often, so it is very disturbing the accuracy of the counting and the real counting.
i have 2 option to using baterry with regulator 5v via 5v pin, and 9v battery via vin pin. Using this power (battery), my project runs quite smoothly, but still have interference of btnUp button. but not often. still annoying. :). for 8 hours used, approximately 5 times random interference BtnUp occur. That's what's caught on display, what about the unconscious count has increase by itself?
what's the solution? Is there anything in the hardware or software that I need to fix, Is there an article link I should read or an incorrect power source issue? Please help.
Thank you