Sommerkunstakademie 2024 Science Workshop

Willkommen zum Science Workshop der Sommerkunstakademie 2024

Workshopleitung: Christoph Reisinger - office@diearte.at

Try it on Wokwi.com!

Projektbeschreibung

Im Science Workshop wurden mittels eines Arduino, Sensoren, Leds, Batterie und natürlich mehrerer Drähte eine Alarmanlage gebastelt. Um dieses Projekt nachzubauen ist hier eine Komponenten Liste angefügt. Des Weiteren ist hier auch noch der Arduino Code ersichtlich, der sich natürlich auch anpassen lässt!

Komponenten

Arduino Code

/**************************************************************************
 This is the code for the Sommerkunstakademie 2024 Science Workshop Alarm Build

Workshoplead:
 Christoph Reisinger
 office@diearte.at
 www.diearte.at
 
The device has 2 main S
States: IDLE and ALARM.
It captures the Sensor-Value and if the value is HIGH, the device switches to ALARM.

During IDLE, the Device switches through multiple messages on the OLED Display. 
During ALARM, the 2 LEDs blink alternately, the Buzzer produces an alarm sound and the display displays some alarming text.

IDLE is resumed when no Sensor input is detected (after a short delay)
**************************************************************************/

#include 
#include 
#include 
#include 

// Screen init
#define SCREEN_WIDTH  128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Pins 
const int buzzer = 10;
const int led1 = 12;
const int led2 = 13;
const int sensor = 2;
const int IDLEinterval = 5000;

int interval = 250;
int state = LOW;
int sensorValue = 0;
int alarmState = 0;
int IDLEState = 0;

// timetracking
unsigned long startTime = 0;
unsigned long stampTime = 0;
unsigned long IDLEcurrentMillis = 0;
unsigned long IDLEpreviousMillis = 0;
unsigned long previousMillis = 0;

void setup() 
{
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever 
  }
  startTime = millis();
  display.display();
  pinMode(led1, OUTPUT);      // initalize LED as an output
  pinMode(led2, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input  
  
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}

// SHOW IDLE MESSAGE
void display_idle_message(int IDLEState)
{
  display.clearDisplay();

  if (IDLEState == 0) // DIECE: This and the if below should be a switch for readability. Applies to the alarm function below as well
  {
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.println("SOMMERKUNSTAKADEMIE");

    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0, 9);
    display.println("2024");
    
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 25);
    display.println("SCIENCE WORKSHOP");
  }

  if (IDLEState == 1)
  {
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.println("Christoph");

    display.setCursor(0, 16);
    display.println("Reisinger");
  }
  display.display();
}

// SHOW ALARM MESSAGES
void display_alarm_message(int alarmState)
{
  display.setTextSize(2);
  display.setTextColor(WHITE);
  if (alarmState == 0)
  {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println(" --------- ");
    display.setCursor(0, 10);
    display.println(" ! ALARM !");
    display.setCursor(0, 20);
    display.println(" --------- ");
    display.display();
  }
  if (alarmState == 1)
  {
    display.clearDisplay();
    display.setCursor(0, 10);
    display.println(" !!ALARM!!");
    display.display();
  }
  if (alarmState == 2)
  {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println(" --------- ");
    display.setCursor(0, 10);
    display.println("  !ALARM! ");
    display.setCursor(0, 20);
    display.println(" --------- ");
    display.display();  
  }
  if (alarmState == 3)
  {
    display.clearDisplay();
    display.setCursor(0, 10);
    display.println(" !!ALARM!!");
    display.display();
  }
}

//main loop
void loop() 
{
  unsigned long currentMillis = millis();
  unsigned long IDLEcurrentMillis = millis();
  
  sensorValue = digitalRead(sensor);   // read sensor value
  
  unsigned long currentTime = millis();
  unsigned long elapsedTime = currentTime - startTime;

  // Check Sensor value (if motion is detected)
  if (sensorValue == HIGH)
    {
      if ((currentMillis  - previousMillis ) >= interval)
      {
        previousMillis = currentMillis;
        alarmState = (alarmState + 1) % 4;
        display_alarm_message(alarmState);  
        if (alarmState % 2 == 1)
        {
          digitalWrite(led1, HIGH);   // turn LED LOW
          digitalWrite(led2, LOW);   // turn LED LOW
          tone(buzzer, 400);
        }else
        {
          digitalWrite(led2, HIGH);   // turn LED LOW       
          digitalWrite(led1, LOW);   // turn LED LOW
          tone(buzzer, 800);
        }
      }
    }else
    {
      digitalWrite(led1, LOW);   // turn LED LOW
      digitalWrite(led2, LOW);   // turn LED LOW
      noTone(buzzer);       
      if ((IDLEcurrentMillis  - IDLEpreviousMillis ) >= IDLEinterval)
      {
        IDLEpreviousMillis = IDLEcurrentMillis;
        IDLEState = (IDLEState + 1) %2;
      }
      display_idle_message(IDLEState);
    }
  } 
Download Project Files