Untitled

 avatar
unknown
java
2 years ago
2.1 kB
5
Indexable
#include <TimeLib.h>
/* Broches */
const byte PIN_LED_R = 9;
const byte PIN_LED_G = 10;
const byte PIN_LED_B = 11;

const int btnPin = A0;
unsigned long currentTime = 0;
unsigned long startTime = 0;

// Fonction setup(), appelée au démarrage de la carte Arduino
void setup() {

  Serial.begin(9600);
  // Initialise les broches
  pinMode(PIN_LED_R, OUTPUT);
  pinMode(PIN_LED_G, OUTPUT);
  pinMode(PIN_LED_B, OUTPUT);
  displayColor(0, 0, 0);
}

// Fonction loop(), appelée continuellement en boucle tant que la carte Arduino est alimentée
void loop() {

  int btnVal = analogRead(btnPin);

  // Check if the button is pressed
  if (btnVal < 50) {
    Serial.println("pressed");

    startTime = millis();
  }

  // Calculate the elapsed time
  currentTime = millis();
  unsigned long elapsedTime = (currentTime - startTime) / 1000 / 60 / 60;


  Serial.println(elapsedTime);
  delay(1000);

  switch (elapsedTime) {
    case 0:
      {
        displayColor(7, 252, 3);
        break;
      }
    case 1:
      {
        displayColor(78, 252, 3);
        break;
      }
    case 2:
      {
        displayColor(252, 110, 3);

        break;
      }
    case 3:
      {
        displayColor(252, 110, 3);

        break;
      }
    case 4:
      {
        displayColor(252, 90, 3);

        break;
      }
    case 5:
      {
        displayColor(252, 70, 0);

        break;
      }
    case 6:
      {
        displayColor(252, 70, 0);

        break;
      }
    case 7:
      {
        displayColor(252, 70, 0);

        break;
      }
    case 8:
      {
        displayColor(252, 70, 0);

        break;
      }
    default:
      {
        displayColor(0, 42, 255);
        break;
      }
  }
}

/** Affiche une couleur */
void displayColor(byte r, byte g, byte b) {

  // Assigne l'état des broches
  // Version cathode commune
  analogWrite(PIN_LED_R, r);
  analogWrite(PIN_LED_G, g);
  analogWrite(PIN_LED_B, b);
  // Version anode commune
  //analogWrite(PIN_LED_R, ~r);
  //analogWrite(PIN_LED_G, ~g);
  //analogWrite(PIN_LED_B, ~b);
}
Editor is loading...