Exercise 22 - Connecting a RGB ring
1. INTRODUCTION
A RGB ring is a bundle of LEDs connected conveniently to control each one individually; it is similar to LED strip. There are two types of LED strip, both can be separated in segments and can be mono or RGB:
· Analog: LEDs of each segment are connected in series; they add the drop voltages of each one. On the other hand, segments are connected in parallel. This connection causes that all LEDs fade and blink at the same time.
· Digital: This type of strip has a driver chip that controls the LEDs individually. You can control it with a microcontroller to create colour patterns.
2. HOW TO CONNECT?
Materials:
· Arduino UNO board
· Protoboard
· Some wire Jumpers
· 10K ohms resistor
· Force resistive sensor.
To connect RGB ring, you have to connect 5V pin to 5V on Arduino board, GND to ground on Arduino and Digital input pin to pin6 on Arduino. You can connect more rings connecting Digital output of one to Digital input on another.
On EXERCISE_A, connect force sensor like in CONNECTING_A_FORCE_SENSITIVE_RESISTOR.
EXERCISE_A_&_B(COLUR_FORCE /COLOUR_WHEEL) |
|
3. PROGRAMMING
It is necessary for both exercises to add Adafruit_NeoPixel.h
EXERCISE_A(COLOUR_FORCE)
In this exercise, RGB ring actuates as force indicator. When the force is increased the ring turn-on more LEDs and change the colour of them.
First, you need to declare variables and ring parameters and on setup() you have to set up pin A0 as INPUT and do not forget to start ring communication.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Pin connected to the NeoPixels
#define PIN 6
//NeoPixels attached to the Arduino
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int i = 0;
int delayval = 20; // delay for half a second
int color;
int force = A0;
int value = 0;
void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
value = analogRead(force);//Read sensor's pin.
value = map(value, 0, 1023, 0, pixels.numPixels());
The first for loop, turn-on LEDs in function of force detected by sensor. pixels.setPixelColor(“i”,”colour”) function set the colour and pixels.Color(red,green,blue) configure the colour with value of the three primary colours.
for (i = 0; i <= value; i++) {
pixels.setPixelColor(i, pixels.Color(i*16, 0, 255-i*16));
}
The second for loop, erase the LEDs that are turned on by the first for. If you do not do this the LEDS are always turn on.
for (int k = pixels.numPixels(); k >= i; k--) {
pixels.setPixelColor(k, pixels.Color(0, 0, 0));
}
pixels.show();
}
EXERCISE_B (COLOUR_WHEEL)
This exercise animates the ring turning on and off consecutively and changing their colour.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Pin connected to the NeoPixels
#define PIN 6
//NeoPixels attached to the Arduino
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 20; // delay for half a second
int color;
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
colorWheel(70);
}
This function changes the colour of ring and turn on and off the LEDs you can change the velocity changing the parameter value.
void colorWheel(uint8_t wait) {
int i, j;
for (j = 0; j < 256; j=j+3) {
for (i = 0; i < pixels.numPixels(); i++) {
Parameter change it value in function of j and number of led i.
pixels.setPixelColor(i, Wheel((i*4+j) & 255));
delay(wait);
pixels.show();
}
for (i = 1; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, 0,0,0);
delay(wait/3);
pixels.show();
}
}
j=0;
}
This function changes the colour value in function of WheelPos parameter. The first condition change between red and blue, the second one between green and blue and the last one change the colour between red and green.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}