Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Treppenlicht
- #include "Tlc5940.h"
- #include "NewPing.h"
- /**************************************************************************/
- //Einstellungen ab hier vornehmen
- #define MAX 11 //Anzahl der Stufen [0-15]
- #define MAX_DISTANCE 70 // Maximaldistanz in cm, die ausgelesen werden soll; max. 400
- #define ULTRASCHALL_1 4 // Pin vom 1. Ultraschall Sensor
- #define ULTRASCHALL_2 5 // Pin vom 2. Ultraschall Sensor
- uint16_t brightness = 2000; //Initialhelligkeit [0-4095]
- uint32_t timeout1 = 10000; //Gesamttimeout in Millisekunden
- //Einstellunge bis hier vornehmen
- /**************************************************************************/
- //Globale Initialisierungen
- bool sensor[2] = {1,1}; //Array fuer Sensorwerten
- int8_t direction = 0; //1: vorwaerts, -1: rueckwarts
- int8_t tempdirection = 0;
- unsigned long timeStart = 0; //Zaehlvariablen fuer Timeout
- int8_t channel=0; //Laufvariable fuer Kanaele
- int schritt = 0;
- //Deklarationen für Ultraschall
- NewPing sonar[2] =
- {
- NewPing(ULTRASCHALL_1, ULTRASCHALL_1, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
- NewPing(ULTRASCHALL_2, ULTRASCHALL_2, MAX_DISTANCE)
- };
- void setup ()
- {
- Tlc.init();
- }
- void loop ()
- {
- readSensor();
- if(direction==1 || direction==1)
- {
- //timeStart2 = 0;
- tempdirection = direction;
- }
- switch (schritt)
- {
- //Leerlauf
- case 0: if(direction != 0)
- {
- schritt = 2;
- }
- break;
- //Einfaden
- case 2: fadein();
- timeStart = millis();
- schritt = 3;
- break;
- //Licht an, wartet auf Timeout
- case 3: if((millis()-timeStart)>timeout1)
- schritt = 4;
- break;
- //Ausfaden
- case 4: fadeout(-1*tempdirection);
- schritt = 0;
- break;
- default: break;
- }
- delay(2);
- }
- //Einlesen der Sensorwerte
- void readSensor () {
- unsigned int us1 = sonar[0].ping()/US_ROUNDTRIP_CM;
- delay(35);
- unsigned int us2 = sonar[1].ping()/US_ROUNDTRIP_CM;
- delay(35);
- if((us1>10) && (us1<MAX_DISTANCE))
- sensor[0] = 1;
- else sensor[0] = 0;
- if((us2>10) && (us2<MAX_DISTANCE))
- sensor[1] = 1;
- else sensor[1] = 0;
- //Laufrichtungen werden bestimmt
- if((sensor[0]==1)&&(sensor[1]==0))
- direction = 1;
- else if((sensor[0]==0)&&(sensor[1]==1))
- direction = -1;
- else direction = 0;
- }
- //Funktion fuer das Einfaden
- int fadein () {
- //Lokale Hilfsvariablen
- int destination = 0;
- //Hilfsgroessen zur Einhaltung der Laufrichtungen
- if (direction==1) {
- channel=0;
- destination = MAX;
- }
- else if (direction==-1) {
- channel = MAX;
- destination = 0;
- }
- //Alle angeschlossene Kanaele werden durchgeschaltet
- do{
- Tlc.set(channel, brightness);
- Tlc.update();
- delay(250);
- channel += direction;
- } while (channel!=destination);
- return 0;
- }
- //Funktion fuer das Ausfaden, Funktion analog zu fadeon()
- //Rueckgabewert: 0, wenn vollstaendig ausgefuehrt,
- int fadeout (int8_t direction_out) {
- int stop = 0;
- int destination = 0;
- //Hilfsgroessen zur Einhaltung der Laufrichtungen
- if (direction_out==1) {
- channel=0;
- destination = MAX;
- }
- else if (direction_out==-1) {
- channel = MAX;
- destination = 0;
- }
- do{
- Tlc.set(channel, 0);
- Tlc.update();
- delay(250);
- channel += direction;
- } while (channel!=destination);
- Tlc.clear();
- Tlc.update();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement