Help with building an alarm clock with fading lights, music and a touch screen

Keep using the same topic if you can. Otherwise you have to explain the whole project over again for each new topic, answer the same questions, post the same links to your components, post same schematics, code....

There are times when starting a new topic can be appropriate, but as a beginner, it can be hard to know when those times are. It's safer to assume you should use the same topic, because if you start a new one, other forum members may flag the new topic to the moderators as being a "cross-post". Cross-posting is severely frowned upon, because it can waste so much of everybody's time.

Ok, that's true.

But I have two tips for you.

  1. Those buttons with 4 pins can be tricky. Two of the pins are permanently connected to the two other pins. All 4 pins are connected only when the button is pressed. So it's easy to get wrong. But if you connect your wires to a diagonal pair of pins, and leave the other diagonal pair unconnected, that always works.

  2. You don't usually need the pull-up resistor. Just connect the button between the Arduino pin and ground, and set the pin mode to INPUT_PULLUP to engage the Arduino's internal pull-up resistor. If your button will be on the end of a very long pair of wires, like a couple of meters or more, or the circuit will be used in an electrically noisy environment, then an external pull up resistor is a good idea. But you can still use INPUT_PULLUP anyway, it can only help.

  // Snooze button
  if (SnoozePressed) {
    if (digitalRead(SnoozePin) == LOW) {
      SnoozePressed = 0;
    }
  } else {
    if (digitalRead(SnoozePin) == HIGH) {
      SnoozePressed = 1;
    }
    if (Alarmstate == 0) {
      Alarmstate = 1;

You do realise that the pin will read LOW when the button is pressed, and HIGH when not pressed?

What is your variable "SnoozePressed" meant to indicate?

Your code is not currently doing that. It is detecting when the button is pressed or not pressed, but it is not detecting when the button changes from being not pressed to being pressed (or vice versa). Have you seen and understood the "state change" example sketch in the IDE?

Okay, I am getting somewhere thanks a lot!

#include <DFRobotDFPlayerMini.h>

// Create the Player object
DFRobotDFPlayerMini player;

//---------------------------------------------------------------------------------------------------------

//declarations for the LED fade
const byte pwmLED = 46;             //LED PWM pin
const int minPWM = 0;
const int maxPWM = 180;             // constants for min and max PWM (LED intensity)
int fadeValue = 0;                  // Global Fade Value starting with no light
byte fadeIncrement = 1;             // How smooth to fade?
unsigned long previousFadeMillis;   // millis() timing Variable, just for fading
int fadeInterval = 5000;            // How fast to increment?

// declarations for the volume fade
unsigned long previousFadeMillis_volume;
byte volumeIncrement = 1;
int volumeInterval = 5000;
int volumeValue = 5;
const int maxvolume = 20;

//---------------------------------------------------------------------------------------------------------

//Button
const int SnoozePin = 22;           // the number of the pushbutton pin
int Alarmstate = 0;                 //Current state of the Alarm
int lastAlarmstate = 0;             //Previous state of the Alarm

//int SnoozePressed = 0;            //Should the alarm be playing?
//int buttonState = 0;              // variable for reading the pushbutton status
//int alarmState = 0;               // variable for reading the alarm status

//---------------------------------------------------------------------------------------------------------

void setup() {

  // put pwmLED into known state (off)
  analogWrite(pwmLED, fadeValue);

  //Button
  pinMode(SnoozePin, INPUT_PULLUP);

  //MP3
  // Init USB serial port for debugging
  Serial.begin(9600);
  // Init serial port for DFPlayer Mini
  Serial1.begin(9600);

  // Start communication with DFPlayer Mini
  if (player.begin(Serial1)) {
    Serial.println("OK");

  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}

void loop() {

  //---------------------------------------------------------------------------------------------------------
  // Serial Communication (Bug checker)

  Serial.println(millis());
  Serial.print("Volume value:"); Serial.println(volumeValue);
  Serial.print("Fade value:"); Serial.println(fadeValue);

  //---------------------------------------------------------------------------------------------------------
  // Snooze button
  // read the snoozebutton input pin:
  Alarmstate = digitalRead(SnoozePin);


  if (Alarmstate != lastAlarmstate) {      // compare the buttonState to its previous state
    if (Alarmstate == HIGH) {              // if the state has changed,
      // if the current state is HIGH then the button went from off to on:
      Serial.print("Alarm state:");
      Serial.println("on ////////////////////////////");
      // Play alarm if the state is high
      player.volume(volumeValue);
      player.play(1);
      unsigned long currentMillis = millis();
      playAlarm(currentMillis);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.print("Alarm state:");
      Serial.println("off //////////////////////");
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state, for next time through the loop
  lastAlarmstate = Alarmstate;
}

//---------------------------------------------------------------------------------------------------------
//Alarm Code
void playAlarm(unsigned long thisMillis)
{
  // LED fade
  // is it time to update yet?, if not, nothing happens
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    // yup, it's time!
    fadeValue = fadeValue + fadeIncrement;
    if (fadeValue >= maxPWM) {
      // At max, limit
      fadeValue = maxPWM;
    }
    // Only need to update when it changes
    analogWrite(pwmLED, fadeValue);
    // reset millis for the next iteration (fade timer only)
    previousFadeMillis = thisMillis;
  }
  //MP3 fade
  if (thisMillis -  previousFadeMillis_volume >= volumeInterval) {
    // yup, it's time!
    volumeValue =  volumeValue + volumeIncrement;
    if (volumeValue >= maxvolume) {
      // At max, limit
      volumeValue = maxvolume;
    }
    // Only need to update when it changes
    player.volume(volumeValue);
    // reset millis for the next iteration (fade timer only)
    previousFadeMillis_volume = thisMillis;
  }
}

But now the alarm restarts when I press the button and I want it to stop. I am still not sure how to do that. Is there a way to stop the function from running?
I think it has something to do with the button being switched back, but I can not get my head around how I should fix that...

      // if the current state is HIGH then the button went from off to on:

Are you sure about that?

Okay I solved it. I figured that my basic knowledge was holding me back too much, I am now following a basic arduino course.
Here I learned more about the conditions, which really helped.
This piece of code now does what I want :slight_smile:

  // Snooze button
  // read the snoozebutton input pin:
  Snoozestate = digitalRead(SnoozePin);

  if ((Snoozestate == LOW) && (Alarmstate == LOW)) {
    Alarmstate = HIGH;
    Serial.print("Alarm state:");
    Serial.println("on ////////////////////////////");
    player.volume(volumeValue);         //Muziek aan
    player.play(1);                     //Muziek aan
  }
  else if ((Snoozestate == LOW) && (Alarmstate == HIGH)) {
    Alarmstate = LOW;
    Serial.print("Alarm state:");
    Serial.println("off ////////////////////////////");
    player.pause();                     //Muziek uit
    volumeValue = 5;                    //Muziek uit
    fadeValue = 0;                      //Lamp uit
    analogWrite(pwmLED, fadeValue);     //Lamp uit
  }

  if (Alarmstate == HIGH) {             //Alleen als het alarm mag spelen
    unsigned long currentMillis = millis();
    playAlarm(currentMillis);
  }

  delay(100);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.