Lab 10 is mainly apply C++ code knowledge to take information form sensor to turn ON and OFF output devices, which is pretty fun once you know a few good function and what the variable represent in the physical world.
"HELLO WORLD!"
Demonstrate Flashing LED with Variable Delay
Example Code:
- "#define"- Sometimes it can be hard to remember which pins are connected to which devices. This command can be used at the start of the program to rename the inputs and outputs.
- A "function"- Is like the void setup() or void loop() in the program below. When the label is first defined as a "type". The type void means that it won't return any data.
- "delay()" and "delayMicroseconds()"- The delay() command delays for a time in milliseconds. delayMicroseconds() command delays for a time in microseconds for shorter intervals.
- for loops- Repeats a block of statements enclosed in brackets. The for loop is used for any repetitive operation and is used in combination with arrays to operate on a collection of data/pins.
#define LED 6 //Define the symbol LED to be pin 6
void setup()
{
pinMode(LED,OUTPUT); //Initialize Digital Pin 6 as an OUTPUT
}
void loop()
{
digitalWrite(LED,HIGH); //Set the LED On
delayMicroseconds(16000); //Wait for 116ms
digitalWrite(LED,LOW); //Set the LED OFF
delayMicroseconds(16000); //Wait for 16ms
}
For loop Example:
for(int = 0 ; x < 100 ; x++)
{
println(x); //prints 0 to 99
}
Summary:
Change your loop so that the delay varies from 1000 to 50 in increments to -50.
#define LED 6
void setup()
{
pinMode(LED,OUTPUT); //Initialize Digital Pin 6 as an OUTPUT
for(int counter = 1000;counter < 50; counter = counter-50)
{
digitalWrite(LED,HIGH); //Set the LED on
delay(200); //Wait for 0.2 second
digitalWrite(LED,LOW); //Set LED off
delay(200); //Wait for 0.2 second
}
}
void loop()
{
digitalWrite(LED,HIGH); //Set the LED on
delay(1000); //Wait for 1 second
digitalWrite(LED,LOW); //Set LED off
delay(1000); //Wait for 1 second
}
Demonstrate Pushbutton LED Using if/else Statement
Example Code:
- A digital sensor is a simple switch that can only be on or off.
- "Variable"- A variable as a bit of program memory that can store information, which we will use to store the status of the button
- "if()"- If is used in conjunction with comparison operator, test whether a certain condition has been reached, such as an input being above a certain number.
- Comparison Operators
- x == y (x is equal to y)
- x != y (x is not equal to y)
- x < y (x us less than y)
- x > y (x is greater than y)
- x <= y (x is less than or equal to y)
- x >= y (x is greater than or equal to y)
- "if()/else"- Allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together.
#define buttonPin 8 //the number of the pushbutton pim
#defineledPin 6 //the number of the LED pin
int buttonState = 0; //variable for reading the pushbutton status
void setup()
{
pinMode(ledPin,OUTPUT); //initialize the LED pin as an output
pinMode(buttonPin, INPUT); //initialize the pushbutton pin as an input
}
void loop()
{
buttonState = digitalRead(buttonPin); //read the state of the push button value
//Check if the button is pressed. If it is, the buttonState is HIGH
if(buttonState == HIGH)
{
digitalWrite(ledPin,HIGH); //turn LED on
}
if(buttonState == LOW)
{
digitalWrite(ledPin, LOW); //turn LED off
}
}
Summary:
Modify your code above to replace the two if statements with a single if/else block.
#define buttonPin 8 //the number of the pushbutton pim
#defineledPin 6 //the number of the LED pin
int buttonState = 0; //variable for reading the pushbutton status
void setup()
{
pinMode(ledPin,OUTPUT); //initialize the LED pin as an output
pinMode(buttonPin, INPUT); //initialize the pushbutton pin as an input
}
void loop()
{
buttonState = digitalRead(buttonPin); //read the state of the push button value
//Check if the button is pressed. If it is, the buttonState is HIGH
if(buttonState == HIGH)
{
digitalWrite(ledPin,HIGH); //turn LED on
}
else
{
digitalWrite(ledPin, LOW); //turn LED off
}
}
Demonstrate LDR Controlling LEDs
Example Code:
- An analog sensor measures a continues signal such as light, temperature, or position; and provides a varying voltage, which is represented by a number in range 0 to 255. This means 0 is the equivalent to dark and 255 is the equivalent to super bright.
- "readAnalog()"- The value of an analog input can be copied into a variable. The following code switch's the LED on, if the value is greater than 750 and off if it is less than 750.
#define sensorPin A0 //select the input pin for the potentiometer
#defineledPin 6 //the number of the LED pin
int sensorValue = 0; //variable to store the value coming from the sonsor
void setup()
{
pinMode(ledPin,OUTPUT); //initialize the LED pin as an output
}
void loop()
{
sensorValue = analogRead(sensorPin); //read the value of the sensor
if(sensorValue > 750)
{
digitalWrite(ledPin,HIGH); //turn ledPin on
delay(sensorValue); //stop the program for <sensorValue> milliseconds
}
else
{
digitalWrite(ledPin, LOW); //turn ledPin off
delay(sensorValue); //stop the program for <sensorValue> milliseconds
}
}
Summary:
Now attach a second LED to pin 7 of your Arduino using a 220 Ohm series resistor. Change your program so that it has 4 states:
- If the room is Bright both LEDs on
- If the room is Medium lit turn LED 1 on
- If the room is Dimly lit turn LED2 on
- If the room is Dark turn LEDs off
#define sensorPin A0
#define ledPin 6
#define ledPIn 7
int sensorValue = 0;
void setup()
{
pinMode(ledPin, OUTPUT); //LED 1
pinMode(ledPIn,OUTPUT); //LED 2
}
void loop()
{
sensorValue = analogRead(sensorPin);
if(sensorValue>900)
{
digitalWrite(ledPin,HIGH);
digitalWrite(ledPIn, HIGH);
}
if(sensorValue>850)
{
digitalWrite(ledPin,HIGH);
digitalWrite(ledPIn, LOW);
}
if(sensorValue>800)
{
digitalWrite(ledPin,LOW);
digitalWrite(ledPIn, HIGH);
}
else
{
digitalWrite(ledPin,LOW);
digitalWrite(ledPIn, LOW);
}
}
Demonstrate Thermistor Controlling Motors
Materials:
- 10K thermistor
- 10K resistor
- TIP 120 transistor
- BDC motor
Summary:
#define sensorPin A0
#define ledPin 6
int sensorValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //LED 1
}
void loop()
{
sensorValue = analogRead(sensorPin);
if(sensorValue>490)
{
digitalWrite(ledPin,HIGH);
delay(sensorValue);
}
else
{
digitalWrite(ledPin,LOW);
delay(sensorValue);
}
Serial.println(sensorValue);
}
Use the LDR code from the previous page to turn on and off the motor. It should turn on if it sees room temperature, and turn off if it gets warmer than room temperature. You may need to adjust the if statement as well as the variable names and comments to get this to work.
Demonstrate Potentiometer Controlling Buzzer
Materials:
- 100K potentiometer
- Buzzer
- 5V power source
Summary:
Similar to the two previous programs, you are to build a control circuit where the potentiometer controls whether a buzzer is on or off. The circuit should look similar to the one on the previous page except the middle pin of the potentiometer goes to the Arduino, the top pin goes to your 5V power supple, and the bottom pin goes to the ground. ON the output side , the buzzer requires 24mA as you will need to use a transistor as part of you circuit to get it to work since the Arduino cannot supply this from one single pin.(Without a transistor the Arduino will only be able to output about 19MA. It will still work without a transistor, it just won't be as loud)
#define sensorPin A0
#define ledPin 6
int sensorValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //LED 1
}
void loop()
{
sensorValue = analogRead(sensorPin);
if(sensorValue>500) // used half way point on potentiometer to trigger the switch at 500K ohms
{
digitalWrite(ledPin,HIGH);
delay(sensorValue);
}
else
{
digitalWrite(ledPin,LOW);
delay(sensorValue);
}
Serial.println(sensorValue);
}
Demonstrate Motion Sensor Controlling Relay
Materials:
Summary:
#define sensorPin A0
#define ledPin 6
int sensorValue = 0;
soid setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
if(sensorValue>950)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
delay(100);
Serial.println(sensorValue);
}