Go Down

Topic: Two Unknown Causes For Two Problems In My Program...Help! (Read 551 times) previous topic - next topic

PinballCoder018

Mar 28, 2018, 10:36 am Last Edit: Mar 28, 2018, 10:39 am by PinballCoder018
Hi ,

I am making a virtual coin acceptor...the user types in a value and it acts like a virtual coin and is meant to save it into the 'piggy bank' and it is supposed to save the balance of the 'piggy bank' to the EEPROM memory of the Arduino when the number 99 is typed in. It can also check the balance by typing in the number 98.
I have two problems with the below code...
1. Whenever I Insert A Value Not Listed (1,2,5,50,10,20,98,99) it triggers the save function.
2. The save function does not work...I am trying to save the value to EEPROM but it will not work for some reason.

Any help would be greatly appreciated.  :)

Cheers.

Code: [Select]
#include <EEPROM.h>
byte Value_EEPROM;

unsigned int T_FiveCents = 0;
unsigned int T_TenCents = 0;
unsigned int T_TwentyCents = 0;
unsigned int T_FiftyCents = 0;
unsigned int T_OneDollars = 0;
unsigned int T_TwoDollars = 0;

unsigned int tempvalue;
unsigned int T_Balance;

void setup() {
  int Read_PastBalance;
 
  bool ReadDone;
 
  Serial.begin(9600);
 
    while (!Serial) {
    ;
    }
 
  Serial.println("Serial Loaded...");
  Serial.println();
  Serial.println("Please Wait...Loading Past Balance");
  Serial.println();
    while(ReadDone = false){
    Value_EEPROM = EEPROM.read(1);
    Serial.println(Value_EEPROM, DEC);
    Read_PastBalance = Serial.parseInt();
    T_Balance = Read_PastBalance * 4;
    ReadDone = true;
    }

    for (int i = 0 ; i < EEPROM.length() ; i++) {
    EEPROM.write(i, 0);
    }
   
  Serial.println("Welcome to the Virtual Coin Acceptor!");
  Serial.println();
  Serial.print("You have ");
  Serial.print("$");
  Serial.print(T_Balance, DEC);
  Serial.print("\n");
  Serial.println();
}

void loop() {
 
  int ReadSerial = 0;
 
  Serial.println("Please input coin value or type in 98 to check your balance or 99 to save your balance.");
  Serial.println();
 
  while(ReadSerial == 0){
    ReadSerial = Serial.parseInt();
  }
    Serial.println(ReadSerial);
    Serial.println();
    if(ReadSerial==5){
        Serial.println("You have inserted five cents.");
        Serial.println();
        tempvalue = T_FiveCents;
        T_FiveCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==1){
        Serial.println("You have inserted ten cents.");
        Serial.println();
        tempvalue = T_TenCents;
        T_TenCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==2){
        Serial.println("You have inserted twenty cents.");
        Serial.println();
        tempvalue = T_TwentyCents;
        T_TwentyCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==50){
        Serial.println("You have inserted fifty cents.");
        Serial.println();
        tempvalue = T_FiftyCents;
        T_FiftyCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==10){
        Serial.println("You have inserted one dollar.");
        Serial.println();
        tempvalue = T_OneDollars;
        T_OneDollars = tempvalue + 1;
       tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==20){
        Serial.println("You have inserted two dollars.");
        Serial.println();
        tempvalue = T_TwoDollars;
        T_TwoDollars = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial == 98){
        Calculate();
        Serial.print("You have ");
        Serial.print("$");
        Serial.print(T_Balance, DEC);
        Serial.print("\n");
        Serial.println();
      }else if(ReadSerial=99){
        Serial.println("Saving balance...");
        Calculate();
        tempvalue = 0;
        tempvalue = (T_Balance/4);
        EEPROM.write(1, tempvalue);
        tempvalue = 0;
        Serial.println("Balance saved.");
        Serial.println();
      }else if(ReadSerial != 5 && ReadSerial != 1 && ReadSerial != 2 && ReadSerial != 50 && ReadSerial != 10 && ReadSerial != 20 && ReadSerial != 98 && ReadSerial != 99){
        Serial.println("Invalid value or selection.");
        Serial.println();
      }
}

