Hi. I am new to this forum, any tips or guidance would be much appreciated. sorry for my bad English I'm Malaysian btw.
I think my problem is not really hard to solve.
I want to build a coin laundry timer. Whenever 50 cents is inserted into the coin acceptor. The relay will turn on for 1 hour and a TM1637 display will show the remaining time.
Right after uploading the code into Arduino. I insert 50 cents into the coin acceptor, and everything works fine, relay turned on and the timer shows the exact remaining time as it supposed to. But on a second try, without restarting or reuploading the code. The timer isn't functioning.
Below is my code. I'm sorry the code is a little bit messy.
#define numberOfSeconds(time) ((time / 1000) % 60)
#define numberOfMinutes(time) (((time / 1000) / 60) % 60)
#include <TM1637Display.h>
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
const uint8_t OFF[] = {0, 0, 0, 0};
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};
unsigned long timeLimit = 60000;
unsigned long interval = 60000;
const int relay = 7;
const int coinpin = 2;
const int ledpin = 13;
const int targetcents = 5;
volatile int cents = 0;
int credits = 0;
TM1637Display display(8, 9);
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
//TIMER SECTION 2
display.setBrightness(0x0c);
display.setSegments(PLAY);
}
void loop() {
if (cents >= targetcents) {
credits = credits + 1;
cents = cents - targetcents;
}
else {}
Serial.print(cents);
Serial.print(" cents toward current credit and ");
Serial.print(credits);
Serial.println(" credit(s) earned so far.");
if (credits == 1) {
digitalWrite(relay, LOW);
unsigned long timeRemaining = timeLimit - millis();
unsigned long interval = 60000;
while (timeElapsed < interval) {
int seconds = numberOfSeconds(timeRemaining);
int minutes = numberOfMinutes(timeRemaining);
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0x80 >> 3, true, 2, 0);
timeRemaining = timeLimit - millis();
}
digitalWrite(relay, HIGH);
display.setSegments(PLAY);
credits = credits - 1;
}
}
void coinInterrupt() {
cents = cents + 1;
}
void displayText() {
display.setSegments(PLAY);
delay(2000);
}
But I think the problem is within this line of code.
if (credits == 1) {
digitalWrite(relay, LOW);
unsigned long timeRemaining = timeLimit - millis();
unsigned long interval = 60000;
while (timeElapsed < interval) {
int seconds = numberOfSeconds(timeRemaining);
int minutes = numberOfMinutes(timeRemaining);
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0x80 >> 3, true, 2, 0);
timeRemaining = timeLimit - millis();
}
digitalWrite(relay, HIGH);
display.setSegments(PLAY);
credits = credits - 1;
}