Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Coin Counter Code i...
 
Share:
Notifications
Clear all

Coin Counter Code is not counting

1 Posts
1 Users
0 Likes
309 Views
 Anonymous
Joined: 53 years ago
Posts: 0
Topic starter  

Greetings, I have a bit of a problem.  Firstly, I did not create the code (that is documented in the comments).  When I pass thru the IR, it is not being recorded.  Also, the "Data Cleared' keeps triggering.  

Will provide more info as/if needed. Could not upload the sketch today.

I am using a Arduino Mega, two push buttons and 4 LM393 Chip Motor Measuring Comparator Speed Sensor Module Slot Type IR Optocoupler for MCU Arduino on a board with pinouts as follows:

  • Main Chip : LM393
  • Working voltage:3.3 V to 5 V
  • Output form: digital switch output (0 and 1)
  • Output: Covered, output high level; Without sunscreen, the output low level
  • VCC: Connect the positive 3.3v-5v power supply
  • GND: Connect power negative
  • DO: TTL switch signal output
  • AO: This module does not work, The module do not have

 

buttons are connected to pins 38 and 40.  IR senors are connected from DO to 22,24,26,28 respectively. 

 

Code is as follows:

/* Built to Change
This program gives the user the ability to count their coins
and find out the amount of coins that were counted using an Arduino
based IDE programmed machine.

Created: Oct. 10, 2017
Last Modified: Dec. 2, 2017
Andrew Overby* Alyssa Keyes
Lane Scroggin* Luke Johnson

Copied for reuse
Modified 12/21/2021 New Pgrmr
*/

/* Components used
* Oiyagai LM393 Chip Motor Measuring Comparator Speed Sensor Module Slot Type IR Optocoupler for MCU Arduino
* VCC: Connect the positive 3.3v-5v power supply
GND: Connect power negative
DO: TTL switch signal output
AO: This module does not work, The module do not have

KEYESTUDIO 1602 LCD IIC/I2C/TWI Display 16x2 Character LCD Screen Module for Arduino
GND: A pin that connects to ground
VCC: A pin that connects to a +5V power supply
SDA: A pin that connects to analog port A4 for IIC communication
SCL: A pin that connects to analog port A5 for IIC communication

TWTADE Momentary Push Button Switch 16MM AC 6A / 125V 3A / 250V Mini No Lock Round Switch SPST 2 Pin R13-507-M-5S
Material: Plastic, metal; Terminals: 2 Pin. Model: R13-507-M-5S
Action Type: Momentary Action, Push and Hold - ON, Release - OFF.
Voltage: AC 6A / 125V 3A / 250V

Arduino Mega */

//including LCD screen and EEPROM libraries

#include <EEPROM.h> //EEPROM Arduino memory formatting
/*#include <SPI.h> //LCD screen formatting     //not sure if this is needed*/

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

//declaring pins onArduino

const int photoDime = 22; //photogate to dime
const int photoPenny = 24; //photogate to penny
const int photoNickel = 26; //photogate to nickel
const int photoQuarter = 28; //photogate to quarter

const int resetButton = 38; //reset data button analog
const int lcdSwitch = 40; //data display button analog

//EEPROM integers -tells which byte to store tobyte value;
int addrD = 0; //store addrD at byte location 0
int addrP = 2; //store addrP at byte location 2
int addrN = 4; //store addrN at byte location 4
int addrQ = 6; //store addrQ at byte location 6
int addrTot = 8; //store addrTot at byte location 8
int addrTotCoins = 10; //store addrTotCoins at byte location 10
int addrTotD = 12; //store addrTotD at byte location 12
int addrTotP = 14; //store addrTotP at byte location 14
int addrTotN = 16; //store addrTotN at byte location 16
int addrTotQ = 18; //store addrTotQ at byte location 18

//photogate & button variable sets to 0
int reset = 0;
int pDime = 0;
int pPenny = 0;
int pNickel = 0;
int pQuarter = 0;
int sLCD = 0;

//setting initial variable values for coin calculator
float d;
float p;
float n;
float q;
float tot;
float dFloat;
float pFloat;
float nFloat;
float qFloat;
float totFloat;
int dInt;
int pInt;
int nInt;
int qInt;
int totInt;
int totCoins;
int totd;
int totp;
int totn;
int totq;

//conditional statements for sensor debounce
int D_0 = 500;
int P_0 = 500;
int N_0 = 500;
int Q_0 = 500;
int D = 1000;
int P = 1000;
int N = 1000;
int Q = 1000;

