20
RGB LED Arduino App by MICILE Description This app allows you to run a program that controls an RGB LED sensor that is connected to an Arduino board. The project includes six different python files and instructions, as well as a short video of what the finished project should look like. Materials you will need: MICILE Tablet Arduino microprocessor RGB LED Keyes KY-016 compatible sensor Red, green, blue, black jumper wires Ping pong ball Some background information: Use Red Jumper Wire connect Pin 1 on KY-016 (labeld B) to pin 9 of Arduino Use Green Jumper Wire connect Pin2 on KY-016 (labeled G) to pin 10 of Arduino Use Blue Jumper Wire connect Pin 3 on KY-016 (labeld R) to pin 11 of Arduino Use Black Jumper Wire connect Pin4 of KY-016 (labeled -) to GND of Arduino The red jumper wire, when set to 5V from Arduino (high), makes the LED glow red. The green jumper wire, when set to 5V from Arduino (high), makes the LED glow green. The blue jumper wire, when set to 5V from Arduino (high), makes the LED glow blue. The black jumper wire is ground (like - on a battery). Think of the LED as three different light bulbs, a red bulb, a blue bulb, and a green bulb. When power flows through the red wire, it goes through the red bulb and then out the black wire lighting the red bulb. It’s the same for the blue and the green bulbs. We can also mix colors, i.e. turn on the blue and the red and it will look purple because blue light and red light mixed together make purple light.

MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

  • Upload
    ngoanh

  • View
    226

  • Download
    5

Embed Size (px)

Citation preview

Page 1: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

RGB LED Arduino App by MICILE

DescriptionThis app allows you to run a program that controls an RGB LED sensor that is connected to an Arduino board. The project includes six different python files and instructions, as well as a short video of what the finished project should look like.

Materials you will need:MICILE TabletArduino microprocessor RGB LED Keyes KY-016 compatible sensorRed, green, blue, black jumper wiresPing pong ball

Some background information:● Use Red Jumper Wire connect Pin 1 on KY-016 (labeld B) to pin 9 of Arduino● Use Green Jumper Wire connect Pin2 on KY-016 (labeled G) to pin 10 of Arduino● Use Blue Jumper Wire connect Pin 3 on KY-016 (labeld R) to pin 11 of Arduino● Use Black Jumper Wire connect Pin4 of KY-016 (labeled -) to GND of Arduino

The red jumper wire, when set to 5V from Arduino (high), makes the LED glow red. The green jumper wire, when set to 5V from Arduino (high), makes the LED glow green. The blue jumper wire, when set to 5V from Arduino (high), makes the LED glow blue. The black jumper wire is ground (like - on a battery).

Think of the LED as three different light bulbs, a red bulb, a blue bulb, and a green bulb. When power flows through the red wire, it goes through the red bulb and then out the black wire lighting the red bulb. It’s the same for the blue and the green bulbs. We can also mix colors, i.e. turn on the blue and the red and it will look purple because blue light and red light mixed together make purple light.

How to control brightness: Even though the light bulb can only be fully on or fully off, we can make it look dimmer by turning the bulb on and off rapidly. If we leave it off longer than on, it will look dimmer. This is what Pulse Width Modulation does. For example (this is exaggerated) if we always have a cycle as 1 second, and we turn on the bulb for 1/4 seconds and off for 3/4 seconds, the bulb will appear to be at about 1/4 brightness. This is called a duty cycle of 25%.

In the program below we adjust the brightness by setting different duty cycles for each color of Red, Green, and Blue.In reality, the PWM happens very fast about 31000 times a second so the human eye does not see any flickering even though the LED is turning on and off very rapidly.

Page 2: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

PVM Waveform

STEP 1: SET UPConnect the jumper wires to the Arduino board as shown below.

RGB LED Keyes KY-016 Compatible Sensor

Page 3: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

RGB LED Keyes sensor correctly connected to jumper wires

