Scheda controllo luci presepe

Buongiorno a tutti. Avrei bisogno di un piccolo aiuto. Ho una centralina luci che monta ATmega 328 (Arduino UNO) programmato come da listato allegato. Vorrei fare due modifiche ma non riesco.

  1. modifica regolare la tensione in uscita per l'accesione delle stelle da 12 v a 6 v

  2. Vorrei modificare l'audio tramite modulo miniPlayer in modo che suoni di continuo riprendendo la musica dall'inizio.

Grazie

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>


const int P1 = 7;
const int P2 = 4;
const int P3 = 2;


const int OUT1 = 5;
const int OUT2 = 6;
const int OUT3 = 9;
const int OUT4 = 10;
const int OUT5 = 11;
const int OUT6 = 3;
const int CHANGESTATE = 12;
const int LEDPLAY = 13;
const int NEOPIXEL = 8;

const int TRIM1 = A0;
const int TRIM2 = A1;
const int TRIM3 = A2;
const int TRIM4 = A3;
const int TRIM5 = A4;
const int TRIM6 = A5;


//#define DEBUG
#define STATE_DAY 0
#define STATE_SUNSET 1
#define STATE_NIGHT 2
#define STATE_SUNRISE 3

#define TRIMMER_DAY 0
#define TRIMMER_SUNSET 1
#define TRIMMER_NIGHT 2
#define TRIMMER_SUNRISE 3
#define TRIMMER_EXT 4
#define TRIMMER_VOL 5

#define OUT_SUN 0
#define OUT_STARS 1
#define OUT_COMET 2
#define OUT_FIRE 3

int fsm_state;
int fsm_stateold;
unsigned long runtime;
unsigned long time_day;
unsigned long time_sunset;
unsigned long time_night;
unsigned long time_sunrise;
unsigned long time_trim;
unsigned long time_debug;
unsigned long time_sunset_running;
unsigned long time_comet_running;
unsigned long time_sunrise_running;
unsigned long time_fire_running;
unsigned long time_volume_running;

int valuesun = 0;
int valuestars = 0;
int valuecomet = 0;
boolean startFire;
boolean startVol = false;
boolean stopVol = false;
int valuehighfire = 50;
int valuelowfire = 0;

boolean playing = true;
int SwitchPressed = 0;
//int VolOld=0;
int volume = 0;




uint8_t trimmer[] = {
  TRIM1, TRIM4, TRIM2, TRIM3, TRIM5, TRIM6
};

unsigned long trimvalue[6] = { 0, 0, 0, 0, 0, 0 };

uint8_t out[] = {
  OUT6, OUT2, OUT1, OUT3, OUT5, OUT6
};




void setup() {
  // declare pin 9 to be an output:
  pinMode(OUT1, OUTPUT);
  pinMode(OUT2, OUTPUT);
  pinMode(OUT3, OUTPUT);
  pinMode(OUT4, OUTPUT);
  pinMode(OUT5, OUTPUT);
  pinMode(OUT6, OUTPUT);
  pinMode(LEDPLAY, OUTPUT);
  pinMode(CHANGESTATE, OUTPUT);
  pinMode(TRIM1, INPUT);
  pinMode(TRIM2, INPUT);
  pinMode(TRIM3, INPUT);
  pinMode(TRIM4, INPUT);
  pinMode(TRIM5, INPUT);
  pinMode(TRIM6, INPUT);
  pinMode(P1, INPUT_PULLUP);
  pinMode(P2, INPUT_PULLUP);
  pinMode(P3, INPUT_PULLUP);


  delay(1000);
  Serial.begin(9600);
  mp3_set_serial(Serial);  //set Serial for DFPlayer-mini mp3 module
  setvolume(10);

  fsm_state = STATE_DAY;
  time_day = millis();
  time_debug = millis();
  time_trim = millis();
  trimvalue[fsm_state] = ReadTrimmer(fsm_state) * 1000;

  SwitchPressed = checkSwitch();
  playtrack(1);
}

void loop() {
  runtime = millis();


  if (runtime > time_debug + 1000) {
#ifdef DEBUG
    Serial.print("runtime: ");
    Serial.println(runtime);
    Serial.print("fsm_state: ");
    Serial.println(fsm_state);
    switch (fsm_state) {
      case STATE_DAY:
        Serial.print("time_day: ");
        Serial.println(time_day + (trimvalue[TRIMMER_DAY]));
        Serial.print("TRIMMER_DAY: ");
        Serial.println((trimvalue[TRIMMER_DAY]));
        Serial.println("-----------------");
        break;
      case STATE_SUNSET:
        Serial.print("time_sunset: ");
        Serial.println(time_sunset + (trimvalue[TRIMMER_SUNSET]));
        Serial.print("TRIMMER_SUNSET: ");
        Serial.println((trimvalue[TRIMMER_SUNSET]));
        Serial.print("valuesun: ");
        Serial.println(valuesun);
        Serial.print("valuestars: ");
        Serial.println(valuestars);
        Serial.println("-----------------");
        break;
      case STATE_NIGHT:
        Serial.print("time_night: ");
        Serial.println(time_night + (trimvalue[TRIMMER_NIGHT]));
        Serial.print("TRIMMER_NIGHT: ");
        Serial.println((trimvalue[TRIMMER_NIGHT]));
        Serial.print("valuecomet: ");
        Serial.println(valuecomet);
        Serial.println("-----------------");
        break;
      case STATE_SUNRISE:
        Serial.print("time_sunrise: ");
        Serial.println(time_sunrise + (trimvalue[TRIMMER_SUNRISE]));
        Serial.print("TRIMMER_SUNRISE: ");
        Serial.println((trimvalue[TRIMMER_SUNRISE]));
        Serial.print("valuesun: ");
        Serial.println(valuesun);
        Serial.print("valuestars: ");
        Serial.println(valuestars);
        Serial.print("valuecomet: ");
        Serial.println(valuecomet);
        Serial.println("-----------------");
        break;
    }
#endif

    if (fsm_state != fsm_stateold) {
      digitalWrite(CHANGESTATE, HIGH);
      fsm_stateold = fsm_state;
    } else {
      digitalWrite(CHANGESTATE, LOW);
    }

    time_debug = millis();
  }

  if (runtime > time_trim + 200) {
    for (int i = 0; i < 6; i++) {
      trimvalue[i] = (ReadTrimmer(i));
      trimvalue[i] = trimvalue[i] * 100;
      delay(2);
    }
    time_trim = millis();
  }



  switch (fsm_state) {
    case STATE_DAY:

      digitalWrite(OUT4, LOW);
      digitalWrite(OUT5, LOW);
      valuesun = 255;
      SetOut(OUT_SUN, valuesun);


      if (runtime > time_day + (trimvalue[TRIMMER_DAY])) {
        fsm_state = STATE_SUNSET;
        stopVol = false;
        valuehighfire = 10;
        valuelowfire = 0;
        time_sunset = millis();
        time_sunset_running = millis();
        break;
      }

      gestAudio(fsm_state);

      break;

    case STATE_SUNSET:

      if (runtime > time_sunset_running + (trimvalue[TRIMMER_SUNSET] / 255)) {  //sostituire la risoluzione con il valore di un trimmer
        if (valuesun > 0) valuesun--;
        if (valuestars < 254) valuestars++;
        SetOut(OUT_SUN, valuesun);
        SetOut(OUT_STARS, valuestars);
        time_sunset_running = millis();
      }

      if (runtime > time_sunset + ((trimvalue[TRIMMER_SUNSET]) / 2)) {
        startFire = true;
      }

      if (startFire == true && (runtime > time_fire_running + 20)) {
        if (valuehighfire < 200) valuehighfire++;
        if (valuelowfire < 50) valuelowfire++;
        SetFire(valuelowfire, valuehighfire);
        time_fire_running = millis();
      }

      if (runtime > time_sunset + (trimvalue[TRIMMER_SUNSET])) {
        startFire = false;
        stopVol = false;
        fsm_state = STATE_NIGHT;
        SetOut(OUT_SUN, 0);
        SetOut(OUT_STARS, 255);
        time_night = millis();
        break;
      }

      gestAudio(fsm_state);

      break;

    case STATE_NIGHT:

      analogWrite(OUT4, 255);
      analogWrite(OUT5, 030);



      if (runtime > time_comet_running + ((trimvalue[TRIMMER_NIGHT] / 255) / 4)) {  //la cometa ci mette 1/4 del tempo impostato dal trimmer della notte per salire a regime
        if (valuecomet < 254) valuecomet++;
        SetOut(OUT_COMET, valuecomet);
        time_comet_running = millis();
      }

      if (runtime > time_fire_running + 20) {  //la cometa ci mette 1/4 del tempo impostato dal trimmer della notte per salire a regime
        SetFire(50, 200);
        time_fire_running = millis();
      }


      if (runtime > time_night + (trimvalue[TRIMMER_NIGHT])) {
        fsm_state = STATE_SUNRISE;
        stopVol = false;
        startFire = true;
        SetOut(OUT_STARS, 0);
        time_sunrise = millis();
        break;
      }

      gestAudio(fsm_state);

      break;

    case STATE_SUNRISE:
      if (runtime > time_comet_running + ((trimvalue[TRIMMER_SUNRISE] / 255) / 4)) {  //la cometa ci mette 1/4 del tempo impostato dal trimmer dell'alba per spegnersi
        if (valuecomet > 0) valuecomet--;
        SetOut(OUT_COMET, valuecomet);
        time_comet_running = millis();
      }

      if (runtime > time_sunrise + ((trimvalue[TRIMMER_SUNRISE]) / 2)) {  //la cometa ci mette 1/4 del tempo impostato dal trimmer della notte per salire a regime
        startFire = false;
        SetOut(OUT_FIRE, 0);
      }

      if (startFire == true && (runtime > time_fire_running + 20)) {  //la cometa ci mette 1/4 del tempo impostato dal trimmer della notte per salire a regime
        if (valuehighfire > 10) valuehighfire--;
        if (valuelowfire > 0) valuelowfire--;
        SetFire(valuelowfire, valuehighfire);
        time_fire_running = millis();
      }

      if (runtime > time_sunrise_running + (trimvalue[TRIMMER_SUNRISE] / 255)) {  //sostituire la risoluzione con il valore di un trimmer
        if (valuesun < 254) valuesun++;
        if (valuestars > 0) valuestars--;
        SetOut(OUT_SUN, valuesun);
        SetOut(OUT_STARS, valuestars);
        time_sunrise_running = millis();
      }

      if (runtime > time_sunrise + (trimvalue[TRIMMER_SUNRISE])) {
        fsm_state = STATE_DAY;
        stopVol = false;
        SetOut(OUT_SUN, 255);
        SetOut(OUT_STARS, 0);
        time_day = millis();
        break;
      }


      gestAudio(fsm_state);

      break;
  }


  if (SwitchPressed != checkSwitch()) {
    SwitchPressed = checkSwitch();
    if (SwitchPressed != 0) {
      if (!playing) {
#ifdef DEBUG
        Serial.println("Play");
#endif
        playing = true;
        int i = fsm_state;
        i++;
        if (i == 5) i = 1;
        playtrack(i);
        //          gestAudio(fsm_state);
      } else {
#ifdef DEBUG
        Serial.println("Stop");
#endif
        mp3_stop();
        digitalWrite(LEDPLAY, LOW);
        playing = false;
      }
    }
  }

  if ((volume != (trimvalue[TRIMMER_VOL] / 3500)) && (stopVol == true)) {
#ifdef DEBUG
    Serial.print("*********  Volume ");
    Serial.print(volume);
    Serial.print(" - Trimm ");
    Serial.println((trimvalue[TRIMMER_VOL] / 3500));
#endif
    volume = (trimvalue[TRIMMER_VOL] / 3500);
    setvolume(volume);
  }
}


