Hi there people I'm new to arduino and coding so bare with. Got a question regarding frequency alteration on the NANO (non genuine) . I have been making an electronic timer recently but I've come across this road block in my progress and thought that the best course of action will be to post my problem here.
On one arduino I have the transmitter with push buttons that I used. It sends the data to the receiver and it displays the value on the tm1637 display. It will show the amount of time in minuets that you want to use. (num) Another button on the transmitter will start the time and another will reset it.
I am using two arduino mega2560 boards with two more hc - 05 bluetooth modules with the tm1637 connected to one of the megas. I have followed [Transmitter and Receiver for multiple data ] (SEND RECEIVE MULTIPLE DATA - #21 by GolamMostafa) this guide that is at the very bottom of the forum that is a Connection diagram between UNO and NANO using Soft and Hard UART Link.
The problem that I am running into is that whenever I start the timer from the transmitter, the display starts counting down properly with the time that I wanted. But then the hc 05 modules disconnect all serial communication and then the timer no longer works.
#include <TM1637Display.h>
bool flag = false; //Frame synchronizer has not come
byte frameArray[21];
byte potMapRxArray[5];
byte x, y, y1;
int i = 0;
int j = 0;
const int CLK = 2; //Set the CLK pin connection to the display
const int DIO = 3; //Set the DIO pin connection to the display
unsigned long startTime; //variables for the clock
unsigned long currentTime;
unsigned long elapsedTime;
const long interval; // Toggle interval in milliseconds
unsigned long previousMillis = 0;
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
unsigned int num = 10; // starting time
void setup()
{
Serial.begin(9600); // Serial port to computer
// for (int i=0, j=8; i<5, j<13; i++, j++)
// {
// myservo[i].attach(j);
// }
pinMode(10,OUTPUT);
pinMode(8,OUTPUT);
pinMode(6,OUTPUT);
pinMode(4,OUTPUT);
display.setBrightness(2); //set the diplay to brightness
}
void loop()
{
if (i == 21) //complete frame is received
{
buildpotMapRxArray(); //nuumber in: z, p, and myData[] of union{}
updown();
clk();
i = 0;
flag = false;
i = 0;
j = 0;
}
}
void buildpotMapRxArray()
{
//cli();
Serial.println();//write(frameArray[18]);
// HERE: goto HERE;
for (i = 9, j = 0; i < 19, j < 5; i++, j++)
{
if (potMapRxArray[2] == 0xB4) {
digitalWrite(6,HIGH);
}
if (potMapRxArray[0] == 0xB4) {
digitalWrite(4,HIGH);
}
else {
digitalWrite(4,LOW);
}
y = frameArray[i];
if (y < 0x41)
{
y = y & 0x0F;
y = y << 4;
}
else
{
y = y - 0x37;
y = y << 4;
}
// Serial.println(y, HEX);
//-------------------
i = i + 1; //i++;
y1 = frameArray[i];
if (y1 < 0x41)
{
y1 = y1 & 0x0F;
}
else
{
y1 = y1 - 0x37;
}
//------------------------
y = y | y1;
potMapRxArray[j] = y;
}
Serial.println(potMapRxArray[0], HEX);
Serial.println(potMapRxArray[1], HEX);
Serial.println(potMapRxArray[2], HEX);
Serial.println(potMapRxArray[3], HEX);
Serial.println(potMapRxArray[4], HEX);
Serial.println("======================");
}
void serialEvent()
{
if (flag == true)
{
while (Serial.available())
{ //Serial Port has data
x = Serial.read();
Serial.write(x); // Send the data to Serial monitor
frameArray[i] = x; //save in array
i++;
}
}
else
{
byte s = Serial.read();
if ( s != 0x3A)
{
flag = false;
}
else
{
if ( s == 0x3A)
{
Serial.write(s);
flag = true;
i++;
}
}
}
}
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void updown() {
// this void increases the amount of time that you want to set, checking potMapRxArray[4] (subtract) and potMapRxArray[3] (add)
display.showNumberDec(num, true, 4, 0);
if (potMapRxArray[3] == 0xB4) {
digitalWrite(10,HIGH);
num++; // increment 'num'
if(num > 9999)
num = 0;
}
else {
digitalWrite(10,LOW);
}
if (potMapRxArray[4] == 0xB4) {
digitalWrite(8,HIGH);
num--; // decrement 'num'
if(num < 0)
num = 9999;
}
else {
digitalWrite(8,LOW);
}
}
void clk() {
//this void checks if the potMapRxArray[2] is equal to 0xB4 and starts the timer on the tm1637
if (potMapRxArray[2] == 0xB4) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
digitalWrite(6,HIGH);
previousMillis = currentMillis;
num = num * 60;
// Button is pressed, start countdown
startTime = millis(); // Record the starting time
while (true) {
currentTime = millis(); // Get the current time
elapsedTime = (currentTime - startTime) / 1000; // Calculate elapsed time in seconds
if (elapsedTime <= num) {
unsigned long remainingTime = num - elapsedTime;
unsigned int minutes = remainingTime / 60;
unsigned int seconds = remainingTime % 60;
display.showNumberDecEx(minutes * 100 + seconds, 0b01000000, true);
if (remainingTime == 0) {
// Start blinking when countdown reaches 00:00
while (true) {
display.showNumberDecEx(0, 0b01000000, true); // Display "00:00"
delay(500);
display.clear(); // Clear the display
delay(500);
if (digitalRead(10) == HIGH) {
resetFunc(); //call reset
}
}
}
}
}
}
}
}