Arduino board correctly connected to jumper wires

Page 4: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

Cover the LED with a ping pong ball to diffuse the light. Your final setup should look like the image above.

STEP 2: MAKE A LARGE RED SLIDING BAR TO SWITCH RED LIGHT ONCreate a new project in your MICILE Browser IDE. Name it RGB_Arduino. Open the project. Create a new file. Name the file RGB_Aduino_1.py. Type, or copy and paste the code in to this file as shown below.

Copy code from below and paste it in the MICILE IDE browser. # --------------------------------------------------# Arduino RGB Part 1a# --------------------------------------------------

# import the ez_arduino library v0.9 and the time libraryfrom ez_arduino_09 import *from ez_graphics_09 import *from ez_touchscreen_09 import *import time

# Initialize the Arduinoarduino_init()

# Configure Pins for PWMarduino_configure_pin(PIN_9, PIN_PWM)

# Set the Blue channel to be off at 0% duty cyclearduino_set_pwm(PIN_9, 0.0) #red

# clear the screenclear_screen('black')

# Loop forever

Page 5: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

while True: # read the touchscreen touch_point = touchscreen_finger_point()

# process the point if a finger is touching if touch_point != None: # get the x and y coordinates of the touch x = touch_point['x'] y = touch_point['y']

# fill the screen with red depending on where the user touched fill_rect(0, 0, x, 480, 'red')

# fill the rest of the screen with black to erase any old red bar fill_rect(x, 0, 800 - x, 480, 'black')

# calculate as a percentage where the user touched the screen horizontally # notice we have to case the x to a float in order to get an answer # that is not rounded to the nearest decimal percentage = float(x) / 800

# this percentage is actually the duty cycle of the PWM duty_cycle = percentage

# set the red part of the LED to flicker at this duty cycle arduino_set_pwm(PIN_9, duty_cycle)

# sleep so app will stop when interrupted by the IDE time.sleep(0.01)

This is what you should see on the screen.

Save and run the file. When you touch the LCD screen on the MICILE Tablet, you will see a red rectangular bar that grows as you slide your finger to the right. As the rectangle gets larger, the red light

Page 6: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

appears brighter. This is what the MICILE Tablet LCD screen should look like after you run RGB_Arduino_1.py.

Now we will want to make this much smaller!

STEP 3: MAKE THE RED BAR NARROWERNext you will narrow the height of the red bar by changing the y value. You can play with the numbers to see what happens. Create a new file in the same project. Name the file RGB_Aduino_2.py.Then, cut the following code. Paste the code into this file as you did before.

Copy code from below and paste it in the MICILE IDE browser.

# --------------------------------------------------# Arduino RGB Part 2# --------------------------------------------------

# import the ez_arduino library v0.9 and the time libraryfrom ez_arduino_09 import *from ez_graphics_09 import *from ez_touchscreen_09 import *import time

# Initialize the Arduinoarduino_init()

# Configure Pins for PWMarduino_configure_pin(PIN_9, PIN_PWM)

# Set the Blue channel to be off at 0% duty cyclearduino_set_pwm(PIN_9, 0.0) #red

Page 7: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

# clear the screenclear_screen('black')draw_rect(0, 200, 800, 100, 'red')

# Loop foreverwhile True: # read the touchscreen touch_point = touchscreen_finger_point()

# process the point if a finger is touching if touch_point != None: # get the x and y coordinates of the touch x = touch_point['x'] y = touch_point['y']

# only process if y is between 200 and 300 if y > 200 and y < 300: # fill the screen with red depending on where the user touched fill_rect(0, 200, x, 100, 'red')

# fill the rest of the screen with black to erase any old red bar fill_rect(x, 200, 800 - x, 100, 'black')

# calculate as a percentage where the user touched the screen horizontally # notice we have to case the x to a float in order to get an answer # that is not rounded to the nearest decimal percentage = float(x) / 800

