Salve a tutti, sono nuovo del forum ho un arduino 1 rev 3,ho creato un coutdown su un lcd ma alla fine del conteggio vorrei farlo suonare ma quando metto la melodia suona insieme al conteggio qualcuno sa aiutarmi ?
io conosco poco di arduino quindi se riuscite a darmi il codice ve ne sarei grato
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Countdown");
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
}
int ms = 5000;
int refreshms = 250;
int restart = 0;
void loop() {
if (restart== 0){
for (int i = 1; i <= 5000 / refreshms ; i++){
lcd.clear();
lcd.print("Countdown");
lcd.setCursor(0, 1);
ms -= refreshms;
lcd.print(ms);
delay(refreshms);
if (i == 5000 / refreshms)
{
//operazioni da eseguire alla fine del countdown
ms = 5000;
restart = 1;// mettere 0 se si vuole far ripartire il countdown quando finisce
}
}
}
}
Per il codice, per due volte usi il calcolo "5000 / refreshms".
Sarebbe meglio allora creare una variabile con questo valore calcolato.
Inoltre mi sembra di capire che "refreshms" non viene modificato nel programma perciò sarebbe meglio fosse costante.
int ms = 5000;
const int refreshms = 250;
const int num=5000/refreshms;
int restart = 0;
...
for (int i = 1; i <= num ; i++){
...
if (i == num)
Ora la "if" proverei a usare "i >= num" o ad usare Serial.println() per visualizzare su Monitor Seriale i valori delle variabili i, num e restart.
il calcolo mi funziona perfettamente senza problemi quando finisce di contare vorrei farlo suonare ovvero quando arriva a 0 il counter pero non riesco se lo predispongo cosi suona ogni secondo
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4,};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Countdown");
pinMode(7,OUTPUT);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 1; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration =1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
digitalWrite(7,HIGH);
}
int ms = 5000;
int refreshms = 50;
int restart = 0;
void loop() {
if (restart== 0){
for (int i = 1; i <= 5000 / refreshms ; i++){
lcd.clear();
lcd.print("Countdown");
lcd.setCursor(0, 1);
digitalWrite(8, HIGH); // turn the melody on (HIGH is the voltage level)
delay(50) ; // wait for a second
digitalWrite(8, HIGH); // turn the melody off by making the voltage LOW
delay(50); // wait for a second
// turn off tone function for pin 8:
noTone(8);
// play a note on pin 8 for 200 ms:
tone(8, 440, 200);
delay(50);
ms -= refreshms;
lcd.print(ms);
delay(refreshms);
if (i == 5000 / refreshms)
{
//operazioni da eseguire alla fine del countdown
ms = 5000;
restart = 1;// mettere 0 se si vuole far ripartire il countdown quando finisce
}
}
}
}
e premettiamo che non sono esperto e a mala pena conosco le basi...