Color Picking Plate

From ElecFreaks
Jump to: navigation, search

Kit List

Arduino advanced kit provided by ELECFREAKS is based on our starter kit to expand more and more popular modules owing the characteristics of plug and use without any requirement of soldering. We have well prepared six courses for learners to learn the usage of modules in an intersting environment which is better for stimulating your thinking mind. For example,Tetris games help us to study the usage of the LCD screen and Arduino program. Color picker used can help us to learn the color sensor as well as BLE Bluetooth communication and Android interface development ... Practical Course included LCD screen, sensor, MP3, BLE, WIFI, Android development, and image interaction (processing), make you open-minded!

Project 4 - Interactive Traffic Lights


This time we are going to extend the previous project to include a set of pedestrian lights and a pedestrian push button to request to cross the road. The Arduino will react when the button is pressed by changing the state of the lights to make the cars stop and allow the pedestrian to cross safely.

For the first time we are able to interact with the Arduino and cause it to do something when we change the state of a button that the Arduino is watching (i.e. Press it to change the state from open to closed). In this project we will also learn how to create our own functions.

From now on when connecting the components we will no longer list the breadboard and jumper wires. Just take it as read that you will always need both of those.

What you will need


Connect it up

Connect the LEDʼs and the switch up as in the diagram on the previous page. You will need to shuffle the wires along from pins 8, 9 and 10 in the previous project to pins 10, 11 and 12 to allow you to connect the pedestrian lights to pins 8 and 9.

Enter the code

Enter the code on the next page, verify and upload it.

When you run the program you will see that the car traffic light starts on green to allow cars to pass and the pedestrian light is on red.

When you press the button, the program checks that at least 5 seconds have gone by since the last time the lights were changed (to allow traffic to get moving), and if so passes code execution to the function we have created called changeLights(). In this function the car lights go from green to amber then red, then the pedestrian lights go green. After a period of time set in the variable crossTime (time enough to allow the pedestrians to cross) the green pedestrian light will flash on and off as a warning to the pedestrians to get a hurry on as the lights are about to change back to red. Then the pedestrian light changes back to red and the vehicle lights go from red to amber to green and the traffic can resume.

The code in this project is similar to the previous project. However, there are a few new statements and concepts that have been introduced so letʼs take a look at those.

// Project 4 - Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
void loop() {
int state = digitalRead(button);
/* check if button is pressed and it is
over 5 seconds since last button press */
if (state == HIGH && (millis() - changeTime) > 5000) {
// Call the function to change the lights
void changeLights() {
digitalWrite(carGreen, LOW); // green off
digitalWrite(carYellow, HIGH); // yellow on
delay(2000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow off
digitalWrite(carRed, HIGH); // red on
delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
digitalWrite(pedGreen, LOW);
// turn ped red on
digitalWrite(pedRed, HIGH);
digitalWrite(carYellow, HIGH); // yellow on
digitalWrite(carRed, LOW); // red off
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop

Project 4 - Code Overview Most of the code in this project you will understand and recognise from previous projects. However, let us take a look at a few new keywords and concepts that have been introduced in this sketch.

unsigned long changeTime;

Here we have a new data type for a variable. Previously we have created integer data types, which can store a number between -32,768 and 32,767. This time we have created a data type of long, which can store a number from -2,147,483,648 to 2,147,483,647.However, we have specified an unsigned long, which means the variable cannot store negative numbers,which gives us a range from 0 to 4,294,967,295. If we were to use an integer to store the length of time since the last change of lights, we would only get a maximum time of 32 seconds before the integer variable reached a number higher than it could store.

As a pedestrian crossing is unlikely to be used every 32 seconds we donʼt want our program crashing due to our variable ʻoverflowingʼ when it tries to store a number too high for the variable data type. That is why we use an unsigned long data type as we now get a huge length of time in between button presses.

   4294967295 * 1ms = 4294967 seconds
    4294967 seconds = 71582 minutes
      71582 minutes - 1193 hours
        1193 hours - 49 days

As it is pretty inevitable that a pedestrian crossing will get itʼs button pressed at least once in 49 days we shouldnʼt have a problem with this data type.

You may well ask why we donʼt just have one data type that can store huge numbers all the time and be done with it. Well, the reason we donʼt do that is because variables take up space in memory and the larger the number the more memory is used up for storing variables. On your home PC or laptop you wonʼt have to worry about that much at all, but on a small microcontroller like the Atmega328 that the Arduino uses it is essential that we use only the smallest variable data type necessary for our purpose.