Troubleshooting Tinkercad Arduino UNO simulator

So ive been using the tinkercad blocks to code for a while and I tried to create an countdown style alarm that beeps and slowly gets faster and I have the equation figured out but when I try to run the program with the tinkercad simulator it just doesnt work and I dont know if theres something wrong with the code or setup.

// C++ code
//
int ON = 0;

int START = 0;

int STOP = 0;

int TIME = 0;

int SEC = 0;

int SEC_LEFT = 0;

int TONE = 0;

int BEEP = 0;

void setup()
{
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(5, INPUT);
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);

  ON = 0;
  TIME = 1;
  SEC = 15;
  digitalWrite(6, HIGH);
}

void loop()
{
  TONE = 1;
  SEC_LEFT = SEC;
  while (ON == 1) {
    analogWrite(3, TONE);
    delay(300); // Wait for 300 millisecond(s)
    analogWrite(3, TONE);
    delay(500); // Wait for 500 millisecond(s)
    while (BEEP > 0.15) {
      SEC_LEFT += -1;
      BEEP = (0.1 * (0.9 * (SEC_LEFT / SEC)));
      analogWrite(3, TONE);
      digitalWrite(9, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      analogWrite(3, 0);
      digitalWrite(9, LOW);
    }
  }

  // START OF CYCLE THROUGH TIME
  if (digitalRead(5) == HIGH) {
    Serial.println("activate");
    TIME += 1;
    if (TIME == 4) {
      TIME = 1;
      SEC = 15;
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
    }
    if (TIME == 1) {
      SEC = 15;
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
    }
    if (TIME == 2) {
      SEC = 15;
      digitalWrite(7, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(8, LOW);
    }
    if (TIME == 3) {
      SEC = 15;
      digitalWrite(8, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(6, LOW);
    }
    // END OF CYCLE THROUGH TIME
    delay(500); // Wait for 500 millisecond(s)
  }
  //
  // START OF START BUTTON
  if (digitalRead(3) == HIGH) {
    Serial.println("activate");
    ON = 1;
    // END OF START BUTTON
    delay(500); // Wait for 500 millisecond(s)
  }
  //
  // START OF STOP BUTTON
  if (digitalRead(4) == HIGH) {
    Serial.println("activate");
    ON = 0;
    digitalWrite(9, LOW);
    // END OF STOP BUTTON
    delay(500); // Wait for 500 millisecond(s)
  }
  //
}


PS: I am very new to this stuff and everything I have learned so far was based on me figeting around the simulator then puting it onto my board in real life.

What is TONE and BEEP meant to do?

Repeatedly calling Analogwrite() with the same value won’t do anything special.

I was using the block codes to program it and translated it into c++
the tone was supposed to represent the note the piezo would play because setting it to high was to loud but i realise that since its analog write i dont think any number higher or lower than 1 would make a difference other than turning it off. beep is the interval at which the system waits before repeating the loop

Your design is very bad at seeing where each LED, each button and buzzer are connected.

Images of wiring like this are not recommended here on the forum.

Make a diagram (wiring) even freehand and it will look better than this image you posted.

In your image there are 3 buttons but in the code there are only 2 INPUT defined in setup().

Although it is not wrong, it is not recommended in codes to use the pins by numbers, (3, 4, 5,,, etc) use references, so if you need to change a pin you don't need to go through the entire code looking for where the pin is, just change the reference.

Ex: #define water 5
pinMode(water,OUTPUT);
digitalWrite(watr,HIGH);

You have the same pin used as OUTPUT and INPUT.

BEEP was define as "int" and 0.15 is a float value.
SEC_LEFT was define as "int" and need float.

while (BEEP > 0.15) { Never go on BEEP is 0.. e no change.

SEC = 15;
SEC_LEFT = SEC;
BEEP = (0.1 * (0.9 * (SEC_LEFT / SEC)));
BEEP never will be greater then 0.15.

Try this....

// C++ code
int ON = 0;
int START = 0;
int STOP = 0;
int TIME = 0;
int SEC = 0;
int SEC_LEFT = 0;
int TONE = 0;
int BEEP = 0;

#define bz1 3 

#define bt1 4
#define bt2 5
#define bt3 10

#define lEDG1 6  
#define lEDG2 7  
#define lEDG3 8  
#define lEDP1 9  
//-----------------------------------------------------------------------
void setup(){
  pinMode(lEDG1, OUTPUT);
  pinMode(bz1, OUTPUT);
  pinMode(lEDP1, OUTPUT);
  pinMode(bt2, INPUT);
  Serial.begin(9600);
  pinMode(lEDG3, OUTPUT);
  pinMode(lEDG3, OUTPUT);
  pinMode(bt3, INPUT);
  pinMode(bt1, INPUT);
  ON = 0;
  TIME = 1;
  SEC = 15;
  digitalWrite(lEDG1, HIGH);
}
//-----------------------------------------------------------------------
void loop(){
  TONE = 1;
  SEC_LEFT = SEC;
  while (ON == 1) {
    analogWrite(bz1, TONE);
    delay(300); // Wait for 300 millisecond(s)
    analogWrite(bz1, TONE);
    delay(500); // Wait for 500 millisecond(s)
    while (BEEP > 0.15) {
      SEC_LEFT += -1;
      BEEP = (0.1 * (0.9 * (SEC_LEFT / SEC)));
      analogWrite(bz1, TONE);
      digitalWrite(lEDP1, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      analogWrite(bz1, 0);
      digitalWrite(lEDP1, LOW);
    }
  }
  if (digitalRead(bt2) == HIGH) {   // START OF CYCLE THROUGH TIME
    Serial.println("activate bt2" );
    if (TIME == 4) {
       Serial.println(TIME); 
      TIME = 1;
      SEC = 15;
      digitalWrite(lEDG1, HIGH);
      digitalWrite(lEDP1, LOW);
      digitalWrite(lEDG3, LOW);
    }
    if (TIME == 1) {
      SEC = 15;
      digitalWrite(lEDG1, HIGH);
      digitalWrite(lEDP1, LOW);
      digitalWrite(lEDG3, LOW);
    }
    if (TIME == 2) {
      SEC = 15;
      digitalWrite(lEDP1, HIGH);
      digitalWrite(lEDG1, LOW);
      digitalWrite(lEDG3, LOW);
    }
    if (TIME == 3) {
      SEC = 15;
      digitalWrite(lEDG3, HIGH);
      digitalWrite(lEDP1, LOW);
      digitalWrite(lEDG1, LOW);
    }
    // END OF CYCLE THROUGH TIME
    delay(500); // Wait for 500 millisecond(s)
    TIME += 1;
  }
  if (digitalRead(bt3) == HIGH) {   // START OF START BUTTON
    Serial.println("activate bt3");
    ON = 1;
    // END OF START BUTTON
    delay(500); // Wait for 500 millisecond(s)
  }
  if (digitalRead(bt1) == HIGH) {   // START OF STOP BUTTON
    Serial.println("activate bt1");
    ON = 0;
    digitalWrite(lEDP1, LOW);
    // END OF STOP BUTTON
    delay(500); // Wait for 500 millisecond(s)
  }
}

How about a basic programming course?

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

Ok so Ive started using the simulator that you posted to see if I could make it work on there, although it does work better, there is a problem that I have encountered

// C++ code
int ON = 0;
int START = 0;
int STOP = 0;
int TIME = 0;
int SEC = 0;
int SEC_LEFT = 0;
int TONE = 0;
int BEEP = 0;

#define bz1 3

#define bt1 4 // cancels the timer loop
#define bt2 5 // changes the number of seconds the timer loops
#define bt3 10 // begins the loop

#define lEDG1 6
#define lEDG2 9
#define lEDG3 8
#define lEDP1 7
//-----------------------------------------------------------------------
void setup() {
  pinMode(lEDG1, OUTPUT);
  pinMode(bz1, OUTPUT);
  pinMode(lEDP1, OUTPUT);
  pinMode(bt2, INPUT);
  Serial.begin(9600);
  pinMode(lEDG3, OUTPUT);
  pinMode(lEDG3, OUTPUT);
  pinMode(bt3, INPUT);
  pinMode(bt1, INPUT);
  ON = 0;
  TIME = 1;

  digitalWrite(lEDG1, HIGH);
}
//-----------------------------------------------------------------------
void loop() {
  if (digitalRead(bt2) == HIGH) {   // START OF CYCLE THROUGH TIME
    Serial.println("activate bt2" );
    if (TIME == 4) {
      Serial.println(TIME);
      TIME = 1;
      SEC = 15000; // Goes back to the first loop of 15 seconds.
      digitalWrite(lEDG1, HIGH);
      digitalWrite(lEDP1, LOW);
      digitalWrite(lEDG3, LOW);
    }
    if (TIME == 1) {
      SEC = 15000; // timer should last 15 seconds
      digitalWrite(lEDG1, HIGH);
      digitalWrite(lEDP1, LOW);
      digitalWrite(lEDG3, LOW);
    }
    if (TIME == 2) {
      SEC = 30000; // timer should last 30 seconds
      digitalWrite(lEDP1, HIGH);
      digitalWrite(lEDG1, LOW);
      digitalWrite(lEDG3, LOW);
    }
    if (TIME == 3) {
      SEC = 45000; // timer should last 45 seconds
      digitalWrite(lEDG3, HIGH);
      digitalWrite(lEDP1, LOW);
      digitalWrite(lEDG1, LOW);
    }
    // END OF CYCLE THROUGH TIME
    delay(500); // Wait for 500 millisecond(s)
    TIME += 1;
  }
  if (digitalRead(bt3) == HIGH) {   // START OF START BUTTON
    Serial.println("activate bt3");
    startTimer(); // starts the function listed below
    // END OF START BUTTON
    delay(500); // Wait for 500 millisecond(s)
  }

  TONE = 1;

}

void startTimer() {
  analogWrite(bz1, TONE);
  delay(300); // Wait for 300 millisecond(s)
  analogWrite(bz1, TONE);
  delay(300); // Wait for 500 millisecond(s)
  // 2 quick beeps to notify that the function is starting
  Serial.println("activate function");

  SEC_LEFT = SEC;
  // sec is the total amount of time and sec_left is the time left before the timer should reach 0
  BEEP = 100 + 900 * (SEC_LEFT / SEC);
  // beep is the interval in miliseconds
  Serial.println(BEEP);
  Serial.println(SEC);
  Serial.println(SEC_LEFT);

  while (SEC_LEFT > 1500) {
    // function should stop at 15 seconds
    analogWrite(bz1, TONE);
    digitalWrite(lEDG2, HIGH);
    Serial.println("on");
    // prints that the buzzer is on for debugging

    delay(BEEP); // Wait for 1000 millisecond(s)

    analogWrite(bz1, 0);
    digitalWrite(lEDG2, LOW);
    Serial.println("off");
    // prints that the buzzer is on for debugging
    
    delay(BEEP);

    BEEP = 100 + 900 * (SEC_LEFT / SEC);

    SEC_LEFT -= 1000;
    Serial.println(SEC);
    Serial.println(SEC_LEFT);
    Serial.println(BEEP);
    

    if (digitalRead(bt1) == HIGH) {   // START OF STOP BUTTON
      Serial.println("activate bt1");
      ON = 0;
      break;
      digitalWrite(lEDG2, LOW);
      // END OF STOP BUTTON
      delay(500); // Wait for 500 millisecond(s)
    }
  }
}

most of the code works fine except the startTimer function
the function is supposed to make the buzzer beep and slowly get faster as the end of the timer approaches. For some reason I only get one second before the timer gets extremely fast.

I have tried your WOKWI-simulation. Though for me the setup is totally unintuitive

I clicked on the buttons which made the buzzer buzz sometimes but I have no idea in what sequence to do what and what then can be expected from the code.

You should give a normal worded description of

which buttons to click on in which sequence
and a description what should happen after each button-press
after what amout of time has passed by
and how the codes behaviour does deviate from the wanted behaviour

Going from right to left:
the first button starts the sequence
the second button alternates through the 3 LED lights each representing different amount of times the sequence should be (45 seconds, 30 seconds, 15 seconds.)
the third button stops the sequence (if the sequence is currently running)
if you press the second button (middle) until the middle LED is active, the sequence is supposed to run for 30 seconds. This means that the buzzer should steadily buzz on and off repeatedly and get faster and faster as the countdown reaches 0, at which it will stop.

This WOWKI-Simualtion has exactly the same logic but has additional / different debug-printing.

Go test this WOKWI-Simulation thouroughly and report if it really does what you have described above

I claim it does work only one single time after that the code hangs up in permanent buzzing.

wrong just prints "activate bt1"

run the WOKWI sim and look at the serial output

The simulation you provided does not do what I provided.

the first button should stop the sequence when it starts but if the sequence is not active it just prints activate bt1.
the second button should cycle through the 3 green leds indicating how long the sequence should be (first led for 15 seconds, second 30, third 45)
the third button should start the countdown and slowly get faster and faster as the time gets lower.

dear @nappymax

I have taken YOUR code from post # 6
that links to a WOKWI-simulation which is this one

I made a copy of YOUR simulation and kept YOUR logic

If you are not yet convinced go and test again YOUR OWN WOKWI-Simulation

It seems to be the exact same WOKWI simulation as user

@ruilviana posted in post # 4

1 Like

After taking a look at your simulation I finally realized why mines was not working.
First I was trying to divide two integer values for my equation, but after changing them to float values it had finally worked as planned.
Second for the purple LED, I had not set a pin mode so it couldn't blink correspondingly with the buzzer.

Going from left to right, for the project to work as planned, you must first press the middle button to select a time that corresponds with one of the three green lights (or the number 2 on your keyboard).

Then you can press the last button (or number 3 on your keyboard) to start the sequence and the buzzer countdown.

The first button (or number 1 on your keyboard) should stop the sequence if you hold it.

Ok, I think I have the buttons figured out.

Can you describe the three sequences.. In human? Like you were explaining it to your mom? I'm really foggy on what you are looking for there.

I think I got it.

This is called ‘debugging’ - well done.

2 Likes

Wow thanks the simulation you provided is a cleaner version of mines. My only thing though is that when you want to stop the third button sequence by pressing the first button the servo should not move because the servo moving is a result of the sequence fully completing itself.

You are correct! I missed the line that set it back to a wait state because it was canceled. Try it now.

1 Like

Yeah it works now thanks. My only question now is that in the simulator it uses 4 terminal buttons but when making the project in real life I used 2 terminal buttons, I just want to know if there is a way for it to work with 2 terminal buttons or changes have to be made.

(btw I put one end of the 2 term buttons to 5 volts and the other end to the corresponding pins, I have also switch the 5 volt pins to ground but it still didnt work).

What's a terminal button? Is it some name for a button? What I setup used 3.

They way mine is wired, one button lead hooks to the pin, the other to ground. No resistors. When the button is pushed it connects the Arduino pin to ground. This is probably the most common way to wire these and the Arduino is set up to do it that way.

If you want to change the function of the buttons. The best thing to do is to describe in detail how they should behave.

by 4 terminal button i mean something like this
pins - Why do some tactile switches have 4 terminals? - Electrical  Engineering Stack Exchange
but I have these buttons
Red Push Button N/C 16MM (5/8) Normally Closed - PLCCable.com - Automation  Supplies and PLC Tools
but the ways yours are wired by only using 2 hooks it should work the same for these buttons so maybe im doing something wrong