IFD:PhysicalComp2011/code: Difference between revisions

From Medien Wiki
(Updated material and sketches from 21.11.)
Line 574: Line 574:


=== I Blinking LED ===
=== I Blinking LED ===
 
[[File:LED_schematics.png|200px|thumb]]
[[File:I_blinkingLED_fritzing_bb.png|200px|thumb]]
<source lang="java">
<source lang="java">
/* BLINKING LED
/* BLINKING LED
Line 609: Line 610:
   delay(1000);
   delay(1000);


}
</source>
<br /><br />
=== II Pushbutton ===
[[File:Pushbutton_schematic.png|200px|thumb]]
[[File:II_pushbutton_arduino.png|200px|thumb]]
<source lang="java">
/* PUSHBUTTON
*
* Here we connect a pushbutton to the digital pin 2.
* When the button is pressed, the LED on port 13
* (the internal one), starts blinking.
*/
int myLedPin = 13;      // LED pin
int myButtonPin = 2;    // input pin (for the pushbutton)
int myButtonState = 0;  // variable for storing the inputpin status
void setup() {
  // declare the LED pin as output
  pinMode(myLedPin, OUTPUT);
  // declare the button pin as input
  pinMode(myButtonPin, INPUT);
 
}
void loop(){
  // read the current state of the button pin
  // (either HIGH or LOW) and store it in the
  // variable myButtonState:
  myButtonState = digitalRead(myButtonPin);
 
  // whenever the button is pressed:
  // start the LED blinking
  if (myButtonState == HIGH) {
      digitalWrite(myLedPin, HIGH);
      delay(300);
      digitalWrite(myLedPin, LOW);
      delay(300);
  }
  // when the button is not pressed:
  else{
    // turn off the LED
    digitalWrite(myLedPin, LOW);
  }
}
}
</source>
</source>