Hello everyone, i have this piece of code i have uploaded to the arduino. It gives the right message in serial monitor, but right after it goes crazy and prints a lot of values. I dont think it is the code that has any errors. I think it might be the board itself, but will anyone take a look at the code and confirm my statement of nothing is wrong with the code.
//List of the libaries that are used
#include <PinChangeInterrupt.h> //This libary makes it possible to create more interrupt pins.
#include <EEPROM.h> //This libary makes is possible to store data within the Arduinos EEPROM.
// The pins defined below, are the pins that are used by the six optocoupler.
int optoPin0 = A0;
int optoPin1 = A1;
int optoPin2 = A2;
int optoPin3 = 3;
int optoPin4 = 4;
int optoPin5 = 5;
// The variables below are the variables stores informatation about each coin, and will be stored within the EEPROM.
volatile int twenty = 0; //This is the variable that stores the combined saldo of the "20 kroner" coins, and in the EEPROM it has the address of 0.
volatile int ten = 0; //This is the variable that stores the combined saldo of the "10 kroner" coins, and in the EEPROM it has the address of 1.
volatile int five = 0; //This is the variable that stores the combined saldo of the "5 kroner" coins, and in the EEPROM it has the address of 2.
volatile int two = 0; //This is the variable that stores the combined saldo of the "2 kroner" coins, and in the EEPROM it has the address of 3.
volatile int one = 0; //This is the variable that stores the combined saldo of the "1 kroner" coins, and in the EEPROM it has the address of 4.
volatile float half = 0; //This is the variable that stores the combined saldo of the "50 øre" coins, and in the EEPROM it has the address of 5.
// Variables holding account of the quantities of the coins
volatile int qtwenty = 0;
volatile int qten = 0;
volatile int qfive = 0;
volatile int qtwo = 0;
volatile int qone = 0;
volatile int qhalf = 0;
// The variable below is the variable that stores the combined amount of money there are in the system.
volatile float saldo = 0;
// The definitions below are the pins that are being used by the motor which drives a wheel that helps sorting one coin at a time.
const int motor1 = 12;
const int motor2 = 13;
// This variables contains characthers, which are used to show information in Serial Monitor about anything in the system.
char userInput;
// Function below is the setup function that runs each time the arduino has been reset, or turned off.
void setup()
{
// This setup the Serial Monitor with a baudrate of 9600 bits per secund.
Serial.begin(9600);
// Below every optocoupler is configured to be an input, and connected to a interrupt.
pinMode(optoPin5, INPUT); // This line configures the optoPin5 or the digitalpin 5 to be an input.
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(optoPin5), add20, RISING); /* Here the PinChangeInterrupt libary has been used, to chance the digitalpin 5 to an interrupt pin.
When the pin detects a rising, 0 to 1, it will call the funciton 'add20', which is descibed further down. */
pinMode(optoPin4, INPUT);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(optoPin4), add10, RISING);
pinMode(optoPin3, INPUT);
attachInterrupt(digitalPinToInterrupt(optoPin3), add5, RISING); /* The pin has been set to be an interrupt pins which calls the function add5, when the pin detects a rising, 0 to 1. */
pinMode(optoPin2, INPUT);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(optoPin2), add2, RISING);
pinMode(optoPin1, INPUT);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(optoPin1), add1, RISING);
pinMode(optoPin0, INPUT);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(optoPin0), add050, RISING);
// This is the startup message you will receive when you open up the serial monitor.
Serial.println(" #In order to see information, enter one of the characters below");
Serial.println("To see info about the total saldo, enter 'Q'");
Serial.println("To see info about the 20 kroner saldo, enter 'W'");
Serial.println("To see info about the 10 kroner saldo, enter 'E'");
Serial.println("To see info about the 5 kroner saldo, enter 'R'");
Serial.println("To see info about the 2 kroner saldo, enter 'T'");
Serial.println("To see info about the 1 kroner saldo, enter 'Y'");
Serial.println("To see info about the 50 øre saldo saldo, enter 'U'");
Serial.println("");
Serial.println("Ready");
Serial.println();
// Configuring the pins for the DC-motor.
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
// Configuring the LCD.
// lcd.begin(16, 2);
// Everytime the arduino is reset, it checks the EEPROM, for the last values known about each coin.
twenty = EEPROM.read(0);
ten = EEPROM.read(1);
five = EEPROM.read(2);
two = EEPROM.read(3);
one = EEPROM.read(4);
half = EEPROM.read(5);
}
void loop() {
// In order to reset the EEPROM 100%, remove the comment tags below and upload the code once. This piece of code updates all the addresses used within the EEPROM to zero.
/*EEPROM.update(0, 0);
EEPROM.update(1, 0);
EEPROM.update(2, 0);
EEPROM.update(3, 0);
EEPROM.update(4, 0);
EEPROM.update(5, 0);*/
if (Serial.available () > 0) { // Only runs the if statement if where is an input.
userInput = Serial.read();
if (userInput == 'Q') {
Serial.println(" #Du kan nu se den samlede saldo.");
Serial.print(saldo);
Serial.println(" kroner");
Serial.println();
}
if (userInput == 'W'){
Serial.println(" #Du kan nu se informationer om 20erne.");
Serial.print("Antallet af mønter: ");
Serial.println(qtwenty);
Serial.print("Saldo: ");
Serial.print(twenty);
Serial.println(" kroner");
Serial.println();
}
if (userInput == 'E'){
Serial.println(" #Du kan nu se informationer om 10erne.");
Serial.print("Antallet af mønter: ");
Serial.println(qten);
Serial.print("Saldo: ");
Serial.print(ten);
Serial.println(" kroner");
Serial.println();
}
if (userInput == 'R'){
Serial.println(" #Du kan nu se informationer om 5erne.");
Serial.print("Antallet af mønter: ");
Serial.println(qfive);
Serial.print("Saldo: ");
Serial.print(five);
Serial.println(" kroner");
Serial.println();
}
if (userInput == 'T'){
Serial.println(" #Du kan nu se informationer om 2 kronerne.");
Serial.print("Antallet af mønter: ");
Serial.println(qtwo);
Serial.print("Saldo: ");
Serial.print(two);
Serial.println(" kroner");
Serial.println();
}
if (userInput == 'Y'){
Serial.println(" #Du kan nu se informationer om 1 kronerne.");
Serial.print("Antallet af mønter: ");
Serial.println(qone);
Serial.print("Saldo: ");
Serial.print(one);
Serial.println(" kroner");
Serial.println();
}
if (userInput == 'U'){
Serial.println(" #Du kan nu se informationer om 50 ørene.");
Serial.print("Antallet af mønter: ");
Serial.println(qhalf);
Serial.print("Saldo: ");
Serial.print(half);
Serial.println(" kroner");
Serial.println();
}
}
}
void add20() //Funktionen tilføjer 20 kroner til den samlede saldo.
{
saldo += 20;
Serial.print(saldo);
Serial.println(" kroner");
Serial.println("");
twenty +=20;
qtwenty +=1;
//EEPROM.write(0, twenty);
}
void add10() //Funktionen tilføjer 10 kroner til den samlede saldo.
{
saldo += 10;
Serial.print(saldo);
Serial.println(" kroner");
Serial.println("");
ten +=10;
qten +=1;
//EEPROM.write(1, ten);
}
void add5() //Funktionen tilføjer 5 kroner til den samlede saldo.
{
saldo += 5;
Serial.print(saldo);
Serial.println(" kroner");
Serial.println("");
five +=5;
qfive +=1;
//EEPROM.write(2, five);
}
void add2() //Funktionen tilføjer 2 kroner til den samlede saldo.
{
saldo += 2;
Serial.print(saldo);
Serial.println(" kroner");
Serial.println("");
two +=2;
qtwo +=1;
}
void add1() //Funktionen tilføjer 1 kroner til den samlede saldo.
{
saldo += 1;
Serial.print(saldo);
Serial.println(" kroner");
Serial.println("");
one +=1;
qone +=1;
}
void add050() //Funktionen tilføjer 0.50 kroner (50 øre) til den samlede saldo.
{
saldo += 0.50;
Serial.print(saldo);
Serial.println(" kroner");
Serial.println("");
half +=0.50;
qhalf +=1;
}