void setup()
{
//tells the Arduino which pins are INPUTS or OUTPUTS

pinMode(photoDime, INPUT);
pinMode(photoPenny, INPUT);
pinMode(photoNickel, INPUT);
pinMode(photoQuarter, INPUT);
pinMode(lcdSwitch, INPUT);
pinMode(resetButton, INPUT);
/*lcd.begin();*/

//screen startups for once power switch or Ardiuno is turned on
/* lcd.begin(); */
Serial.begin(9600); //initialize serial monitoring
lcd.begin(); //LCD parameters (16 characters long by 2 characters wide)
lcd.setCursor(0, 0); //tells LCD where to start the letters at
lcd.print("Coin Sorter"); //places the text on the screen
delay(1500);
lcd.setCursor(0, 1); //creates a new line on the LCD screen
lcd.print("Initializing");
Serial.println("Initializing"); //displays in serial monitor
delay(500); //loading dots
lcd.print(".");
delay(300);
lcd.print(".");
delay(300);
lcd.print(".");

//EEPROM read startup
Serial.println("EEPROM read startup"); //displays in serial monitor
/*retrieves value form arduino memory based on storage location
(can reference location with variables mentioned above*/

EEPROM.get(addrD, dInt);
EEPROM.get(addrTotD, totd);
EEPROM.get(addrP, pInt);
EEPROM.get(addrTotP, totp);
EEPROM.get(addrN, nInt);
EEPROM.get(addrTotN, totn);
EEPROM.get(addrQ, qInt);
EEPROM.get(addrTotQ, totq);
EEPROM.get(addrTot, totInt);
EEPROM.get(addrTotCoins, totCoins);

//Converting EEPROM values so they can be converted from integers to floats
dFloat = (float) dInt;
d = dFloat / 100;
pFloat = (float) pInt;
p = pFloat / 100;
nFloat = (float) nInt;
n = nFloat / 100;
qFloat = (float) qInt;
q = qFloat / 100;
totFloat = (float) totInt;
tot = totFloat / 100;

delay(2000);
lcd.clear(); //clears all characters on the LCD and leaves it blank
lcd.begin(); //re-initializes the LCD for new text
lcd.print("System Ready");
Serial.println("System Ready"); //displays in serial monitor
delay(2000);
lcd.clear();
}

void loop() {
//Total coins --live feed
if (reset == LOW && sLCD == LOW) { /*/// --testing to fix screen noise*/
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("Coins: "); //"coins" will always be displayed without changing
lcd.print(totCoins); //prints the amount for variable totCoins
lcd.setCursor(0, 1);
lcd.print("Amount: $"); //"amount" will always be displayed without changing
lcd.print(tot); //prints the amount for variable tot
delay(20);
}

/*EEPROM code below stores the values in the arduino so it can retrieve them later if it is to be turned off.*/

EEPROM.put(addrD, dInt);
EEPROM.put(addrTotD, totd);
EEPROM.put(addrP, pInt);
EEPROM.put(addrTotP, totp);
EEPROM.put(addrN, nInt);
EEPROM.put(addrTotN, totn);
EEPROM.put(addrQ, qInt);
EEPROM.put(addrTotQ, totq);
EEPROM.put(addrTot, totInt);
EEPROM.put(addrTotCoins, totCoins);

/* to ensure the photgates are reading when the program is running
use the serial monitor to display the values. When the photogate is not
crossed, it should read from 800-1000. When the photogate is crossed,
it should read a value between 0-300 (given that the user wires the
photgate up with the same resistance values)*/

Serial.println(pDime); // -WORKS!
Serial.println(pPenny); // -WORKS!
Serial.println(pNickel); // -WORKS!
Serial.println(pQuarter); // -WORKS!

//resets all current values stored as variables
reset = digitalRead(resetButton); //reads the reset button when pressed
if (reset == HIGH) { //if the button is being pressed > begin this if loop
lcd.clear(); /*variable set below replaces all integer values with 0 to reset the coin values in the arduino system. This is also updated to EEPROM memory.*/

d = 0;
p = 0;
n = 0;
q = 0;
tot = 0;
totCoins = 0;
totd = 0;
totp = 0;
totn = 0;
totq = 0;
dInt = 0;
pInt = 0;
nInt = 0;
qInt = 0;
totInt = 0;
delay(50);

lcd.print("Data Cleared."); //displays text on screen once all data is cleared
delay(2000);
lcd.clear();
} //end if(reset == HIGH) loop */

//the photogates count each coin and adds it to the bank in the code below
pDime = digitalRead(photoDime);
if (pDime < D_0) { //D_0 = 500 & D = 1500
D_0 = 0; //set the threshold of the photogate to zero
d = d + .10; //adds 10 cents to the total dime value in $$
totd = totd + 1; //adds 1 dime to total dime value incoins
tot = tot + .10; //adds 10 cent to the total coin bank value in $$
totCoins = totCoins + 1; //adds 1 coin to the total coin value of the bank
dFloat = d * 100; /*EEPROM conversions -converting the values to an integer instead of a float in order to take up less space */

dInt = (int) dFloat;
totFloat = tot * 100;
totInt = (int) totFloat;
D = 850; //changing variable so photogate can exit the loop
}
if (pDime > D) {
D_0 = 850; //debouncing D = 1000; //setting photogate back to original threshold
} //end if(pDime < D_0) loop

pPenny = digitalRead(photoPenny);
if (pPenny < P_0) {
P_0 = 0; //set the threshold of the photogate to zero
p = p + .01; //adds 1 cent to the total penny value in $$
totp = totp + 1; //adds 1 penny to total penny value in coins
tot = tot + .01; //adds 1 cent to the total coin bank value in $$
totCoins = totCoins + 1; //adds 1 coin to the total coin value of the bank
pFloat = p * 100; /*EEPROM conversions -converting the values to an integer instead of a float in order to take up less space */

pInt = (int) pFloat;
totFloat = tot * 100;
totInt = (int) totFloat;
P = 850; //changing variable so photogate can exit the loop
}
if (pPenny > P) {
P_0 = 850; //debouncing
P = 1000; //setting photogate back to original threshold
} //end if(pPenny < P_0) loop

pNickel = digitalRead(photoNickel);
if (pNickel < N_0) {
N_0 = 0; //set the threshold of the photogate to zero
n = n + .05; //adds 5 cents to the total nickel value in $$
totn = totn + 1; //adds 1 nickel to total nickel value in coins
tot = tot + .05; //adds 5 cent to the total coin bank value in $$
totCoins = totCoins + 1; //adds 1 coin to the total coin value of the bank
nFloat = n * 100; /*EEPROM conversions -converting the values to an integer instead of a float in order to take up less space */
nInt = (int) nFloat;
totFloat = tot * 100;
totInt = (int) totFloat;
N = 500; //changing variable so photogate can exit the loop
}
if (pNickel > N) {
N_0 = 500; //debouncing
N = 1000; //setting photogate back to original threshold
} //end if(pNickel < N_0) loop

pQuarter = digitalRead(photoQuarter);
if (pQuarter < Q_0) {
Q_0 = 0; //set the threshold of the photogate to zero
q = q + .25; //adds 25 cents to the total quarter value in $$
totq = totq + 1; //adds 1 quarter to total quarter value in coins
tot = tot + .25; //adds 25 cent to the total coin bank value in $$
totCoins = totCoins + 1; //adds 1 coin to the total coin value of the bank
qFloat = q * 100; /*EEPROM conversions -converting the values to an integer instead of a float in order to take up less space */
qInt = (int) qFloat;
totFloat = tot * 100;
totInt = (int) totFloat;
Q = 500; //changing variable so photogate can exit the loop
}
if (pQuarter > Q) {
Q_0 = 500; //debouncing
Q = 1000; //setting photogate back to original threshold
} //end if(pQuarter < Q_0) loop

//LCD display commands

sLCD = digitalRead(lcdSwitch); //reads the LCD switch as a digital state
if (sLCD == HIGH) { //if LCD switch is being pressed

lcd.clear();
lcd.begin(); //Dimes
lcd.setCursor(0, 0); //display text below on top line of display
lcd.print("Dimes: "); //"Dimes" will appear on screen
lcd.print(totd); //displays amount of dimes
lcd.setCursor(0, 1); //display text below on bottom line of display
lcd.print("Amount: $"); //"Amount" will appear on screen
lcd.print(d); //displays amount of dimes in $$
delay(2000); //delays for 2 seconds before moving on to the next value
lcd.clear(); //clear LCD

//Pennies
lcd.setCursor(0, 0); //display text below on topline of display
lcd.print("Pennies: "); //"Pennies" will appear on screen
lcd.print(totp); //displays amount of pennies
lcd.setCursor(0, 1); //display text below on bottom line of display
lcd.print("Amount: $"); //"Amount" will appear on screen
lcd.print(p); //displays amount of pennies in $$
delay(2000); //delays for 2 seconds before moving on to the next value
lcd.clear(); //clear LCD

//Nickels
lcd.setCursor(0, 0); //display text below on top line of display
lcd.print("Nickels: "); //"Nickels" will appear on screen
lcd.print(totn); //displays amount of nickels
lcd.setCursor(0, 1); //display text below on bottom line of display
lcd.print("Amount: $"); //"Amount" will appear on screen
lcd.print(n); //displays amount of nickels in $$
delay(2000); //delays for 2 seconds before moving on to the next value
lcd.clear(); //clear LCD

//Quarters
lcd.setCursor(0, 0); //display text below on top line of display
lcd.print("Quarters: "); //"Quarters" will appear on screen
lcd.print(totq); //displays amount of quarters
lcd.setCursor(0, 1); //display text below on bottom line of display
lcd.print("Amount: $"); //"Amount" will appear on screen
lcd.print(q); //displays amount of quarters in $$
delay(2000); //delays for 2 seconds before moving on to the next value
lcd.clear(); //clear LCD
} //end if(sLCD == HIGH) loop
} //end void loop

 


ReplyQuote
Share: