Resetting a Displayed Value via Switch

#include <LiquidCrystal.h>
#include <Servo.h>

Servo myservo;
Servo stopper;

const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12; // initialize the library by associating any needed LCD interface pin
                                                              // with the arduino pin number it is connected to
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int sensorValue1;          // variable for the sensors outputted value
int sensorValue2;
int sensorValue3;
int sensorValue4;
int sensorValue5;
int sensorValue6;

bool processed = false;

int dollars = 0;          // variable for the total output, dollars and cents
int cents = 0;

int dimes = 0;          // variable for the coin amounts
int nickles = 0;
int quarters = 0;
int loonies = 0;
int toonies = 0;


int printSpot = 3;

int screenState = 0;            // variable for subroutine actions

int startstoppin = 5;
int basinpin = 3;

// for the starting and stopping of the basin servo
int basinState = HIGH;                        // current state of output pin
int controlState;                            // current reading from input pin
int lastControlState = HIGH;                   // previous reading from input pin
long time = 0;                                // last time the output pin was toggled
long debounce = 300;                         // debounce time, higher is the output is flickering

// for the resetting of the total amount
int 

unsigned long onDelayVariable = 0;           // a delay for screen effects
unsigned long onDelay = 700;            // the larger the number here equals larger delay


unsigned long lastDebounceTimeScreen = 0;
unsigned long debounceDelayScreen = 400;      // the debounce time

void setup() {
  Serial.begin(9600);

pinMode(startstoppin, INPUT);                  // sets up the input and output pins for basin servo control
pinMode(basinpin, OUTPUT);


                  // identifies the servo pin on the Arduino
  stopper.attach(2);
  stopper.write(150);
  delay(200);
  stopper.detach();

    lcd.begin(16, 2);                     // set up the LCD's number of columns and rows.
  lcd.print("Total Amount");              // Print a message to the LCD.
  lcd.setCursor(0, 1);                // set the cursor to column 0, line 1
  lcd.print("$");                // print the number of total money value
  lcd.print(dollars);
  lcd.print(".");
  if(cents<10) {
    lcd.print("0");
  }
  lcd.print(cents);

}

void loop() {
 // push buttons will go here
                 // detaches to avoid random movements

startStopBasin();
totalReset();

if(screenState == 0) {
  LCD();
}
  
  Serial.println("Sensor Inputs:");
    sensorValue1 = analogRead(A0);                      //reads analog inputs
    sensorValue2 = analogRead(A1);
    sensorValue3 = analogRead(A2);
    sensorValue4 = analogRead(A3);
    sensorValue5 = analogRead(A4);
    sensorValue6 = analogRead(A5);


  Serial.print("Sensor 1 = ");                        // prints sensor values for debugging
  Serial.println(sensorValue1);
  Serial.print("Sensor 2 = ");
  Serial.println(sensorValue2);
  Serial.print("Sensor 3 = ");
  Serial.println(sensorValue3);
  Serial.print("Sensor 4 = ");
  Serial.println(sensorValue4);
  Serial.print("Sensor 5 = ");
  Serial.println(sensorValue5);
  Serial.print("Sensor 6 = ");
  Serial.println(sensorValue6);   

  if((sensorValue1 = analogRead(A0))<900) {
   delay(200);                                     // delay for the coin to settle after bouncing off the stopper
   detect();
  }
}

void startStopBasin() {
  
  int reading = digitalRead(4);                         //reads pin 4 where the button is
  if(reading != lastControlState) {
    time = millis();
  }
 if((millis() - time) > debounce) {

  if (reading != controlState) {
     controlState = reading;
    
  if (controlState == LOW) { 
    basinState = !basinState;
  }
  }
  }

if(basinState == LOW) {
  myservo.attach(3);
  myservo.write(84.5);
}
  else{
    myservo.detach();
  }
  lastControlState = reading;
}

void totalReset() {

 
  
}

void detect() { 
 
 if(sensorValue2<850 && sensorValue3<850 && sensorValue4>900 && sensorValue5>910 && sensorValue6>910) {
  lcd.setCursor((printSpot+4), 1);     // prints coin value
  lcd.print("+0.10");
  cents = cents + 10;
  processed = true;
  LCD();
}
if(processed == true){
  stopper.attach(2);
  stopper.write(90);  //moves servo to let coin out
  delay(600);
  stopper.write(150);
  delay(600);
  stopper.detach();
  processed = false;
  }

 else if(sensorValue2<680 && sensorValue3>390 && sensorValue3<610 && sensorValue4>720 && sensorValue4<910 && sensorValue5>900 && sensorValue6>900) {
  lcd.setCursor((printSpot+4), 1);     // prints coin value
  lcd.print("+0.05");
  cents = cents + 5;
  processed = true;
  LCD();
}
if(processed == true){
  stopper.attach(2);
  stopper.write(90);  //moves servo to let coin out
  delay(600);
  stopper.write(150);
  delay(600);
  stopper.detach();
  processed = false;
  }

 else if(sensorValue2<530 && sensorValue3<450 && sensorValue4>590 && sensorValue4<750 && sensorValue5>870 && sensorValue6>900) {
  lcd.setCursor((printSpot+4), 1);     // prints coin value
  lcd.print("+0.25");
  cents = cents + 25;
  processed = true;
  LCD();
}
 if(processed == true){
  stopper.attach(2);
  stopper.write(90);  //moves servo to let coin out
  delay(600);
  stopper.write(150);
  delay(600);
  stopper.detach();
  processed = false;
 }
 
 else if(sensorValue2<450 && sensorValue3<370 && sensorValue4<520 && sensorValue5<900 && sensorValue5>800 && sensorValue6>890) {
  lcd.setCursor((printSpot+4), 1);     // prints coin value
  lcd.print("+1.00");
  dollars = dollars + 1;
  processed = true;
  LCD();
}
 if(processed == true){
  stopper.attach(2);
  stopper.write(90);  //moves servo to let coin out
  delay(600);
  stopper.write(150);
  delay(600);
  stopper.detach();
  processed = false;
 }
 
 else if(sensorValue2<390 && sensorValue3<340 && sensorValue4<490 && sensorValue5<800 && sensorValue5>560 && sensorValue6>880) {
  lcd.setCursor((printSpot+4), 1);     // prints coin value
  lcd.print("+2.00");
  dollars = dollars + 2;
  processed = true;
  LCD();
}
  if(processed == true){
  stopper.attach(2);
  stopper.write(90);  //moves servo to let coin out
  delay(600);
  stopper.write(150);
  delay(600);
  stopper.detach();
  processed = false;
 }
  }

void LCD() {
    
 if(cents>=100 && ((millis() - onDelayVariable) > onDelay)){                        //roles over 100 cents to one dollar if cents varible is more than 99 cents
  dollars = dollars + 1;
  cents = cents - 100;
  lcd.setCursor(4,1);
  lcd.print("0");
  lcd.setCursor(8,1);
  lcd.print(" ");
}


if ((dollars>=10 && dollars<=99) && ((millis() - onDelayVariable) > onDelay)){    //moves decimal to correct place for double digit dollars
 lcd.setCursor(3,1);
 lcd.print(".");
 printSpot = 4;
 
 if (cents == 0){                                                                // adds extra 0 if necessary for cleaner look
  lcd.setCursor((printSpot+1),1);
  lcd.print("0");
 }

}


int digit;                                 //variable for moving single digit cents to proper digit

if(cents == 5){                 //if cents is less than two digits move single digit one digit to the right
  digit = 1;
}
else{
  digit = 0;
}

 if ((millis() - onDelayVariable) > onDelay){        // adds pleasing delay to displaying showing coin type and value before adding the value to total
 
  lcd.setCursor(1,1);                               //prints total on LCD in proper locataion
  lcd.print(dollars);
  lcd.setCursor((printSpot+digit),1);
  lcd.print(cents);

  lcd.setCursor((printSpot+2),1);

 }

//if ((millis() - onDelayVariable) > (onDelay+700)){          // erases coin addition amount after 1.6 seconds
 //  lcd.setCursor(7,1);
//} 
}

Hello World!

I am in seek of assistance with my project involving coin counting. Essentially the code starts with the total amount being displayed at base value (0) for the integrated dollars and cents values. And whenever the desired reset switch is clicked, I want it to reset the cents and dollars variable back to its initial 0 value.

I just coded in a first switch to control the servo of the coin basin. It is designated through use of the input state effecting the output state (input being pin 4, output being pin 3). I was thinking of using similar commands for the resetting of the value but there it's technically an "output pin" to have affected since it's internal code.

Any help for understanding of this would be appreciated as I can't come to a conclusion on this.

Thanks in advance for the help!
Adam

When you detect the button is pressed, just set 'dollars' and 'cents' to zero. You are not required to change any output pins if you don't want to. After you change them to zero you should probably display the new value.

johnwasser:
When you detect the button is pressed, just set 'dollars' and 'cents' to zero. You are not required to change any output pins if you don't want to. After you change them to zero you should probably display the new value.

Right! I figured that would be the way to do it. Only thing is.... I'm not sure how to incorporate lines of code to do such a thing. I was barely familiar with adding in the button to manipulate an output HIGH or LOW.

Would you mind throwing a sample code at me???

Do not cross-post again. Other thread removed.

Pistolkors:
Right! I figured that would be the way to do it. Only thing is.... I'm not sure how to incorporate lines of code to do such a thing. I was barely familiar with adding in the button to manipulate an output HIGH or LOW.

Would you mind throwing a sample code at me???

File->Examples->02.Digital->Button
File->Examples->02.Digital->Debounce
File->Examples->02.Digital->StateChangeDetection
Or you could use a library:
Sketch->Include Library->Manage Libraries...
filter for the word "pushbutton"
Pick one of the button handling libraries and look at the examples that come with it.