Advertisement
Guest User

Untitled

a guest
Jun 30th, 2014
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. //Treppenlicht
  2. #include "Tlc5940.h"
  3. #include "NewPing.h"
  4.  
  5. /**************************************************************************/
  6. //Einstellungen ab hier vornehmen
  7. #define MAX 11 //Anzahl der Stufen [0-15]
  8. #define MAX_DISTANCE 70 // Maximaldistanz in cm, die ausgelesen werden soll; max. 400
  9. #define ULTRASCHALL_1 4 // Pin vom 1. Ultraschall Sensor
  10. #define ULTRASCHALL_2 5 // Pin vom 2. Ultraschall Sensor
  11.  
  12.  
  13. uint16_t brightness = 2000; //Initialhelligkeit [0-4095]
  14. uint32_t timeout1 = 10000; //Gesamttimeout in Millisekunden
  15.  
  16. //Einstellunge bis hier vornehmen
  17. /**************************************************************************/
  18.  
  19. //Globale Initialisierungen
  20. bool sensor[2] = {1,1}; //Array fuer Sensorwerten
  21. int8_t direction = 0; //1: vorwaerts, -1: rueckwarts
  22. int8_t tempdirection = 0;
  23. unsigned long timeStart = 0; //Zaehlvariablen fuer Timeout
  24. int8_t channel=0; //Laufvariable fuer Kanaele
  25. int schritt = 0;
  26.  
  27.  
  28. //Deklarationen für Ultraschall
  29. NewPing sonar[2] =
  30. {
  31. NewPing(ULTRASCHALL_1, ULTRASCHALL_1, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  32. NewPing(ULTRASCHALL_2, ULTRASCHALL_2, MAX_DISTANCE)
  33. };
  34.  
  35.  
  36. void setup ()
  37. {
  38. Tlc.init();
  39. }
  40.  
  41. void loop ()
  42. {
  43.  
  44. readSensor();
  45. if(direction==1 || direction==1)
  46. {
  47. //timeStart2 = 0;
  48. tempdirection = direction;
  49. }
  50.  
  51. switch (schritt)
  52. {
  53. //Leerlauf
  54. case 0: if(direction != 0)
  55. {
  56. schritt = 2;
  57. }
  58. break;
  59.  
  60. //Einfaden
  61. case 2: fadein();
  62. timeStart = millis();
  63. schritt = 3;
  64. break;
  65.  
  66. //Licht an, wartet auf Timeout
  67. case 3: if((millis()-timeStart)>timeout1)
  68. schritt = 4;
  69. break;
  70.  
  71. //Ausfaden
  72. case 4: fadeout(-1*tempdirection);
  73. schritt = 0;
  74. break;
  75.  
  76. default: break;
  77.  
  78. }
  79.  
  80. delay(2);
  81. }
  82.  
  83. //Einlesen der Sensorwerte
  84. void readSensor () {
  85.  
  86. unsigned int us1 = sonar[0].ping()/US_ROUNDTRIP_CM;
  87. delay(35);
  88. unsigned int us2 = sonar[1].ping()/US_ROUNDTRIP_CM;
  89. delay(35);
  90.  
  91. if((us1>10) && (us1<MAX_DISTANCE))
  92. sensor[0] = 1;
  93. else sensor[0] = 0;
  94.  
  95. if((us2>10) && (us2<MAX_DISTANCE))
  96. sensor[1] = 1;
  97. else sensor[1] = 0;
  98.  
  99. //Laufrichtungen werden bestimmt
  100. if((sensor[0]==1)&&(sensor[1]==0))
  101. direction = 1;
  102. else if((sensor[0]==0)&&(sensor[1]==1))
  103. direction = -1;
  104. else direction = 0;
  105. }
  106.  
  107. //Funktion fuer das Einfaden
  108. int fadein () {
  109.  
  110. //Lokale Hilfsvariablen
  111. int destination = 0;
  112.  
  113. //Hilfsgroessen zur Einhaltung der Laufrichtungen
  114. if (direction==1) {
  115. channel=0;
  116. destination = MAX;
  117. }
  118. else if (direction==-1) {
  119. channel = MAX;
  120. destination = 0;
  121. }
  122.  
  123. //Alle angeschlossene Kanaele werden durchgeschaltet
  124. do{
  125.  
  126. Tlc.set(channel, brightness);
  127. Tlc.update();
  128.  
  129. delay(250);
  130.  
  131. channel += direction;
  132.  
  133. } while (channel!=destination);
  134.  
  135. return 0;
  136. }
  137.  
  138. //Funktion fuer das Ausfaden, Funktion analog zu fadeon()
  139. //Rueckgabewert: 0, wenn vollstaendig ausgefuehrt,
  140. int fadeout (int8_t direction_out) {
  141.  
  142. int stop = 0;
  143. int destination = 0;
  144.  
  145. //Hilfsgroessen zur Einhaltung der Laufrichtungen
  146. if (direction_out==1) {
  147. channel=0;
  148. destination = MAX;
  149. }
  150. else if (direction_out==-1) {
  151. channel = MAX;
  152. destination = 0;
  153. }
  154.  
  155. do{
  156.  
  157. Tlc.set(channel, 0);
  158. Tlc.update();
  159.  
  160. delay(250);
  161.  
  162. channel += direction;
  163.  
  164. } while (channel!=destination);
  165.  
  166. Tlc.clear();
  167. Tlc.update();
  168.  
  169. return 0;
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement