Wednesday, January 23, 2013

Microcontrollers, input and output

Input tranducers are electronic devices that detect changes in the real world and send signals into the process block of the electronic system: push switches, LDR, microphone, tilt switch, infared

First two videos were corrupted and could not be uploaded...

Demonstrate flashing LED with variable delay


#define LED 6

void setup()
{
  pinMode(LED,OUTPUT);              //Initialize Digital Pin 6 as output
    for(int counter = 20; counter > 0; counter = counter - 1)    //Definte the for loop
    {
      digitalWrite(LED,HIGH); //Set the LED ON
      delay(50 * counter);
      digitalWrite(LED,LOW); //Set the LED OFF
      delay(50 * counter);
    }
}
 
void loop()
{
  digitalWrite(LED,HIGH);            //Set the LED On
  delayMicroseconds(16000);          //Wait for 16 ms (1 second)
  digitalWrite(LED,LOW);             //Set the LED Off
  delayMicroseconds(16000);          //Wait for .016 seconds
}

Demonstrate pushbutton LED


#define buttonPin 5  //the number of the pushbutton pin
#define ledPin 6  //the number of the LED pin
int buttonState = 0;  //variable for reading the pushbutton status

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);  //initialize the pushbutton pin as an input
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
}

void loop()
{
  buttonState = digitalRead(buttonPin);  //read the state of the pushbutton value;
 
  if (buttonState == HIGH) //check if the pushbutton is pressed
  {
    digitalWrite(ledPin, HIGH); //turn the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW);  //turn the LED off
  }
}



Demonstrate LDR controlling LEDs


#define sensorPin A0  //select the input pin for the LDR
#define ledPin6 6  //select the pin for the LED
#define ledPin7 7
int sensorValue = 0;  //variable to store the value coming from the sensor

void setup()
{
  pinMode(ledPin6, OUTPUT);  //declare the ledPin6 as an OUTPUT
  pinMode(ledPin7, OUTPUT);  //declare the ledPin7 as an OUTPUT
  Serial.begin(9600);
}

void loop()
{
  sensorValue = analogRead(sensorPin);  //read the value from the sensor
  Serial.println(sensorValue);
    if (sensorValue >= 565)
    {
      digitalWrite(ledPin6, HIGH);  //turn the ledPin on
      digitalWrite(ledPin7, HIGH);  //turn the ledPin on
    }
   
    else if (sensorValue < 565 && sensorValue >= 530)
    {
      digitalWrite(ledPin6, HIGH);  //turn the ledPin on
      digitalWrite(ledPin7, LOW);  //turn the ledPin on
    }
   
    else if (sensorValue < 530 && sensorValue >= 500)
    {
      digitalWrite(ledPin6, LOW);  //turn the ledPin on
      digitalWrite(ledPin7, HIGH);  //turn the ledPin on
    }
    else
    {
      digitalWrite(ledPin6, LOW);  //turn the ledPin on
      digitalWrite(ledPin7, LOW);  //turn the ledPin on
    }
}



No comments:

Post a Comment