Bonjour,
J'ai créé un compteur de 30min grâce à un afficheur 7 segments - 4digits. Cela me donne quelque chose dans ce genre là : http://thepolyscope.com/fr/afficheur-7-segments-4-digits-arduino/
J'ai le code suivant qui fonctionne très bien et se lance à l'alimentation de l'arduino :
// Include library found at
// SevSeg library 2.3.zip - Google Drive
#include "SevSeg.h"
const int debit = 9600;
int ledPin = 0;
int speakerOut = 13;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
int flag1=0;
//Create an instance of the object.
SevSeg sevseg;
unsigned long timer;
int Sec=1;
int dSec=0;
int Min=0;
int dMin=3;
int temps=0;
int desam=A1;
int explo=A3;
void setup() {
//
// Set up pins
// Arguments :
// First 0 is for common cathode
// Following 0 to 11 numbers are for Arduino pins
// connected to display in the following order
// 1,2,3,4,A,B,C,D,E,F,G,DP
sevseg.Begin(0,0,1,2,3,4,5,6,7,8,9,10,11);
pinMode(desam,INPUT);
pinMode(explo,INPUT);
}
void loop() {
do{
//Produce an output on the display
sevseg.PrintOutput();
//Check if 10ms has elapsed
unsigned long mils=millis();
if (mils-timer>=991) {
timer=mils;
Sec--;
int valdesam=analogRead(desam);
int valexplo=analogRead(explo);
if (valdesam<=1000){ goto desam;}
if (valexplo<=1000){ goto explo;}
if (Sec==-1) { // Reset to 0 after counting for 100 seconds.
Sec=9;
dSec=dSec-1;
}
if (dSec==-1) { // Reset to 0 after counting for 100 seconds.
dSec=5;
Min=Min-1;
}
if (Min==-1) { // Reset to 0 after counting for 100 seconds.
Min=9;
dMin=dMin-1;
}
if (dMin==-1) { // Reset to 0 after counting for 100 seconds.
Min=9;
dMin=dMin-1;
}
temps=Sec+10dSec+100Min+1000*dMin;
//Update the number to be displayed, with a decimal
//place in the correct position.
sevseg.NewNum(temps,(byte) 2);
}
}while (dMin>=-1);
goto explo;
desam :;
do{
sevseg.PrintOutput();
if (flag1==0) {
Serial.begin(debit);
Serial.println("désamorcé");
Serial.end();
flag1++;
}
}while(1);
explo :;
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
do{}while(1);
}
}
}
}
}
Par contre dès que j'essaie de lancer le programme au déclechement d'une entrée, j'ai plus que les 2 derniers digits des secondes qui fonctionnent. J'ai repéré que cela est dû au SerialBegin mais je ne sais pas comment remplacer cela. J'ai besoin que le programme se lance lorsqu'une entrée passe à 1.
J'ai fait le programme suivant :
// Include library found at
// SevSeg library 2.3.zip - Google Drive
#include "SevSeg.h"
const int debit = 9600;
int ledPin = 0;
int speakerOut = 13;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
int signal = A5; // Signal recepteur
int Val1;
int flag1=0;
//Create an instance of the object.
SevSeg sevseg;
unsigned long timer;
int Sec=1;
int dSec=0;
int Min=0;
int dMin=3;
int temps=0;
int desam=A1;
int explo=A3;
void setup() {
Serial.begin(9600); // Vitesse de transmission
pinMode(signal, INPUT);
//
// Set up pins
// Arguments :
// First 0 is for common cathode
// Following 0 to 11 numbers are for Arduino pins
// connected to display in the following order
// 1,2,3,4,A,B,C,D,E,F,G,DP
sevseg.Begin(0,0,1,2,3,4,5,6,7,8,9,10,11);
pinMode(desam,INPUT);
pinMode(explo,INPUT);
}
void loop() {
Val1 = analogRead(signal); //Alors stocker la valeur du potard dans Val1
Serial.print("Valeur = "); // Ecrit sur l’écran "Valeur = "
Serial.println(Val1); // Ecrit sur l’écran le contenu de Val1
delay (1000); // Attente 1 seconde
if(Val1>1000)
{
do{
//Produce an output on the display
sevseg.PrintOutput();
//Check if 10ms has elapsed
unsigned long mils=millis();
if (mils-timer>=991) {
timer=mils;
Sec--;
int valdesam=analogRead(desam);
int valexplo=analogRead(explo);
if (valdesam<=1000){ goto desam;}
if (valexplo<=1000){ goto explo;}
if (Sec==-1) { // Reset to 0 after counting for 100 seconds.
Sec=9;
dSec=dSec-1;
}
if (dSec==-1) { // Reset to 0 after counting for 100 seconds.
dSec=5;
Min=Min-1;
}
if (Min==-1) { // Reset to 0 after counting for 100 seconds.
Min=9;
dMin=dMin-1;
}
if (dMin==-1) { // Reset to 0 after counting for 100 seconds.
Min=9;
dMin=dMin-1;
}
temps=Sec+10dSec+100Min+1000*dMin;
//Update the number to be displayed, with a decimal
//place in the correct position.
sevseg.NewNum(temps,(byte) 2);
}
}while (dMin>=-1);
goto explo;
desam :;
do{
sevseg.PrintOutput();
if (flag1==0) {
Serial.begin(debit);
Serial.println("désamorcé");
Serial.end();
flag1++;
}
}while(1);
explo :;
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
do{}while(1);
}
}
}
}
}
}
Savez vous d'ou peut venir mon problème ?
Merci