void Calculate() {
tempvalue = T_FiveCents;
T_FiveCents = tempvalue * 0.05;
tempvalue = 0;

tempvalue = T_TenCents;
T_TenCents = tempvalue * 0.10;
tempvalue = 0;
 
tempvalue = T_TwentyCents;
T_TwentyCents = tempvalue * 0.20;
tempvalue = 0;
 
tempvalue = T_FiftyCents;
T_FiftyCents = tempvalue * 0.50;
tempvalue = 0;
 
tempvalue = T_OneDollars;
T_OneDollars = tempvalue * 1.00;
tempvalue = 0;
 
tempvalue = T_TwoDollars;
T_TwoDollars = tempvalue * 2.00;
tempvalue = 0;

tempvalue = T_Balance;
T_Balance = (T_FiveCents + T_TenCents + T_TwentyCents + T_FiftyCents + T_OneDollars + T_TwoDollars + tempvalue);
tempvalue = 0;

T_FiveCents = 0;
T_TenCents = 0;
T_TwentyCents = 0;
T_FiftyCents = 0;
T_OneDollars = 0;
T_TwoDollars = 0;
}

UKHeliBob

Code: [Select]
  else if (ReadSerial = 99)

Code: [Select]
  while (ReadDone = false)

Code: [Select]
    EEPROM.write(1, tempvalue);saves a single byte.  Have you tried printing tempvalue before saving ?  You can save and load any type of data using EEPROM.put() and EEPROM.get()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PinballCoder018

Hi,

Thank-you for all of that...My only question is what do you mean about printing the tempvalue first and how do I use EEPROM.put() and EEPROM.get()?

Cheers.

slipstick

how do I use EEPROM.put() and EEPROM.get()?
https://www.arduino.cc/en/Reference/EEPROM

Steve

UKHeliBob

Quote
what do you mean about printing the tempvalue first
What I meant was that you should print it to see whether its value was what you expect it to be.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PinballCoder018

Ok, I have done that and it wasn't working so I fixed that which lead me to notice a few more errors so thanks :D Anyway...My problem not is that I am using float variables which work fine but I need help to save the final value float (T_Balance) into the EEPROM memory.

Thanks again.

Code: [Select]
#include <EEPROM.h>

#define EEPROM_DATAPOS 0
#define EEPROM_BALANCEPOS 1

int Value_EEPROM;
int DataQuery;

float T_FiveCents = 0;
float T_TenCents = 0;
float T_TwentyCents = 0;
float T_FiftyCents = 0;
float T_OneDollars = 0;
float T_TwoDollars = 0;

float tempvalue;
float T_Balance;

void setup() {
  int Read_PastBalance;
 
  Serial.begin(9600);
 
    while (!Serial) {
    ;
    }
 
  Serial.println("Serial Loaded...");
  Serial.println();
  Serial.println("Please Wait...Loading Past Balance");
  Serial.println();
 
  DataQuery = EEPROM.read(0);
 
    if(DataQuery == 10){
    Value_EEPROM = EEPROM.read(1);
    T_Balance = Value_EEPROM;
      for (int i = 0 ; i < EEPROM.length() ; i++) {
      EEPROM.write(i, 0);
      }
    Serial.print("You have a past balance of ");
    Serial.print("$");
    Serial.print(T_Balance, 2);
    Serial.print("\n");
   Serial.println();
    }else{
      Serial.println("No Past Balance Found");
      Serial.println();
    }
   
  Serial.println("Welcome to the Virtual Coin Acceptor!");
  Serial.println();
}