# this percentage is actually the duty cycle of the PWM duty_cycle = percentage

# set the red part of the LED to flicker at this duty cycle arduino_set_pwm(PIN_9, duty_cycle)

# sleep so app will stop when interrupted by the IDE time.sleep(0.01)

Save and run the file. When you touch the LCD screen on the MICILE Tablet, you will see a red smaller rectangular bar that grows as you slide your finger to the right. As the rectangle gets larger, the red light appears brighter. This is what the MICILE Tablet LCD screen should look like after you run RGB_Arduino_2.py. This is what you should see:

Page 8: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

Notice that the led doesn’t really turn off since it is hard to get the percentage down to zero, we can fix that by making the box have a border of 100 pixels on the left and right sides of the screen and counting anything touches in the left border as 0% and any touches in the right border as 100%.

STEP 4: THE RED BAR HAS AN OUTLINE THAT STAYS ON THE SCREEN

Next you will change the code so that a red outline remains on the screen. You can play with the numbers to see what happens. Create a new file in the same project. Name the file RGB_Aduino_3.py. Then, cut the following code. Paste the code into this file as you did before.

Copy code from below and paste it in the MICILE IDE browser.

# --------------------------------------------------# Arduino RGB Part 3# --------------------------------------------------

# import the ez_arduino library v0.9 and the time libraryfrom ez_arduino_09 import *from ez_graphics_09 import *from ez_touchscreen_09 import *import time

# Initialize the Arduinoarduino_init()

# Configure Pins for PWMarduino_configure_pin(PIN_9, PIN_PWM)

# Set the Blue channel to be off at 0% duty cyclearduino_set_pwm(PIN_9, 0.0) #red

Page 9: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

# clear the screenclear_screen('black')draw_rect(0, 199, 800, 102, 'red')

# Loop foreverwhile True: # read the touchscreen touch_point = touchscreen_finger_point()

# process the point if a finger is touching if touch_point != None: # get the x and y coordinates of the touch x = touch_point['x'] y = touch_point['y']

# only process if y is between 200 and 300 if y > 200 and y < 300: # fill the screen with red depending on where the user touched fill_rect(1, 200, x, 100, 'red')

# fill the rest of the screen with black to erase any old red bar fill_rect(x, 200, 799 - x, 100, 'black')

# calculate as a percentage where the user touched the screen horizontally # notice we have to case the x to a float in order to get an answer # that is not rounded to the nearest decimal percentage = float(x) / 800

# this percentage is actually the duty cycle of the PWM duty_cycle = percentage

# set the red part of the LED to flicker at this duty cycle arduino_set_pwm(PIN_9, duty_cycle)

# sleep so app will stop when interrupted by the IDE time.sleep(0.01)

Page 10: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

Success! Now we need to add a bar to control the blue and the green colors.

STEP 5:

Next, we need to shrink the bar down a bit. Create a new file in the same project. Name the file RGB_Aduino_4.py. Then, cut the following code. Paste the code into this file as you did before.

Copy code from below and paste it in the MICILE IDE browser.

# --------------------------------------------------# Arduino RGB Part 4# --------------------------------------------------

# import the ez_arduino library v0.9 and the time libraryfrom ez_arduino_09 import *from ez_graphics_09 import *from ez_touchscreen_09 import *import time

# Initialize the Arduinoarduino_init()

# Configure Pins for PWMarduino_configure_pin(PIN_9, PIN_PWM)

# Set the Blue channel to be off at 0% duty cyclearduino_set_pwm(PIN_9, 0.0) #red

# clear the screen

Page 11: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

clear_screen('black')draw_rect(100, 199, 600, 102, 'red')

# Loop foreverwhile True: # read the touchscreen touch_point = touchscreen_finger_point()

# process the point if a finger is touching if touch_point != None: # get the x and y coordinates of the touch x = touch_point['x'] y = touch_point['y']

