Showing posts with label 05. Control. Show all posts
Showing posts with label 05. Control. Show all posts

Saturday, July 23, 2016

While Loop


Sometimes you want everything in the program to stop while a given condition is true. You can do this using a while loop. This example shows how to use a while loop to calibrate the value of an analog sensor.

In the main loop, the sketch below reads the value of a photoresistor on analog pin 0 and uses it to fade an LED 
on pin 9. But while a button attached to digital pin 2 is pressed, the program runs a method called calibrate() that looks for the highest and lowest values of the analog sensor. When you release the button, the sketch continues with the main loop.

This technique lets you update the maximum and minimum values for the photoresistor when the lighting conditions change.





Step 1: What You Need?



Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.



Connect your analog sensor (e.g. potentiometer, light sensor) on analog input 2 with a 10K ohm resistor to ground. Connect your button to digital pin, again with a 10K ohm resistor to ground. Connect your LED to digital pin 9, with a 220 ohm resistor in series.


Step 3: Upload The Code.





1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.

2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.

3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 05.Control >> WhileStatementConditional

Click press the "upload" button (see the button with right arrow mark).

Download: 




05. Control library while whileloop

Switch (case) Statement, used with serial input 2


An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options.

This tutorial shows you how to use switch to turn on one of several different LEDs based on a byte of data received serially. The sketch listens for serial input, and turns on a different LED for the characters a, b, c, d, or e.



Step 1: What You Need?

Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.

Five LEDs are attached to digital pins 2, 3, 4, 5, and 6 in series through 220 ohm resistors.

To make this sketch work, your board must be connected to your computer. In the Arduino IDE open the serial monitor and send the characters a, b, c, d, or e to lit up the corresponding LED, or anything else to switch them off.




Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.

2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.

3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 05. Control >> switchCase2

Click press the "upload" button (see the button with right arrow mark).

Download: 



05. Control library swtichCase

Switch (case) Statement, used with sensor input


An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options. This tutorial shows you how to use it to switch between four desired states of a photoresistor: really dark, dim, medium, and bright.

This program first reads the photoresistor. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned.



Step 1: What You Need?

Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.

The photoresistor is connected to analog in pin 0 using a voltage divider circuit. A 10K ohm resistor makes up the other side of the voltage divider, running from Analog in 0 to ground. The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.



Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.

2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.

3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 05.Control >> switchCase


Click press the "upload" button (see the button with right arrow mark).

Download:

Arduino IDE




Sources:https://www.arduino.cc/en/Tutorial/SwitchCase
05. Control library swtichCase

If Statement (Conditional Statement)


The if() statement is the most basic of all programming control structures. It allows you to make something happen or not, depending on whether a given condition is true or not. It looks like this:

if (someCondition) {
   // do stuff if the condition is true
}
There is a common variation called if-else that looks like this:

if (someCondition) {
   // do stuff if the condition is true
} else {
   // do stuff if the condition is false
}
There's also the else-if, where you can check a second condition if the first is false:

if (someCondition) {
   // do stuff if the condition is true
} else if (anotherCondition) {
   // do stuff only if the first condition is false
   // and the second condition is true
}
You'll use if statements all the time. The example below turns on an LED on pin 13 (the built-in LED on many Arduino boards) if the value read on an analog input goes above a certain threshold.


Step 1: What You Need?



Step 2: Build Your Circuit.





Step 3: Upload The Code.




1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.


Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE. 

Select File >> Examples >> 05. Control >> IfStatementConditional

Click press the "upload" button (see the button with right arrow mark).

In the code above, a variable called analogValue is used to store the data collected from a potentiometer connected to the board on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the built-in LED connected to digital pin 13 is turned on. If analogValue is found to be < (less than) threshold, the LED remains off.



Download: 


05. Control IfStatementConditional library

Friday, July 22, 2016

For Loop Iteration (aka The Knight Rider)


Often you want to iterate over a series of pins and do something to each one. For instance, this example blinks 6 LEDs attached to the Arduino by using a for() loop to cycle back and forth through digital pins 2-7. The LEDs are turned on and off, in sequence, by using both the digitalWrite() and delay() functions .

We also call this example "Knight Rider" in memory of a TV-series from the 80's where David Hasselhoff had an AI machine named KITT driving his Pontiac. The car had been augmented with plenty of LEDs in all possible sizes performing flashy effects. In particular, it had a display that scanned back and forth across a line, as shown in this exciting fight between KITT and KARR. This example duplicates the KITT display.




Step 1: What You Need?



1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
6 x LEDs (RED)

6 x 220 Ohm Resistors
1 x Mini Breadboard 
1 x USB Type-B Cable 
Male-to-Male Jumper Wires 


Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.



Connect six LEDs, with 220 ohm resistors in series, to digital pins 2-7 on your board.



Step 3: Upload The Code.




1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.



2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.


Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE. 

Select File >> Examples >> 05. Control >> ForLoopIteration

Click press the "upload" button (see the button with right arrow mark).



The code below begins by utilizing a for() loop to assign digital pins 2-7 as outputs for the 6 LEDs used.

In the main loop of the code, two for() loops are used to loop incrementally, stepping through the LEDs, one by one, from pin 2 to pin seven. Once pin 7 is lit, the process reverses, stepping back down through each LED.


Download: 



05. Control For Loop Iteration library

Arrays


This variation on the For Loop Iteration example shows how to use an array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.

The For Loop Iteration example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino board, with certain limitations (the pins have to be numbered contiguously, and the LEDs have to be turned on in sequence).

This example shows you how you can turn on a sequence of pins whose numbers are neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use for loops to iterate over the array.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 ohm resistors, just like in the For Loop. However, here the order of the LEDs is determined by their order in the array, not by their physical order.

This technique of putting the pins in an array is very handy. You don't have to have the pins sequential to one another, or even in the same order. You can rearrange them in any order you want.




Step 1: What You Need?



1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
6 x LEDs (RED)

6 x 220 Ohm Resistors
1 x Mini Breadboard 
1 x USB Type-B Cable 
Male-to-Male Jumper Wires 


Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.



Connect six LEDs, with 220 ohm resistors in series, to digital pins 2-7 on your board.


Step 3: Upload The Code.



1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.


Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE. 

Select File >> Examples >> 05. Control >> Arrays

Click press the "upload" button (see the button with right arrow mark).


Download: 


05. Control Arrays library

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.