void loop() {
 
  int ReadSerial = 0;
 
  Serial.println("Please input coin value or type in 98 to check your balance or 99 to save your balance.");
  Serial.println();
 
  while(ReadSerial == 0){
    ReadSerial = Serial.parseInt();
  }
    Serial.println(ReadSerial);
    Serial.println();
    if(ReadSerial==5){
        Serial.println("You have inserted five cents.");
        Serial.println();
        tempvalue = T_FiveCents;
        T_FiveCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==1){
        Serial.println("You have inserted ten cents.");
        Serial.println();
        tempvalue = T_TenCents;
        T_TenCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==2){
        Serial.println("You have inserted twenty cents.");
        Serial.println();
        tempvalue = T_TwentyCents;
        T_TwentyCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==50){
        Serial.println("You have inserted fifty cents.");
        Serial.println();
        tempvalue = T_FiftyCents;
        T_FiftyCents = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==10){
        Serial.println("You have inserted one dollar.");
        Serial.println();
        tempvalue = T_OneDollars;
        T_OneDollars = tempvalue + 1;
       tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial==20){
        Serial.println("You have inserted two dollars.");
        Serial.println();
        tempvalue = T_TwoDollars;
        T_TwoDollars = tempvalue + 1;
        tempvalue = 0;
        ReadSerial = 0;
      }else if(ReadSerial == 98){
        Calculate();
        Serial.print("You have ");
        Serial.print("$");
        Serial.print(T_Balance, 2);
        Serial.print("\n");
        Serial.println();
      }else if(ReadSerial == 99){
        Serial.println("Saving balance...");
        Calculate();
        tempvalue = (T_Balance);
        EEPROM.write(EEPROM_DATAPOS, 10);
        EEPROM.write(EEPROM_BALANCEPOS, tempvalue);
        delay(1000);
        tempvalue = 0;
        Serial.println("Balance saved.");
        Serial.println();
      }else if(ReadSerial != 5 && ReadSerial != 1 && ReadSerial != 2 && ReadSerial != 50 && ReadSerial != 10 && ReadSerial != 20 && ReadSerial != 98 && ReadSerial != 99){
        Serial.println("Invalid value or selection.");
        Serial.println();
      }
}

void Calculate() {
 
Serial.println("Please Wait...");
delay(1000);
 
tempvalue = T_FiveCents;
T_FiveCents = tempvalue * 0.05;
tempvalue = 0;

tempvalue = T_TenCents;
T_TenCents = tempvalue * 0.10;
tempvalue = 0;
 
tempvalue = T_TwentyCents;
T_TwentyCents = tempvalue * 0.20;
tempvalue = 0;
 
tempvalue = T_FiftyCents;
T_FiftyCents = tempvalue * 0.50;
tempvalue = 0;
 
tempvalue = T_OneDollars;
T_OneDollars = tempvalue * 1.00;
tempvalue = 0;
 
tempvalue = T_TwoDollars;
T_TwoDollars = tempvalue * 2.00;
tempvalue = 0;

tempvalue = T_Balance;
T_Balance = (T_FiveCents + T_TenCents + T_TwentyCents + T_FiftyCents + T_OneDollars + T_TwoDollars + tempvalue);
tempvalue = 0;

T_FiveCents = 0;
T_TenCents = 0;
T_TwentyCents = 0;
T_FiftyCents = 0;
T_OneDollars = 0;
T_TwoDollars = 0;
}

UKHeliBob

EEPROM.read() reads a singe byte
EEPROM.write() writes a single byte

EEPROM.get() reads a variable of any kind, including a float
EEPTOM.put() write  a variable of any kind, including a float
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PinballCoder018

#7
Mar 29, 2018, 09:16 am Last Edit: Mar 29, 2018, 09:24 am by PinballCoder018
How do I incoperate EEPROM.get() and EEPROM.put() in my program for all the values on EEPROM except the 'EEPROM_DATAPOS'/'DataQuery' because I have never used EEPROM and have only been using Arduino for a short period of time and am having a lot of trouble but after I learn how to do this it will help me greatly :D

Cheers
- PinballCoder018

UKHeliBob

Quote
How do I incoperate EEPROM.get() and EEPROM.put() in my program
Use them wherever you would use read() or write() but make sure that you write to EEPROM locations that will not cause data to be overwritten.

For instance if you EEPROM.put() an int into location 0 then location 1 will also be written to because an int is 2 bytes in size.  So if you need to put() another variable into EEPROM then the next available location is 2 to avoid overwriting the second byte of the first int.  When using EEPROM.get() the compiler is smart enough to know how many bytes to load because of the data type of the destination variable

https://www.arduino.cc/en/Reference/EEPROMPut
https://www.arduino.cc/en/Reference/EEPROMGet
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


Go Up