Hi Everyone,
I am new here. I am just wondering if anyone could help me out. I am looking at attaching an RGB led, an accelerometer, a motor and an electric imp. What I am currently working on is how to put together the code for the accelerometer and RGB.
What I want to happen is to have when the accelerometer is at particular co-ordinates, a particular colour will turn on. But I haven’t got much of an idea of how to go about this!
Here’s a previous thread where someone played with the ADXL335:
http://forums.electricimp.com/discussion/comment/8344
I’m not sure what RGB LED you have there, but configuring imp pins 1, 2 and 5 as PWM outputs might work. You’d use pin.write to set the relative intensities.
PWM is indeed what you need. Here’s my code for pulsing an RGD LED: red through green through blue.
`r <- hardware.pin1 // R
b <- hardware.pin2 // B
g <- hardware.pin5 // G
r.configure(PWM_OUT, 0.0025, 1.0)
b.configure(PWM_OUT, 0.0025, 1.0)
g.configure(PWM_OUT, 0.0025, 1.0)
rState <- 0.0
gState <- 0.0
bState <- 0.0
rDo <- false
gDo <- false
bDo <- false
rDelta <- 0.0
gDelta <- 0.0
bDelta <- 0.0
timer <- null
function init()
{
rState = 1.0
gState = 1.0
bState = 1.0
rDo = true
gDo = false
bDo = false
rDelta = -0.05
gDelta = -0.05
bDelta = -0.05
}
function pulse()
{
r.write(rState);
b.write(gState);
g.write(bState);
if (rDo) rState = rState + rDelta
if (gDo) gState = gState + gDelta
if (bDo) bState = bState + bDelta
if (rState <= 0.0)
{
gDo = true
bDo = false
}
if (gState <= 0.0)
{
bDo = true
rDo = false
}
if (bState <= 0.0)
{
gDo = false
rDo = true
}
if (rState >= 1.0 || rState <= 0.0) rDelta = rDelta * -1.0
if (gState >= 1.0 || gState <= 0.0) gDelta = gDelta * -1.0
if (bState >= 1.0 || bState <= 0.0) bDelta = bDelta * -1.0
timer = imp.wakeup(0.08, pulse);
}
function showColor(colorTable)
{
if (timer != null) imp.cancelwakeup(timer)
r.write(1.0)
b.write(1.0)
g.write(1.0)
r.write((255.0 - colorTable.red) / 255.0)
b.write((255.0 - colorTable.blue) / 255.0)
g.write((255.0 - colorTable.green) / 255.0)
init()
timer = imp.wakeup(10.0, pulse)
}
agent.on(“new.color”, showColor)
init()
pulse()
`