Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Switchy switches

29. Oktober 2010

Es werden verschiedene Möglichkeiten gezeigt, den Schaltkreis zu schliessen. Wenn dies geschieht leuchten drei rote LEDs nacheinander kurz auf. Wir der Schaltkreis dreimal nacheinander geschlossen, leuchtet die grüne LED dreimal kurz auf. Die Blinkrate der roten LEDs kann über einen Potentiometer gesteuert werden. YouTube Preview Image
/*
/
/ jabba jabba hey, let's get the stuff runnin'.
/
*/

// declare pin variables
int led1Pin = 13;
int led2Pin = 12;
int led3Pin = 11;
int led4Pin = 10;
int buttonPin = 2;
int potPin = A5;

// declare misc variables
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int potValue = 0;

// the setup function
void setup() {
  
  // open serial communications
  Serial.begin(9600);
  
  // init pins
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(potPin, INPUT);
  
  // set internal pull up resistor
  digitalWrite(buttonPin, HIGH);
  
}

// the main loop
void loop() {
  
    // read potentiometer value
    potValue = analogRead(potPin);
    
    // read button state
    buttonState = digitalRead(buttonPin);
    
    // debounce...
    if (buttonState != lastButtonState) {
  
      if (buttonState == HIGH) {
  
        // increase push counter
        buttonPushCounter++;
        
        // set all leds off
        resetLeds();
        
        if (buttonPushCounter == 3) {
          
          // set green led on
          goGreen();
          
          // reset push counter
          buttonPushCounter = 0;
          
        }
  
      }
      
      else {
        
        // set green led off
        digitalWrite(led4Pin, LOW);
        
        // run red led theater
        runLeds();
        
      }
      
      // save last button state
      lastButtonState = buttonState;
      
    }
  
}

// set all leds off
void resetLeds() {
  
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, LOW);
  digitalWrite(led4Pin, LOW);
        
}

// make red leds run in glorious fancyness
void runLeds() {
  
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, LOW);
  delay(potValue / 5);
  
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, LOW);
  delay(potValue / 5);
  
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);
  delay(potValue / 5);
        
}

// yay, it's green
void goGreen() {
   
  for (int i = 0; i <= 3; i++) {
    analogWrite(led4Pin, 50);
    delay(35);
    analogWrite(led4Pin, 0);
    delay(35);
  }
  
}