# only process if y is between 200 and 300 if y > 200 and y < 300: # if x is past the left edge of the box, make it the left edge if x < 100: x = 100 # if x is past the right edge of the box, make it the right edge if x > 700: x = 700

# fill the screen with red depending on where the user touched fill_rect(101, 200, x - 100, 100, 'red')

# fill the rest of the screen with black to erase any old red bar fill_rect(x, 200, 699 - x, 100, 'black')

# calculate as a percentage where the user touched the screen horizontally # notice we have to case the x to a float in order to get an answer # that is not rounded to the nearest decimal percentage = float(x - 100) / 600

# this percentage is actually the duty cycle of the PWM duty_cycle = percentage

# set the red part of the LED to flicker at this duty cycle arduino_set_pwm(PIN_9, duty_cycle)

# sleep so app will stop when interrupted by the IDE time.sleep(0.01)

Page 12: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

Now we’re getting somewhere. Lets finish it off by adding a green and a blue bar.

STEP 6: ADD A BLUE AND A GREEN BAR

Next you will add a function and a bar for the red and green colors as well. Create a new file in the same project. Name the file RGB_Aduino_5.py. Then, cut the following code. Paste the code into this file as you did before.

Copy code from below and paste it in the MICILE IDE browser.# --------------------------------------------------# Arduino Part 5# --------------------------------------------------

# import the ez_arduino library v0.9 and the time libraryfrom ez_arduino_09 import *from ez_graphics_09 import *from ez_touchscreen_09 import *import time

# Initialize the Arduinoarduino_init()

# Configure Pins for PWMarduino_configure_pin(PIN_9, PIN_PWM)arduino_configure_pin(PIN_10, PIN_PWM)arduino_configure_pin(PIN_11, PIN_PWM)

# Configure for a 25% duty cyclearduino_set_pwm(PIN_9, 0.0) #bluearduino_set_pwm(PIN_10, 0.00) #greenarduino_set_pwm(PIN_11, 0.00) #red

# draw 3 rectanglesclear_screen('black')draw_rect(99, 49, 402, 52, 'red')draw_rect(99, 149, 402, 52, 'green')draw_rect(99, 249, 402, 52, 'blue')

Page 13: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

# Loop foreverwhile True: # read the touchscreen touch_point = touchscreen_finger_point()

# process the point if a finger is touching if touch_point != None: # get the x and y coordinates of the touch x = touch_point['x'] y = touch_point['y']

# check if we are touching the red bar if y > 50 and y < 100: # draw the red bar val = (float(x) - 100) / 400 if val < 0: val = 0 if val > 1.0: val = 1.0 width = int(400 * val) fill_rect(100, 50, width, 50, 'red') fill_rect(100 + width, 50, 399 - width, 50, 'black') arduino_set_pwm(PIN_9, val)

# check if we are touching the green bar if y > 150 and y < 200: # draw the green bar val = (float(x) - 100) / 400 if val < 0: val = 0 if val > 1.0: val = 1.0 width = int(400 * val) fill_rect(100, 150, width, 50, 'green') fill_rect(100 + width, 150, 399 - width, 50, 'black') arduino_set_pwm(PIN_10, val)

# check if we are touching the blue bar if y > 250 and y < 300: # draw the blue bar val = (float(x) - 100) / 400 if val < 0: val = 0 if val > 1.0: val = 1.0 width = int(400 * val) fill_rect(100, 250, width, 50, 'blue') fill_rect(100 + width, 250, 399 - width, 50, 'black') arduino_set_pwm(PIN_11, val)

# sleep so app will stop when interrupted by the IDE time.sleep(0.01)

This is what you should see on your MICILE Tablet now.

Page 14: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run
Page 15: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

Now there is one last fun experiment to do.

STEP 7: NOW COMBINE IT WITH THE CAMERA APP

