Outils pour utilisateurs

Outils du site


issue117:python

Ceci est une ancienne révision du document !


Welcome back or, if you are new to the series, welcome. This month we will be doing three projects driving up to nine LEDs. They are: • Two blinking LEDs • Cylon Lights • Bar Graph

The original project (bar graph) is from a book that I reviewed a few months ago: Arduino Project Handbook by Mark Geddes (No Starch Press). I really enjoyed the book so I wanted to use at least one of his projects.

Let’s get started by laying out the parts list and looking at the hardware layout.

The Parts List

For the projects this time, you will need: • An Arduino Uno or Mega • 9 x LEDs (preferably 3 red, 3 yellow and 3 green) • 9 x 220 Ohm resistors • 10K Potentiometer • Breadboard • Jumpers

The Hardware Layout

Shown right is the Fritzing breadboard layout (I am also including the schematic, shown bottom right, for those who like to see that sort of thing.) for the Bargraph project. We can use the same component layout for all three projects, since our code will ignore any extra components.

Notice that the long Anode LED leads are connected to the 220 ohm resistors which are then connected to the Arduino pins 2-10 (positive LED pins), and the short Cathode LED leads (negative LED pins) are all connected to ground.

We’ll go through the various components when we discuss each project.

Project 1 - Two Blinking LEDs

This first project is really simple in both logic and implementation. The idea is to alternately turn on and off two LEDs. In this case the LEDs are the ones connected to Arduino pins 2 and 3. We’ll use the potentiometer to send a value between 0 and 1023 for the delay through Arduino analogue pin A0. The higher the value the longer the delay. Since a delay value below 30 can cause the LEDs to blink so fast that you can’t tell they are blinking, we will check the value and if it is less than 30, we’ll force it to 30.

The Code

const int ledPin1 = 2; const int ledPin2 = 3; const int analogPin = A0;

In the first three lines, we are setting the constants that we’ll need.

void setup() {

Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);

}

In the setup routine, we start the serial monitor to transmit at 9600 baud and the two digital pins as OUTPUT pins.

void loop() {

 int speedReading = analogRead(analogPin);
 if (speedReading < 30) {
    speedReading = 30;
 }

Now we read, using the analogRead function call, the value of the potentiometer, and if the value is less than 30, force it up to 30.

 Serial.println(speedReading);   digitalWrite(ledPin1,HIGH);

delay(speedReading); digitalWrite(ledPin1,LOW); digitalWrite(ledPin2,HIGH); delay(speedReading); digitalWrite(ledPin2,LOW); }

Finally, we print to the Serial Monitor the value of the pot, turn on the first LED, delay however many milliseconds the pot value is, turn off the led, then turn on the next one, delay and turn if off and then repeat the entire process.

See how simple that was?

Project 2 - Cylon Lights

In this project, we will light the LEDs in a sweep motion (0 to 8 and 8 to 0), right and left, reminiscent of the Cylons from the original 1978 Battlestar Galactica television show. (I showed the running project to a friend and his response was that it looks like the lights on a police car. I guess it’s just a matter of perspective.) Again, it is a VERY simple project. We will be using all 9 of the LEDs in this project. Since I worked with the bargraph project first, I simply modified the code to create this one.

We use two simple for loops to switch the LEDs on and off in order, starting with the Arduino pin 2, going up to pin 10, and then back down to pin 2.

The Code

const int ledCount = 9;

const int delayTime = 90;

int ledPins[] = {2,3,4,5,6,7,8,9,10};

Here we set up the various variables we will be using. The first two are defined as constants and the third is set up as an array which holds the Arduino pin numbers that are connected to our LEDs.

In the setup routine (below), we use a for loop to define each of the pins in the array as OUTPUT pins.

The loop routine (next page) is where the “magic” happens. Again, we use a simple for loop to sweep the LED on in order for 90 milliseconds, then turn it off before we move to the next pin. Once we have gone through the first 9 LEDs, we do another loop, this time moving backwards through the list of the LEDs. Notice, however, that we skip the ninth led on the count down.

As you can see (next page, top right), the for loop in C works like this…

  for (counter value low, counter value high, amount to increment or decrement)

By this time, it should be simple for you to figure it all out.

Project 3 - Bar Graph

As I said earlier, this project is from the book Arduino Project Handbook by Mark Geddes. It’s a very easy project code wise.

We will be using all the hardware in this one. The idea it to give a “graphical” representation of the voltage value of the potentiometer using the nine LEDs. The lower the voltage going into the A0 pin, the fewer LEDs are lit. The higher the voltage, more are lit. The Arduino C language gives us a wonderful function called MAP that makes this a breeze. However, it can be a bit confusing at first.

The Map function

The map function takes a value, a low and high range of the input, and a low and high range that the output should be mapped to. Our code is as follows…

int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

• ledLevel is the mapped output. • sensorReading is the input level from the analogue input pin. • The values 0 and 1023 are the range that can be expected from the analogue pin. • The values 0 to 9 (ledCount) are the values that can be expected as the output. There is some math magic being done in the function that produces the output map. The following table shows the Input v.s. Output values.

So you can see that anytime that the voltage value into pin A0 is, for example, between 455 and 568, the output will be a 4, and in this case, the first four LEDs will be lit.

The Code

You have seen the first three lines and the setup routine already, so we’ll just skip over the discussion.

That’s it. You now have learned a lot about the Arduino language and controlling LEDs.

Next time, we will be working with some of the motors that we used when we were learning the RPi, so dust them off and be ready. Until then, have fun!

issue117/python.1485857281.txt.gz · Dernière modification : 2017/01/31 11:08 de auntiee