I'm a new arduino user, so I'm learning now...
I want make a new class to control my RGB led...
I already use this led with a method on my code... But I want to generalize the code to put more led without ctrl+c ctrl+v my code.
I create the class:
/*
STATUSLED CLASS DEFINITION
*/
class StatusLED {
int pinVermelho;
int pinVerde;
int pinAzul;
public:
StatusLED(int pinRed, int pinGreen, int pinBlue) {
this->pinVermelho = pinRed;
this->pinVerde = pinGreen;
this->pinAzul = pinBlue;
}
void RGB(int red, int green, int blue) {
Serial.print("StatusLED.RGB(");
Serial.print(red);
Serial.print(", ");
Serial.print(green);
Serial.print(", ");
Serial.print(blue);
Serial.println(");");
digitalWrite(pinVermelho, 255 - red);
digitalWrite(pinVerde, 255 - green);
digitalWrite(pinAzul, 255 - blue);
}
};
I initialize that with the pins of my RBG led:
#define pinoAzul 9
#define pinoVerde 10
#define pinoVermelho 11
StatusLED led(pinoVermelho, pinoVerde, pinoAzul);
And to test it I use:
void setup() {
pinMode(pinoAzul, OUTPUT);
digitalWrite(pinoAzul, LOW);
pinMode(pinoVerde, OUTPUT);
digitalWrite(pinoVerde, LOW);
pinMode(pinoVermelho, OUTPUT);
digitalWrite(pinoVermelho, LOW);
}
void loop() {
led.RGB(255, 0, 0);
delay(1000);
led.RGB(0, 255, 0);
delay(1000);
led.RGB(0, 0, 255);
delay(1000);
}
The method RGB is called, but my led don't turn on.
If i move the RGB method to outside of my class, this works fine.
Can someone please tell my what I'm doing worng?
You are attempting to control an RGB LED using PWM (Pulse Width Modulation). To set the pulse on/off ratio on a specific PWM pin, you should use analogWrite(PWM_out_pin, PWM_out_level);
Your code is incorrectly using digitalWrite which doesn't affect the PWM wave form. It also only takes a HIGH or LOW value parameter besides pin number.
I moved the configuration of pins as OUTPUT to constructor of my class like that:
StatusLED::StatusLED(int pinRed, int pinGreen, int pinBlue) {
this->pinRed = pinRed;
pinMode(pinRed, OUTPUT);
this->pinGreen = pinGreen;
pinMode(pinGreen, OUTPUT);
this->pinBlue = pinBlue;
pinMode(pinBlue, OUTPUT);
RGB(0, 0, 0);
}
This solve the problem.
I also change the method RGB to use analogWrite as you tell and now the fade is working well too.
void StatusLED::RGB(int redValue, int greenValue, int blueValue) {
if (redValue > 255) {
redValue = 255;
}
if (greenValue > 255) {
greenValue = 255;
}
if (blueValue > 255) {
blueValue = 255;
}
if (redValue < 0) {
redValue = 0;
}
if (greenValue < 0) {
greenValue = 0;
}
if (blueValue < 0) {
blueValue = 0;
}
analogWrite(pinRed, 255 - redValue);
analogWrite(pinGreen, 255 - greenValue);
analogWrite(pinBlue, 255 - blueValue);
}
Thanks everyone!
Related
Hi I am trying to build a arduino project to control led strip and change the light pattern when pressed different button
the code i am using is
#include <IRremote.h> //include the library
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 70
#define DATA_PIN 3
#define CLOCK_PIN 13
#define Button_0 0xFF9867
#define Button_1 0xFFA25D
#define Button_2 0xFF629D
#define Button_3 0xFFE21D
#define Button_4 0xFF22DD
#define Button_5 0xFF02FD
#define Button_6 0xFFC23D
#define Button_7 0xFFE01F
#define Button_8 0xFFA857
#define Button_9 0xFF906F
int receiver = 7; //initialize pin 13 as recevier pin.
IRrecv irrecv(receiver); //create a new instance of receiver
decode_results results;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
LEDS.setBrightness(84);
irrecv.enableIRIn(); //start the receiver
}
void loop() {
if (irrecv.decode(&results)) { //if we have received an IR signal
Serial.println (results.value, HEX); //display HEX results
irrecv.resume(); //next value
if (results.value == Button_0) {
RunningLightSlow();
}
else if(results.value == Button_1) {
RainbowLights();
}
// switch (results.value) {
//
// case Button_0: RunningLightSlow(); break;
// case Button_1: RainbowLights(); break;
// }
}
}
// LIGHTSHOW 0 starts --------------------------------------------------------------------------------------------------
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
void RunningLightSlow() {
while(1) {
static uint8_t hue = 0;
Serial.print("x");
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
Serial.print("x");
// Now go in the other direction.
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
}
}
// LIGHTSHOW 0 ends --------------------------------------------------------------------------------------------------------
// LIGHTSHOW 1 STARTS ------------------------------------------------------------------------------------------------------
void RainbowLights() {
randomSeed(millis());
int wait=random(10,30);
int dim=random(4,6);
int max_cycles=8;
int cycles=random(1,max_cycles+1);
while(1) {
rainbowCycle(wait, cycles, dim);
}
}
void rainbowCycle(int wait, int cycles, int dim)
{
Serial.println("Let's make a rainbow.");
//loop several times with same configurations and same delay
for(int cycle=0; cycle < cycles; cycle++)
{
byte dir=random(0,2);
int k=255;
//loop through all colors in the wheel
for (int j=0; j < 256; j++,k--)
{
if(k<0)
{
k=255;
}
//Set RGB color of each LED
for(int i=0; i<NUM_LEDS; i++)
{
CRGB ledColor = wheel(((i * 256 / NUM_LEDS) + (dir==0?j:k)) % 256,dim);
leds[i]=ledColor;
}
FastLED.show();
FastLED.delay(wait);
}
}
}
CRGB wheel(int WheelPos, int dim)
{
CRGB color;
if (85 > WheelPos)
{
color.r=0;
color.g=WheelPos * 3/dim;
color.b=(255 - WheelPos * 3)/dim;;
}
else if (170 > WheelPos)
{
color.r=WheelPos * 3/dim;
color.g=(255 - WheelPos * 3)/dim;
color.b=0;
}
else
{
color.r=(255 - WheelPos * 3)/dim;
color.g=0;
color.b=WheelPos * 3/dim;
}
return color;
}
Now the issue i am facing is when i press a button (for eg 0) it runs the function RunningLightSlow(); i have written the infinite loop code because i want infinite light show and when i press button 1 it does not change the light pattern ,
Root cause would be once the function goes into infinite loop it then never receives the ir signal and hence never change the light pattern
Remove all infinite loops from your program and create a simple structure in loop() where you check for IR signal and update a "state" variable with the last button pressed.
void loop() {
// create a function or add here whatever is needed to read the IR code
irCode = readIrCode();
// check if the new code is identical to a previous code
if (lastIrCode != irCode) {
// memorize the current code
lastIrCode = irCode;
/ act according to user selection
switch (irCode) {
case Button_0:
doThis(); // this could be your RunningLightSlow()
break;
case Button_1:
doThat(); // maybe RainbowLights()
break;
default:
// for cases you have not handled above
doWhatever();
}
}
}
Do not forget to remove the infinite loops from the functions that run the lights and obviously you would need to declare the variables and set the correct calls.
I am working on a project and I encountered some problems.
I am using a DHT11 temperature sensor, an Arduino Uno and a TFT LCD display 2.2-inch model ITDB02-2.2.
What I want my project to do is to use 2 functioning modes for the sensor that I can select from the keyboard at the beginning of the program(one which is normal and one which will be used on special occasions)(so I need serial communication).
I noticed that the screen does not function if I start a serial communication at any rate so I used Arduino Serial.begin(9600) and Serial.end() for the mode selecting part of the program.
THE PROBLEM: My Arduino is still sending data through serial port even if I ended the serial communication and is looking like this:
I found out that Serial.end() function does not shut off serial communication but just the rate of communication. I am curious if you have any idea that I can use in order to get rid of the extra data, to neglect it before the computer receives it.
I`m stuck. I thought that interruptions would be a solution but they are not as far as I researched on the internet.
My ARDUINO CODE:
#include <SimpleDHT.h>
#include <UTFT.h>
UTFT myGLCD(ITDB22,A5,A4,A3,A2);
SimpleDHT11 dht11;
// Declare which fonts we will be using
extern uint8_t BigFont[];
//dht sensor data pin
int dataPinSensor1 = 12;
char mode;
int del;
void setup()
{
Serial.begin(9600);
Serial.print("Select functioning mode");
mode=SensorModeSelect(mode);
Serial.end();
pinMode(12, INPUT);
}
void loop()
{
if(mode=='1') {
FirstFuncMode(dataPinSensor1);
}
if(mode=='2') {
SecondFuncMode(dataPinSensor1,del);
}
delay(10);
}
char SensorModeSelect(char in)
{
char mode='0';
while(mode=='0') {
if(Serial.available() > 0) {
mode=Serial.read();
}
}
if (mode == '1') {
Serial.print("\nMOD1 SELECTED: press t key to aquire data \n");
}
if (mode == '2') {
Serial.print("\nMOD2 SELECTED: press q if you want to quit auto mode \n");
Serial.print("Select the data aquisition period(not smaller than 1 second) \n");
}
return mode;
}
int DataAqPeriod()
{
int del=0;
while(del==0) {
while(Serial.available() > 0) {
//Get char and convert to int
char a = Serial.read();
int c = a-48;
del *= 10;
del += c;
delay(10);
}
}
del*=1000;
return del;
}
void FirstFuncMode(int dataPinSensor1)
{
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
bool DispCond=false;
Serial.begin(9600);
delay(1500);
if (Serial.read() == 't' ) {
DispCond=true;
//read temperature and compare it with an error value
if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("unreliable measurement or unselected functioning mode");
}
byte f = temperature * 1.8 + 32;
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)f);
Serial.print(" *F, ");
Serial.print((int)humidity);
Serial.println(" H humidity");
delay(1500);
}
Serial.end();
if(DispCond==true) {
//Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
//print value on LCD
displayNoInit((int)temperature,(int)humidity);
}
}
void SecondFuncMode(int dataPinSensor1,int del)
{
bool q=false;
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
Serial.begin(9600);
del=DataAqPeriod();
Serial.end();
//Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
while(q==false) {
Serial.begin(9600);
//read temperature and compare it with an error value
if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("unreliable measurement or unselected functioning mode \n");
}
float f = temperature * 1.8 + 32;
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)f);
Serial.print(" *F, ");
Serial.print((int)humidity);
Serial.println(" H humidity");
delay(del);
if(Serial.read() == 'q')
q=true;
Serial.end();
displayNoInit((int)temperature,(int)humidity);
delay(10);
}
}
void displayNoInit(int temperature,int humidity)
{
//effective data display
myGLCD.clrScr();
myGLCD.setColor(255, 255, 0);
myGLCD.setBackColor(10,10,10);
myGLCD.print(" Temperature ", CENTER, 10);
myGLCD.setColor(254, 254, 254);
myGLCD.printNumI(temperature, CENTER, 45);
myGLCD.setColor(255, 255, 0);
myGLCD.print("C ", RIGHT, 45);
myGLCD.print("Relative Hum.", CENTER, 90);
myGLCD.setColor(204, 245, 250);
myGLCD.printNumI(humidity, CENTER, 120);
myGLCD.print("%", RIGHT, 120);
}
You are correct in the definition that Serial.end() does not disable the serial monitor, only the interrupts. After calling Serial.end() you can disable the serial monitor like so.
#include <avr/io.h>
// Save status register, disable interrupts
uint8_t oldSREG = SREG;
cli();
// Disable TX and RX
cbi(UCSRB, RXEN);
cbi(UCSRB, TXEN);
// Disable RX ISR
cbi(UCSRB, RXCIE);
// Flush the internal buffer
Serial.flush();
// Restore status register
SREG = oldSREG;
I am trying to make a led controller for my room rgb led strip.
I am using arduino uno. I want two pushbuttons:
controls static colors of led (one color at a time.cycles color on pressing that same switch again)
rgb crossfade
Both programs work separately. Now I want these two programs in one sketch and toggled by two pushbuttons one for color cycle and one for crossfade
sketch works but only one pushbutton is working.
I have connected 2 10k ohm resistors with both buttons according to various guides and tutorials. I think the wiring is correct.
Here is the sketch:
const int buttonPin1 = 2; // button 1
const int buttonPin2 = 3; // button 2
const int redPin = 9; // red led pin
const int greenPin = 11; // green led pin
const int bluePin = 10; // blue led pin
int pushCounter = 0; // push counter for changing colors at each press
int buttonState = 0; // button state i.e. HIGH or LOW
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setColourRgb(0,0,0);
}
void setColor(int red, int green, int blue) // function for setting custom colors
{
//#ifdef COMMON_ANODE
//red = 255 - red;
//green = 255 - green;
//blue = 255 - blue;
//#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { //function for setting crossfade colors
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop()
{
int buttonState1 = 0;
int buttonState2 = 0;
buttonState1 = digitalRead(buttonPin1); //reads button states
buttonState2 = digitalRead(buttonPin2);
if ((buttonState1 == HIGH) && (buttonState2 == LOW)) //compares button states of both switches to enable the correct program
{
buttonState = digitalRead(buttonPin1);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) //increases pushcounter of button1 on every press
{
pushCounter++;
}
}
//delay(1);
lastButtonState = buttonState;
if (pushCounter == 1) //custom led colors
{ setColor(255, 0, 0);}; //red
if (pushCounter == 2)
{ setColor(0, 255, 0);}; //green
if (pushCounter == 3)
{ setColor(0, 0, 255);}; //blue
if (pushCounter == 4)
{ setColor(255,200,255);}; //white
if (pushCounter == 5)
{ setColor(255,255,0);}; //lime
if (pushCounter == 6)
{ setColor(0,255,255);}; //aqua
if (pushCounter == 7)
{ setColor(255,0,255);}; //violet
if (pushCounter == 8)
{ setColor(128,0,128);}; //dim_violet
if (pushCounter == 9)
{ pushCounter = 0;}
}
if ((buttonState1 == LOW) && (buttonState2 == HIGH)) //compares button states to load second program
{
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255; //sets first color
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1)
{
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1)
{
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(30);
}
}
};
}
Now if I reverse the if condition for second program... the second program runs but the first doesn't run. What is wrong here?
Since you are reading from two buttons you need to do some debouncing.
Something like you've done but a bit better :)
There is a library that does this for you and can be downloaded from Arduino web
Once downloaded Change the #include "WProgram.h" to #include "Arduino.h" in Debounce.cpp and you should be good to go.
below is the implementation of the lib with your code.
From what described the logic is right.
#include <Debounce.h>
#define buttonPin1 2 // button 1
#define buttonPin2 3 // button 2
#define redPin 9 // red led pin
#define greenPin 11 // green led pin
#define bluePin 10 // blue led pin
#define debounceTime 20 // debounce time
int pushCounter = 0; // push counter for changing colors at each press
int buttonState = 0; // button state i.e. HIGH or LOW
int lastButtonState = 0;
// Instantiate a Debounce object
Debounce button1 = Debounce(debounceTime, buttonPin1); //for button 1
Debounce button2 = Debounce(debounceTime, buttonPin2); //for button 2
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setColourRgb(0,0,0);
}
void loop()
{
//update debounce stat for both buttons
button1.update();
button2.update();
int button1State = button1.read(); //reads button states
int button2State = button2.read();
if ((button1State == 1) && (button2State == 0)) //compares button states of both switches to enable the correct program
{
//Since we are checking for the first button being HIGH or 1 we can increament the counter
pushCounter++;
/*
buttonState = button1State;
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) //increases pushcounter of button1 on every press
{
pushCounter++;
lastButtonState = buttonState;
}
}
*/
//delay(1);
switch (pushCounter)
{
case 1:
setColor(255, 0, 0); //red
break;
case 2:
setColor(0, 255, 0); //green
break;
case 3:
setColor(0, 0, 255); //blue
break;
case 4:
setColor(255,200,255); //white
break;
case 5:
setColor(255,255,0); //lime
break;
case 6:
setColor(0,255,255); //aqua
break;
case 7:
setColor(255, 0, 0); //violet
break;
case 8:
setColor(128,0,128); //dim_violet
break;
case 9:
pushCounter = 0; // reset the counter
break;
}
}
else if ((button1State == 0) && (button2State == 1)) //compares button states to load second program
{
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255; //sets first color
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour++)
{
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1)
{
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(30);
}
}
}
}
void setColor(int red, int green, int blue) // function for setting custom colors
{
//#ifdef COMMON_ANODE
//red = 255 - red;
//green = 255 - green;
//blue = 255 - blue;
//#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { //function for setting crossfade colors
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
I have made a circuit involving 3 strings of LEDs.
They are supposed to glow at a low intensity initially.
Now when pin 2 on Arduino goes HIGH momentarily, the LED strings glow brighter one by one and then stop. AND, if the pin 2 is kept HIGH, then the transition should continue as long as pin 2 is HIGH.
The problem is that if I make pin 2 HIGH permanently via a push button, the transition occurs 4 times and then it stops. No transition occurs after that even if i again make the pin 2 HIGH after some time.
The code is given below:
const int pin1 = 9;
const int pin2 = 10;
const int pin3 = 11;
const int button = 2;
int val = 0;
//int brightness = 0;
void setup(){
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
while(true)
{
val = digitalRead(button);
if(val == HIGH)
{
if(Serial.available())
{
long int brightness = Serial.read();
analogWrite(pin1, 255);
delay(1000);
analogWrite(pin2, 255);
delay(1000);
analogWrite(pin3, 255);
delay(1000);
analogWrite(pin1, brightness);
analogWrite(pin2, brightness);
analogWrite(pin3, brightness);
delay(1000);
}
}
}
}
If you really want to configure your dimmed brightness after each reset of your arduino, you should rearrange your source in a way that reading from the serial port doesn't block your transition, while still making sure your "brightness" has a predefined value:
// global variable with preset value
long int brightness = 64;
// loop function
// http://arduino.cc/en/pmwiki.php?n=Reference/Loop
void loop(){
// check for new brightness value
if(Serial.available())
{
int brightness = Serial.read();
}
// check for pressed button
val = digitalRead(button);
if(val == HIGH)
{
// start light cycle
analogWrite(pin1, 255);
delay(1000);
analogWrite(pin2, 255);
delay(1000);
analogWrite(pin3, 255);
delay(1000);
analogWrite(pin1, brightness);
analogWrite(pin2, brightness);
analogWrite(pin3, brightness);
delay(1000);
}
}
I have to make a program where a switch causes an led to change brightness each time the switch is clicked. The code below causes the LED to change brightness as needed but it does not stop changing if the switch is held down. I need it to only change once each time the switch is clicked. How do I make it change once when clicked and not loop through the other brightnesses?
const int ledPin = 10;
int brightness = 0;
boolean up = 1;
int steps = 0;
int inpin = 2;
int reading;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode (inpin, INPUT);
}
void loop() {
do{
reading = digitalRead(inpin);
}while (reading != HIGH);
analogWrite(ledPin, brightness);
delay(1000);
if (up)
{
brightness = brightness + 63;
}
else {
brightness = brightness - 63;
}
steps = steps + 1;
if (steps == 4)
{
up = !up;
steps = 0;
}
}
Once the brightness has been changed, wait for the switch to be released.