int ReadTrimmer(int id) {
  return (analogRead(trimmer[id]));
}

void SetOut(int id, int value) {
  analogWrite(out[id], value);
}

void SetFire(int low, int high) {
  int randNumber = random(low, high);
  SetOut(OUT_FIRE, randNumber);
}

int checkSwitch() {
  if (!digitalRead(P1)) return (1);
  if (!digitalRead(P2)) return (2);
  if (!digitalRead(P3)) return (3);
  return (0);
}

void playtrack(int track) {
#ifdef DEBUG
  Serial.println("Playtrack");
#endif
  mp3_play(track);
  delay(100);
  mp3_single_loop(true);
  //        playing=true;
  delay(100);
  digitalWrite(LEDPLAY, HIGH);
}

void setvolume(int volume) {
  mp3_set_volume(volume);
#ifdef DEBUG
  Serial.print("Vol ");
  Serial.println((volume));
#endif
}

void gestAudio(int track) {

  //  digitalWrite (LEDPLAY,LOW);
  if (playing == true) {
    //      digitalWrite (LEDPLAY,HIGH);
    track++;
    if (track == 5) track = 1;
    if (stopVol == false) {
      if (runtime > time_volume_running + 250) {
        if (startVol == false) {
          if (volume > 3) {
            volume = volume - 3;
#ifdef DEBUG
            Serial.print("dec ");
            Serial.println(volume);
#endif
          } else {
            playtrack(track);
            delay(100);
            startVol = true;
          }
        } else {
          if (volume < (trimvalue[TRIMMER_VOL] / 3500)) {
            volume = volume + 3;
#ifdef DEBUG
            Serial.print("inc ");
            Serial.println(volume);
#endif
          } else {
            startVol = false;
            stopVol = true;
#ifdef DEBUG
            Serial.println("finito ciclo");
#endif
          }
        }
        time_volume_running = millis();
        setvolume(volume);
        delay(100);
      }
    }
  }
}

Ti segnalo che, nella sezione in lingua Inglese, si può scrivere solo in Inglese ... quindi, per favore, la prossima volta presta più attenzione in quale sezione metti i tuoi post; questa volta esso è stato spostato, da un moderatore della sezione di lingua Inglese, nella sezione di lingua Italiana.

Grazie.

Per cortesia, metti il programma come testo nel messaggio, anzichè allegato, in maniera che sia subito visibile per tutti anziché doverlo scaricare, inserire in una cartella con lo stesso nome e aprirlo con l'IDE.

Attenzione che e’ postato come testo regolare, e qundi alcuni gruppi di caratteri potrebbero essere interpretati dallo script del forum come degli stupidissimi comandi “markdown” e quindi andare persi.

Per favore, modifica il post racchiudendo tutta la parte del listato dentro i tag “code”, cosi oltre ad essere piu ordinato e comprensibile, non si rischia che venga modificato dallo script (sarebbe anche richiesto dal regolamento, in effetti :wink: )

Fai anche la corretta indentazione (puoi farla automaticamente nell'IDE con CTRL+T), altrmenti non si capisce nulla!

Se usi lampadine da 6V, comunque, devi alimentarle a 6V, oppure collegarle a due a due in serie.

Ma è di nuovo questo?...

Puoi chiedere semplicemente di riaprire la discussione!

fatto. Spero che adesso qualcuno possa aiutarmi

Era il lontano 2020...

Si ma ora il problema è diverso. Voglio diminuire la tensione all’uscita Stelle, inoltre vorrei che il player suonasse sempre indipendentemente dalle fasi delle luci
Grazie