Last, we will be adding the camera to the app. You will be able to point the camera at any colored object and the RGB LED will turn that color. Try it with something bright orange. Create a new file in the same project. Name the file RGB_Aduino_6.py. Then, cut the following code. Paste the code into this file as you did before.

Copy code from below and paste it in the MICILE IDE browser.

# --------------------------------------------------# camera_demo.py# --------------------------------------------------

# import the ez_camera library v0.9# import the ez_graphics library v0.9# import the ez_arduino library v0.9from ez_camera_09 import *from ez_graphics_09 import *from ez_arduino_09 import *import timeimport math

# Initialize the Arduinoarduino_init()

# Configure Pins for PWMarduino_configure_pin(PIN_9, PIN_PWM)arduino_configure_pin(PIN_10, PIN_PWM)arduino_configure_pin(PIN_11, PIN_PWM)

Page 16: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

# clear the screenclear_screen('black')

# loop foreverwhile True: # get an image from the camera img = camera_capture_image()

# resize the image to 320 x 240 img = resize_image(img, 320, 240)

# set the image as the drawing image old_drawing_image = set_drawing_image(img)

# draw a white rectangle on the image along with a line heading east draw_rect(154, 114, 12, 12, 'white') draw_line(165, 120, 320, 120, 'white')

# extract a 20 x 20 pixel image from the center of the camera image img_roi = capture_image(155, 115, 10, 10)

# set the drawing image back to the screen and draw the camera image set_drawing_image(old_drawing_image) draw_image(img, 20, 20)

draw_image(img_roi, 400, 10)

# draw an outline around the camera image draw_rect(19, 19, 322, 242, 'white')

# draw a horizontal line and a white square for the average color outline draw_line(340, 140, 400, 140, 'white') draw_rect(399, 119, 42, 42, 'white')

# loop through the 100 pixels in the extracted image and generate an average color roi_data = get_image_data(img_roi) (r, g, b) = (0, 0, 0) for i in range(0, 10 * 10 * 4, 4): b = b + roi_data[i + 0] g = g + roi_data[i + 1] r = r + roi_data[i + 2] set_color(r / 100, g / 100, b / 100) fill_rect(400, 120, 40, 40)

# draw white lines to connect to the color bars draw_line(420, 160, 420, 312, 'white') draw_line(420, 212, 450, 212, 'white') draw_line(420, 262, 450, 262, 'white') draw_line(420, 312, 450, 312, 'white')

# calculate the duty cycles of each color dcr = float(r) / (100 * 255) dcg = float(g) / (100 * 255)

Page 17: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

dcb = float(b) / (100 * 255)

# draw the red, green, and blue bars draw_rect(450, 200, 100, 25, 'red') fill_rect(451, 201, int(98 * dcr), 23, 'darkred') fill_rect(451 + int(98 * dcr), 201, int(98 * (1 - dcr)), 23, 'black') draw_rect(450, 250, 100, 25, 'lime') fill_rect(451, 251, int(98 * dcg), 23, 'green') fill_rect(451 + int(98 * dcg), 251, int(98 * (1 - dcg)), 23, 'black') draw_rect(450, 300, 100, 25, 'blue') fill_rect(451, 301, int(98 * dcb), 23, 'darkblue') fill_rect(451 + int(98 * dcb), 301, int(98 * (1 - dcb)), 23, 'black')

# set the RGB LED connected to arduino have the same color arduino_set_pwm(PIN_9, max(0, dcr / 8 - 0.05)) arduino_set_pwm(PIN_10, max(0, dcg / 8 - 0.05)) arduino_set_pwm(PIN_11, max(0, dcb / 8 - 0.05))

# sleep so app will stop when interrupted time.sleep(0.01)

Page 18: MICILE Arduino RGB LED Final version.docxmicile.com/.../2014/09/MICILEArduinoRGBLEDFinalversion1.docx · Web viewRGB LED Arduino App by MICILE Description This app allows you to run

Let us know if you come up with any other creative ideas! Contact us at www.micile.com.