Lec3 - Digital Inputs, Outputs, PWM
Lec3 - Digital Inputs, Outputs, PWM
CSE 3105
Computer Interfacing & Embedded System
void loop()
{
void loop()
{
[Link]
Turning On LED
Turning On LED
const int LED=PC13; // Define LED for pin PC13
Turning On LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
Turning On LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
digitalWrite(LED, HIGH);
// Set the LED pin high
}
Turning On LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
digitalWrite(LED, HIGH);
// Set the LED pin high
}
void loop()
{
// We are not doing anything in the loop!
}
Blinking LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
Blinking LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
void loop()
{
digitalWrite(LED, HIGH);
}
Blinking LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000); // Time in milliseconds
}
Blinking LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000); // Time in milliseconds
digitalWrite(LED, LOW);
}
Blinking LED
const int LED=PC13; // Define LED for pin PC13
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000); // Time in milliseconds
digitalWrite(LED, LOW);
delay(1000);
}
Using For Loops
It’s frequently necessary to use loops with changing
variable values to adjust the parameters of a program.
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
LED with changing blink rate
const int LED=PA15; // Define LED for pin PA15
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
void loop()
{
for (int i=100; i<=1000; i=i+100)
{
}
}
LED with changing blink rate
const int LED=PA15; // Define LED for pin PA15
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
}
void loop()
{
for (int i=100; i<=1000; i=i+100)
{
digitalWrite(LED, HIGH);
delay(i);
digitalWrite(LED, LOW);
delay(i);
}
}
Pulse-Width Modulation
What if you want to output a voltage other than 0V or
5V? Well, you can’t—unless you are using a digital to-
analog converter (DAC) integrated circuit.
analogWrite(PIN, 0) analogWrite(PIN,
255)
LED fade
void setup()
{
pinMode (PB0, OUTPUT);
//Set the LED pin as an output
}
LED fade
void setup()
{
pinMode (PB0, OUTPUT);
//Set the LED pin as an output
}
void loop()
{
for (int i=0; i<256; i++)
{
}
}
LED fade
void setup()
{
pinMode (PB0, OUTPUT);
//Set the LED pin as an output
}
void loop()
{
for (int i=0; i<256; i++)
{
analogWrite(PB0, i);
delay(10);
}
}
LED fade
void setup()
{
pinMode (PB0, OUTPUT);
//Set the LED pin as an output
}
void loop()
{
for (int i=0; i<256; i++)
{
analogWrite(PB0, i);
delay(10);
}
for (int i=255; i>=0; i--)
{
}
}
LED fade
void setup()
{
pinMode (PB0, OUTPUT);
//Set the LED pin as an output
}
void loop()
{
for (int i=0; i<256; i++)
{
analogWrite(PB0, i);
delay(10);
}
for (int i=255; i>=0; i--)
{
analogWrite(PB0, i);
delay(10);
}
}
Reading Digital Inputs
Floating
Imagine the circuit in figure without the 10kΩ resistor.
What happens when the button is not being pressed?
void setup()
{
pinMode (LED, OUTPUT);
// Set the LED pin as an output
pinMode (BUTTON, INPUT);
// Set button as input (not required)
}
Simple LED control with a button
void loop()
{
if (digitalRead(BUTTON) == LOW)
{
digitalWrite(LED, LOW);
}
else
{
digitalWrite(LED, HIGH);
}
}
Simple LED control with a button
void loop()
{
if (digitalRead(BUTTON) == LOW)
{
digitalWrite(LED, LOW);
}
else
{
digitalWrite(LED, HIGH);
}
}
When was the last time you had to hold a button down to keep a light
on? Probably never. It makes more sense to be able to click the button
once to turn it on and to click the button again to turn it off.
Unfortunately, this is not quite as easy as you might first guess. You
cannot just look for the value of the switch to change from low to
high; you need to worry about a phenomenon called switch bouncing.
Switch Bouncing
void setup()
{
pinMode (LED, OUTPUT); // Set the LED pin as an output
pinMode (BUTTON, INPUT); // Set button as input (not required)
}
Debounce Button Toggling
/*
* Debouncing Function
* Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); // Read the button state
if (last != current) // if it's different...
{
}
}
Debounce Button Toggling
/*
* Debouncing Function
* Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); // Read the button state
if (last != current) // if it's different...
{
delay(5); //Wait 5ms
current = digitalRead(BUTTON); //Read it again
}
return current; //Return the current value
}
Debounce Button Toggling
void loop()
{
currentButton = debounce(lastButton); //Read debounced state
}
Debounce Button Toggling
void loop()
{
currentButton = debounce(lastButton); //Read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
}
}
Debounce Button Toggling
void loop()
{
currentButton = debounce(lastButton); //Read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledOn = !ledOn; //Toggle the LED value
}
}
Debounce Button Toggling
void loop()
{
currentButton = debounce(lastButton); //Read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledOn = !ledOn; //Toggle the LED value
}
lastButton = currentButton; //Reset button value
digitalWrite(LED, ledOn); //Change the LED state
}
Controllable RGB LED Nightlight