4. November 2010
Wie lässt sich mit LEDs ja/nein sagen? Diese Frage wollten wir mit neun LEDs beantworten. Da wir diese Woche schon mit dem Logik Chip 74138 gearbeitet haben, dachten wir, dass wir den doch einfach wieder verwenden können. Nach dem Aufbau kam dann jedoch die Entäuschung, da unsere Schaltung nicht funktionierte. Das Problem war dann auch schnell gefunden - es musste ein anderer Logik Chip her (74595). Nach dem austauschen des Chips funktionierte unsere Schaltung dann auch und wir konnten endlich an unser "Ja" und "Nein" gehen. Variante 1 Source Code #define LED 7 int latchPin = 8; int clockPin = 12; int dataPin = 11; byte nachricht = 0; byte dataRED; byte dataGREEN; byte dataArrayRED[10]; void setup() { Serial.begin(9600); pinMode(latchPin, OUTPUT); pinMode(LED, OUTPUT); digitalWrite(LED, HIGH); dataArrayRED[0] = 0xFF; } void loop() { for (int j = 0; j < 10; j++){ //load the light sequence you want from array dataRED = dataArrayRED[j]; //ground latchPin and hold low for as long as you are transmitting digitalWrite(latchPin, 0); //move 'em out shiftOut(dataPin, clockPin, dataGREEN); shiftOut(dataPin, clockPin, dataRED); //return the latch pin high to signal chip that it //no longer needs to listen for information digitalWrite(latchPin, 1); delay(300); } } void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { // This shifts 8 bits out MSB first, //on the rising edge of the clock, //clock idles low //internal function setup int i=0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); //clear everything out just in case to //prepare shift register for bit shifting digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); //for each bit in the byte myDataOut� //NOTICE THAT WE ARE COUNTING DOWN in our for loop //This means that %00000001 or "1" will go through such //that it will be pin Q0 that lights. for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); //if the value passed to myDataOut and a bitmask result // true then... so if we are at i=6 and our value is // %11010100 it would the code compares it to %01000000 // and proceeds to set pinState to 1. if ( myDataOut & (1<Variante 2 Um den Source Code besser zu verstehen, probierten wir verschiedene Anzeigen mit den LEDs aus. Wellen-Beispiel Weiter griffen wir nochmals das Thema "Ja" / "Nein" auf, verwendeten aber die Worte "OK" bzw. "NO". OK NO #define LED 7 int latchPin = 8; int clockPin = 12; int dataPin = 11; byte nachricht = 0; int j = 0; void setup() { Serial.begin(9600); pinMode(latchPin, OUTPUT); pinMode(LED, OUTPUT); digitalWrite(LED, HIGH); } void loop(){ j = 0; shiftOut(dataPin, clockPin, 0); if(Serial.available() > 0){ nachricht = Serial.read(); } handleInput(); } void handleInput(){ digitalWrite(latchPin, LOW); if(nachricht == 'y'){ sayYes(); } else if(nachricht == 'n'){ sayNo(); } else if(nachricht == 'c'){ countUp(); } else if(nachricht == 'w'){ welle(); } else if(nachricht == 'o'){ allesOK(); } else if(nachricht == 'x'){ nichtsOK(); } digitalWrite(latchPin, HIGH); delay(500); } void allesOK(){ int allesOKArray[] = {255, 173}; if(Serial.available() > 0){ nachricht = Serial.read(); } digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, allesOKArray[0]); digitalWrite(LED, LOW); digitalWrite(latchPin, HIGH); delay(500); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); digitalWrite(LED, LOW); delay(200); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, allesOKArray[1]); digitalWrite(LED, HIGH); digitalWrite(latchPin, HIGH); delay(500); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); delay(200); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); digitalWrite(LED, LOW); delay(1000); } void nichtsOK(){ int nichtsOKArray[] = {189, 255}; if(Serial.available() > 0){ nachricht = Serial.read(); } digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, nichtsOKArray[0]); digitalWrite(LED, HIGH); digitalWrite(latchPin, HIGH); delay(500); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); digitalWrite(LED, LOW); delay(200); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, nichtsOKArray[1]); digitalWrite(LED, LOW); digitalWrite(latchPin, HIGH); delay(500); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); delay(200); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, HIGH); digitalWrite(LED, LOW); delay(1000); } void countUp(){ for (j = 0; j < 256 && nachricht == 'c'; j++){ if(Serial.available() > 0){ nachricht = Serial.read(); } digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, j); digitalWrite(latchPin, HIGH); delay(100); } } void welle(){ int welleArray[] = {41, 66, 148, 0}; digitalWrite(LED, HIGH); for (j = 0; j < 4 && nachricht == 'w'; j++){ if(Serial.available() > 0){ nachricht = Serial.read(); } digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, welleArray[j]); digitalWrite(latchPin, HIGH); delay(500); } } void sayYes(){ j = 90; digitalWrite(LED, HIGH); shiftOut(dataPin, clockPin, j); } void sayNo(){ j = 165; digitalWrite(LED, HIGH); shiftOut(dataPin, clockPin, j); } void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { int i = 0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); if ( myDataOut & (1<