25. September 2012
Aufbau Funktionen Lampe (Externer Verbraucher) = Wird mit hilfe der Taster und der Anzeige reguliert +Taster/ -Taster = Tippen: Lichtstärke und Anzeige steigert/verringert sich um 1 Gedrückt halten: Lichtstärke und Anzeige steigert/verringert sich Stufenweise Anzeige = Füllt/entlehrt sich je nach Taster-Input const int LATCH_PIN = 8; //Pin zu ST_CP vom 74HC595 const int CLOCK_PIN = 12; //Pin zu SH_CP vom 74HC595 const int DATA_PIN = 11; //Pin zu DS vom 74HC595 const int led9 = 10; const int led8 = 9; const int buttonUp = 7; const int buttonDown = 6; const int lampenBirne = 3; int buttonUpState = 0; int buttonDownState = 0; boolean loslassenUp = false; boolean loslassenDown = false; int i = 0; long pushTime = 0; // pushTime = Aktuelle millis(); Zeit int delayTime = 1000; long letztesMal = 0; int warteZeit = 250; void setup() { Serial.begin(9600); pinMode(LATCH_PIN, OUTPUT); pinMode(CLOCK_PIN, OUTPUT); pinMode(DATA_PIN, OUTPUT); pinMode(led9, OUTPUT); pinMode(led8, OUTPUT); pinMode(buttonUp, INPUT); pinMode(buttonDown, INPUT); pinMode(lampenBirne, OUTPUT); digitalWrite(led9, LOW); digitalWrite(led8, LOW); } //------------------------------------------------------------------------------------------------ void loop() { plusButton(); minuButton(); registerWrite(i, HIGH); // Entsprechende LED anmachen lampe(); } //------------------------------------------------------------------------------------------------ void registerWrite(int i, int _whichState) { byte bitsToSend = 0; // Dieses Byte hat acht Bits also: 00000000 digitalWrite(LATCH_PIN, LOW); switch(i) { case 0: bitWrite(bitsToSend, 0, _whichState); break; case 1: bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 2: bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 3: bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 4: bitWrite(bitsToSend, 4, _whichState); bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 5: bitWrite(bitsToSend, 5, _whichState); bitWrite(bitsToSend, 4, _whichState); bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 6: bitWrite(bitsToSend, 6, _whichState); bitWrite(bitsToSend, 5, _whichState); bitWrite(bitsToSend, 4, _whichState); bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 7: bitWrite(bitsToSend, 7, _whichState); bitWrite(bitsToSend, 6, _whichState); bitWrite(bitsToSend, 5, _whichState); bitWrite(bitsToSend, 4, _whichState); bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 8: bitWrite(bitsToSend, 8, _whichState); bitWrite(bitsToSend, 7, _whichState); bitWrite(bitsToSend, 6, _whichState); bitWrite(bitsToSend, 5, _whichState); bitWrite(bitsToSend, 4, _whichState); bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; case 9: bitWrite(bitsToSend, 9, _whichState); bitWrite(bitsToSend, 8, _whichState); bitWrite(bitsToSend, 7, _whichState); bitWrite(bitsToSend, 6, _whichState); bitWrite(bitsToSend, 5, _whichState); bitWrite(bitsToSend, 4, _whichState); bitWrite(bitsToSend, 3, _whichState); bitWrite(bitsToSend, 2, _whichState); bitWrite(bitsToSend, 1, _whichState); bitWrite(bitsToSend, 0, _whichState); break; default: break; } // basisLicht(bitsToSend, _whichState); shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitsToSend); digitalWrite(LATCH_PIN, HIGH); if (i == 7){ digitalWrite(led8, LOW); } if (i == 8){ digitalWrite(led8, HIGH); digitalWrite(led9, LOW); } if (i == 9){ //digitalWrite(led8, LOW); digitalWrite(led9, HIGH); } } void plusButton() { buttonUpState = digitalRead(buttonUp); if (buttonUpState == LOW){ loslassenUp = true; } if (buttonUpState == HIGH && i < 9){ if (loslassenUp == true){ i = i+1; pushTime = millis(); loslassenUp = false; } else{ if (millis()-pushTime > delayTime){ if (millis()-letztesMal > warteZeit){ letztesMal = millis(); i = i+1; } } } Serial.println("i = i + 1"); } } void minuButton() { buttonDownState = digitalRead(buttonDown); if (buttonDownState == LOW){ loslassenDown = true; } if (buttonDownState == HIGH && i > 0){ if (loslassenDown == true){ i = i-1; pushTime = millis(); loslassenDown = false; } else{ if (millis()-pushTime > delayTime){ if (millis()-letztesMal > warteZeit){ letztesMal = millis(); i =i-1; } } } Serial.println("i = i + 1"); } } void lampe() { float lampLichtMax = 255; float j = lampLichtMax/9 * i; analogWrite(lampenBirne, j); // Den Wert i an LED_PIN setzen Serial.print("j = "); Serial.println(j); }