Coin Sorter/Counter – Code

Main Arduino Code

#include<printing.h>                           //custom library
#include<LiquidCrystal.h>

LiquidCrystal lcd(12,13,8,9,10,11);            //sets pins for lcd
printing p();
#define SENSORPIN 2                            //dime
#define SENSORPIN2 3                           //nickel
#define SENSORPIN3 4                           //quarter

int sensorState = 0;
int lastState = 0;
int sensorState2 = 0;
int sensorState3 = 0;
int sum;                                                                             // keeps track of current money total
void setup() {      
  pinMode(SENSORPIN, INPUT);                  //initialize sensor as input 
  pinMode(SENSORPIN2, INPUT);                 //initialize sensor as input
  pinMode(SENSORPIN3, INPUT);                 //initialize sensor as input     
  digitalWrite(SENSORPIN, HIGH);              //set initial value for sensor as HIGH
  digitalWrite(SENSORPIN2, HIGH);             //set initial value for sensor as HIGH
  digitalWrite(SENSORPIN3, HIGH);             //set initial value for sensor as HIGH
  
  Serial.begin(9600);
}
 
void loop(){
  sensorState = digitalRead(SENSORPIN);        //reads sensor value
  sensorState2 = digitalRead(SENSORPIN2);      //reads sensor value
  sensorState3 = digitalRead(SENSORPIN3);      //reads sensor value
 
  if (sensorState == LOW) {                    //checks if sensor 1 beam is broken    
    delay(500);
    sum += 10;                                 //adds 10 to the current total
    displayLCD();
    p.printingDime();
 
  } 

   if (sensorState2 == LOW){                    //checks if sensor 2 beam is broken
    delay(500);
    sum += 5;                                   //adds 5 to current total
    displayLCD();
    p.printingNickel();
  }
  if(sensorState3 == LOW) {                     //checks if sensor 3 beam is broken
    delay(500);
    sum += 25;                                  //adds 25 to current total
    displayLCD();
    p.printingQuarter();
  }

  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}
void displayLCD(){                                                             //function to display total value
  lcd.clear();
  lcd.print("Total:");
  lcd.print(sum);
}

library.cpp – Our custom code library

#include "printing.h"
#inlcude "arduino.h"
#include <stdio.h>
printing::printing()
void printing::printingDime(){
	Serial.println("Break - Dime");
}
void printing::printingQuarter(){
	Serial.println("Break - Quarter");
}
void printing::printingNickel(){
	Serial.println("Break - Nickel");
}

library.h – Header file for our custom code library

#ifndef printing_h
#define printing_h

class printing
{
	public:
		printing();
		void printingDime();
		void printingQuarter();
		void printingNickel();
		
}

#endif

These are the three files we used to code the project, the first being the main Arduino file, the second being the custom library used in our main Arduino file, and lastly the header file used for the custom library. This code is responsible for detecting breaks in the IR break beams and adding to the count of the total amount in coins that has been sorted. The code for the IR breaks was mostly original with help from the Adafruit documentation/learning section specifically for the IR break beam sensors we used.

Smart House

ARDUINO CODE:

/*
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
3 INT Interrupt
8 – Speaker
*/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT 3 // Needs to be an interrupt pin
#define Speaker_PIN 8 // Speaker

// Constants
#define PROX_INT_HIGH 50 // Proximity level for interrupt
#define PROX_INT_LOW 0 // No far interrupt

// Global variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint8_t proximity_data = 0;
int isr_flag = 0;

void setup() {

// Set speaker and communication as output
pinMode(8, OUTPUT);
pinMode(APDS9960_INT, INPUT);
pinMode(7,OUTPUT);

// Initialize Serial port
Serial.begin(9600);
Serial.println(F(“Speaker is online!”));
// Initialize interrupt routine
attachInterrupt(0, interruptRoutine, FALLING);

// Initialize proximity sensor
if ( apds.init() ) {
Serial.println(F(“APDS-9960 initialization complete”));
} else {
Serial.println(F(“Something went wrong during APDS-9960 init!”));
}

// Adjust the Proximity sensor gain
if ( !apds.setProximityGain(PGAIN_2X) ) {
Serial.println(F(“Something went wrong trying to set PGAIN”));
}

// Set proximity interrupt thresholds
if ( !apds.setProximityIntLowThreshold(PROX_INT_LOW) ) {
Serial.println(F(“Error writing low threshold”));
}
if ( !apds.setProximityIntHighThreshold(PROX_INT_HIGH) ) {
Serial.println(F(“Error writing high threshold”));
}

// Start running the proximity sensor
if ( apds.enableProximitySensor(true) ) {
Serial.println(F(“Proximity sensor is now running”));
} else {
Serial.println(F(“Something went wrong during sensor init!”));
}
}

void loop() {

// If interrupt occurs, print out the proximity level
if ( isr_flag == 1 ) {

// Read proximity level and print it out
if ( !apds.readProximity(proximity_data) ) {
Serial.println(“Error reading proximity value”);
} else {
Serial.print(“Proximity detected! Level: “);
Serial.println(proximity_data);
}

// Turn on speaker play tone at 523 Hz for 300 ms

tone(8, 523, 300);
Serial.println(“HI”);

// Reset flag and clear APDS-9960 interrupt (IMPORTANT!)
isr_flag = 0;

digitalWrite(7,LOW);
if ( !apds.clearProximityInt() ) {
Serial.println(“Error clearing interrupt”);
}

}
}

void interruptRoutine() {
digitalWrite(7,HIGH);

isr_flag = 1;

}