/*
* Lokschuppen mit Servo und Blinkdioden
* 2020-03-16
* Wolfgang aka Running_Wolf
*/
#include <MobaTools.h>
const int rotPin1 = 12; // rotes Blinklicht über Tor1 sw/we
const int blitzPin1 = 11; // Blitzlicht LED (lila)
const int torservoPin = 10; // Anschluß für den Servo Links/Rechts
const int lichtPin3 = 9; // LED Strip (gelb)
const int lichtPin2 = 8; // Hallenlicht Nebenhalle LED Strip (grün)
const int torrelaisPin = 7; // via Darlington (noch offen)
const int lichtPin1 = 2; // Hallenlicht Haupthalle LED Strip (grün)
unsigned long previousMillis_Tor1 = 0; // will store last time of change
unsigned long previousMillis_Rot1 = 0; // will store last time of change
unsigned long previousMillis_Halle1 = 0; // will store last time of change
unsigned long previousMillis_Halle2 = 0; // will store last time of change
unsigned long wann_Halle1[2] = { 30000,40000} ; // Startabstand, Laufzeit Hallenlicht 1
unsigned long wann_Halle2[2] = {310000,51000} ; // Startabstand, Laufzeit Hallenlicht 2
unsigned long wann_Tor1[2] = {300000,300000} ; // Startabstand, Laufzeit Hallentor 1
//unsigned long blitzStartenB1 = 0 ;
unsigned long previousMillisB1 = 0; // will store last time of change
const long blitzDauerB1 = 20000 ;
long blitzOffB1 = 2000;
//const int blitzZeitB1 = 20 ; // Länge Blitzlicht
//unsigned long wann_Blitz = 500 ; // Blitzfrequenz
int blitzStateB1 = LOW;
//byte blitzFlicker[] = {180, 30, 89, 23, 255, 200, 90, 150, 60, 230, 180, 45, 90};
unsigned long wann_Rot1 = 200 ; // rotes Blinklicht über Tor
int status_Halle1 = 0 ; // Licht Halle 1 aus
int status_Halle2 = 0 ; // Licht Halle 2 aus
int status_Tor1 = 0 ; // Tor 1 zu
int ziel_Tor1 = 0 ; // 0 = zu, 170 = auf
//
Servo8 torServo;
// EggTimer Pause; // Timer für Pausen zwischen den Servobewegungen
// byte schritt = 0; // Zustand Schrittkette
//
// Tastenfunktionen
int Taste_1 = A0; // Steuertaste 1
int Taste_2 = A1; // Steuertaste 2
int Taste_3 = A2; // Drucktaste
int Taste_1_Wert = 0; // Variable für Tastenabfrage
int Taste_2_Wert = 0; // Variable für Tastenabfrage
int Druck_3_Wert = 0; // Drucktaste
int entprellzeit = 1000 ;
int Tortaste1 = 0 ; // Tastenwert für Tor 1, 0 = nichts, 1 = zu, 2 = auf
//
//
void setup() {
Serial.begin(9600);
torServo.attach(torservoPin, true); //Servo an Pin 5
torServo.setSpeed( 3 ); // Verfahrgeschwindigkeit einstellen Links/Rechts
// pinMode(Taste_1, INPUT_PULLUP) ;
// pinMode(Taste_2, INPUT_PULLUP) ;
pinMode(Taste_3, INPUT_PULLUP) ;
pinMode(lichtPin1, OUTPUT);
pinMode(lichtPin2, OUTPUT);
pinMode( rotPin1, OUTPUT);
pinMode(blitzPin1, OUTPUT);
pinMode(torrelaisPin, OUTPUT) ;
digitalWrite(rotPin1, HIGH); // Licht aus
digitalWrite(lichtPin1, HIGH) ;
digitalWrite(lichtPin2, HIGH) ;
digitalWrite(blitzPin1, HIGH) ;
digitalWrite(torrelaisPin, HIGH) ; // Relais aus
}
//
void loop() {
unsigned long currentMillis = millis();
//
// Torstatus abfragen
// Serial.print ( "Torstatus: ");
// Serial.println(torServo.read()) ;
if (torServo.read()> 160) {
status_Tor1 = 0;
}
if (torServo.read()< 10) {
status_Tor1 = 1;
}
// Hallenlicht Haupthalle 1
if (currentMillis - previousMillis_Halle1 >= wann_Halle1[status_Halle1]) {
previousMillis_Halle1 = currentMillis; // save the last change
digitalWrite(lichtPin1, !digitalRead(lichtPin1));
status_Halle1 = 1-status_Halle1;
// Ende Licht Haupthalle
}
//
// Tor Halle 1 automatisch betätigen
//
if (currentMillis - previousMillis_Tor1 >= wann_Tor1[status_Tor1]) {
Serial.print("Automatikt: ") ;
Serial.println(wann_Tor1[1-status_Tor1]);
previousMillis_Tor1 = currentMillis; // save the last change
torServo.write(170 * status_Tor1); // 0 macht zu, 170 macht auf
}
// Ende Tor Haupthalle
//
if (torServo.moving()) {
digitalWrite(lichtPin1, HIGH);
previousMillis_Halle1 = currentMillis;}
//
// Hallenlicht Halle 2
if (currentMillis - previousMillis_Halle2 >= wann_Halle2[status_Halle2]) {
previousMillis_Halle2 = currentMillis; // save the last change
digitalWrite(lichtPin2, !digitalRead(lichtPin2));
status_Halle2 = 1-status_Halle2;
// Ende IF Licht Halle 2
}
//
Tortaste1 = 0 ;
Tortaste1 = Wert_Taste(Taste_1) ; // fragt Wert der Taste ab
//
if (Druck_Taste(Taste_3)==1) {
torServo.write(170 * status_Tor1); // 0 macht zu, 170 macht auf
}
// Servo Loop Anfang ----------------------------------------
//
if (digitalRead(torrelaisPin)) {
// noch nicht genutzt
} // torrelaisPin
//
//
// Hier können jetzt weitere Aktionen ausgeführt werden.
//
// Loop Blink LED Tor 1
//
if (currentMillis - previousMillis_Rot1 >= wann_Rot1) {
previousMillis_Rot1 = currentMillis; // save the last change
if (torServo.moving()) {
digitalWrite(rotPin1, !digitalRead(rotPin1)); }
else {
digitalWrite(rotPin1, LOW); }
}
//
// Loop Blink Ende
// Bltzer an wenn Tor nicht bewegt
//
currentMillis = millis();
// ----------------Beginn jetztTBlitzen--------------------------------------
// Anschalten
if ((currentMillis - previousMillisB1 >= blitzOffB1) && !(torServo.moving())) {
//
blitzStateB1 = !blitzStateB1;
digitalWrite(blitzPin1, blitzStateB1);
previousMillisB1 = currentMillis;
blitzOffB1 = random(1000, 3000);
//
}
// // Ende jetztBlitzen --------------------------------------------
} // Loop Ende
// Funktionen
//
// Analoge TASTE abfragen
//
int Wert_Taste(int dieseTaste){ // Widerstandsnetzwerk erlaubt ...
Taste_1_Wert = analogRead(dieseTaste); // ... zwei Tasten an einem Eingang
delay (entprellzeit);
Taste_1_Wert = analogRead(dieseTaste);
// Serial.print("Tastenwert: ") ;
// Serial.println(Taste_1_Wert);
if (Taste_1_Wert > 410 && Taste_1_Wert < 640) {return 0; }
else if (Taste_1_Wert > 300 && Taste_1_Wert < 400) {return 1; }
else if (Taste_1_Wert > 650 && Taste_1_Wert < 850) {return 2; }
} /// Ende Wert_Taste ///
//
int Druck_Taste(int dieseTaste){
Druck_3_Wert = analogRead(dieseTaste);
delay (entprellzeit);
Druck_3_Wert = analogRead(dieseTaste);
// Serial.print("Tastenwert: ") ;
// Serial.println(Druck_3_Wert);
if (Druck_3_Wert < 100) {return 1; }
else if (Druck_3_Wert > 1000) {return 0; }
}
//