salve questo programma è una sorta di bomba giocattolo che dovrei usare, in questo momneto funzoiona con sei fili da tagliare 2 di questi la scoppiano, 2 non fanno nulla , 2 la disarmano.
il programma genera a ogni ravvio tramite il random una nuova sequenza per far si che sia sempre diversa (la stampa sulla seriale ma non abbiamo un pc da lasciare sempre acceso), ma a me servirebbe che la sequenza (non importa quale sia, magari mischiata tipo detona,disarma nulla detona disarma, nulla ) fosse sempre la stessa, ma non riesco a forzarla.. mi dareste una mano, magari spiegandomi nel limite del possibile come avete fatto? perchè nella sua banalità mi ha bloccato un progetto.. vi ringrazio e inserisco il codice.
int used[7] = {0, 0, 0, 0, 0, 0};
int dead[3] = {-1, -1};
int gameOver = 0;
int deadIndex = 0;
int second=0, minute=10, hour=0;
int speakerPin = 10;
long previousTime = 0;//holds previous time in ms
long previousDelay = 0;//holds previous time in ms since buzzer was active
long interval = 60000;//60 second delay
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long ledInterval = 1000; // interval at which to blink (milliseconds)
int repeatBuzzer = 0;
void setup() {
Serial.begin(9600);
//randomize();
//delay(1000);
randomSeed(analogRead(0));
initialize();
randomize();
}
void loop() {
countdown();
int wire = -1;
wire = pinCheck();//check for wires that have been removed
if(wire != -1 && gameOver == 0){
if(wire == 0 || wire == 1){
digitalWrite(8, HIGH);//detonate
detonate();
gameOver = 1;
}
if(wire == 2 || wire == 3){
gameOver = 0;//no effect
dead[deadIndex] = used[wire];
deadIndex++;
}
if(wire == 4 || wire == 5){
digitalWrite(9, HIGH); //disarmed
gameOver = 1;
}
}//end if wire!= -1
if(minute >= 1){
if(minute <= 8){
digitalWrite(14, HIGH);
}
if(minute <= 6){
digitalWrite(15, HIGH);
}
if(minute <= 4){
digitalWrite(16, HIGH);
}
if(minute <= 2){
digitalWrite(17, HIGH);
}
if(minute <= 1){
digitalWrite(18, HIGH);
}
}//end if minute > 1
if(minute < 1 && second <= 60 && second > 30){
blinkLast();
}
if(minute < 1 && second <= 30){
blinkAll();
}
if(hour == 0 && minute == 0 && second == 0){
digitalWrite(8, HIGH);//detonate
detonate();
gameOver = 1;
}
while(gameOver);
}//end main loop
void randomize() {
for(int i=0; i < 6; i++){
int randomNumber = -9;
do {
randomNumber = random(1,7); //if the pin has been used, choose another
} while (search(randomNumber, used) == 1);//close while
//Serial.println(randomNumber);
used[i] = randomNumber;
}//close for
int foo;
for (foo = 0; foo < 6; foo = foo + 1) {
Serial.println(used[foo]);
}
}//close randomize();
int search(int i, int a[7]){
int flag = 0;
for (int c=0; c < 6; c++){
if (a[c] == i){
flag = 1;
}
}
return flag;
}
int pinCheck(){
int position = -1;
for (int i=0;i<6; i++){
int val =0;
val = digitalRead((used[i] + 1));
if(val == HIGH && (used[i] != dead[0] || used[i] != dead[1])){
position = i;
}
}//end for loop
return position;
}//end pinCheck();
void initialize(){
pinMode(9, OUTPUT);//green LED/bomb disarmed
pinMode(8, OUTPUT);//red LED/bomb detonated
pinMode(speakerPin, OUTPUT);
for (int i=14;i<19;i++){
pinMode(i, OUTPUT);//set analog LED pins as outputs
}
for (int i=0;i<6;i++){
pinMode((i+2), INPUT);//set pins 2 through 7 as input
}
}
/***********************************************************
* Main countdown timer *
* countdown() *
************************************************************/
void countdown(){
static unsigned long lastTick = 0; // set up a local variable to hold the last time we decremented one second
static unsigned long currentMillis = 0;
// (static variables are initialized once and keep their values between function calls)
// decrement one second every 1000 milliseconds
if (second > 0) {
if (millis() - lastTick >= 1000) {
lastTick = millis();
second--;
Serial.println(minute);
Serial.println(" : ");
Serial.println(second);
}
}
// decrement one minute every 60 seconds
if (minute > 0) {
if (second <= 0) {
minute--;
second = 60; // reset seconds to 60
}
}
// decrement one hour every 60 minutes
if (hour > 0) {
if (minute <= 0) {
hour--;
minute = 60; // reset minutes to 60
}//closes if
}//closes if
//the code below beeps the siren once every minute.
currentMillis = millis();
if (currentMillis - previousTime > interval)
{
previousTime = currentMillis;
previousDelay = currentMillis;
digitalWrite(speakerPin, HIGH);//turn on buzzer
}
if (currentMillis - previousDelay > 100) {//100ms chirp duration
digitalWrite(speakerPin, LOW);//turn off buzzer
}
} //close countdown();
void blinkLast() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > ledInterval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(18, ledState);
}
}
void blinkAll() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > ledInterval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(18, ledState);
digitalWrite(17, ledState);
digitalWrite(16, ledState);
digitalWrite(15, ledState);
digitalWrite(14, ledState);
}
}
void detonate(){
if (repeatBuzzer == 0) { //make sure buzzer for loop has not already been run.
digitalWrite(speakerPin, HIGH);//turn on buzzer
delay(5000);//wait 5 seconds
digitalWrite(speakerPin, LOW);//turn off buzzer
repeatBuzzer = 1;//set flag to prevent code from looping again.
}
}