I tried to have a random fading pattern on several leds and it leads to flickering because it cannot recognise which led are already on use.
I am in the process of creating a simple lamp that show some light patterns using 6 led and an arduino uno. The goal is that depending on some inputs and a bit of randomisation, the patterns will change. For instance, only 2 lights at the same time but fading up and down randomly on all 6 led. Or 4 lights fading up and down randomly on the 6 leds.
I decided to create a struct that would be a light pattern named a particle, and this particle have a variable named led_position that shows which led it is on at a certain moment. The particle can change its position to light up an other led.
I have tried to add a data field for busy, as you can see and to update the list of available led ( the one without a busy process on it) to no avail.
'''Main code '''' -edited after bartek's comment.
#include "MyTypes.h"
int led0 = 3; // the PWM pin the LED is attached to
int led1 = 5;
int led2 = 6;
int led3 = 9;
int led4 = 10;
int led5 = 11;
//int led;
int brightness;
int fadeAmount;
// Definition of the different particles
particle_type particle[]={
{ 0,led0,0, 2, false },
{ 1,led1,50, 2, false },
{ 2,led2,100, 2, false },
{ 3,led3,80, 2, false },
{ 4,led4,30, 2, false},
{ 5,led5,70, 2, false},
};
led_type led[]={
{ 3,false },
{ 5,false },
{ 6,false },
{ 9,false },
{ 10,false },
{ 11,false },
};
//initiation of the particles
void initiate_particle(particle_type& particle_example,led_type led[]){
//take a random led from list of led
int number=random(6);
Serial.println(number);
led[number];
if (led[number].busy == false){ //if led non-occupied
for (int i=0;i<6;i++){
if (led[i].led_position==particle_example.led_position){
led[i].busy=false;
Serial.println("success");
Serial.println(led[i].led_position);
}
}
particle_example.led_position=led[number].led_position;
Serial.println(particle_example.led_position);
led[number].busy = true ;//take this led as output
}
else{
// initiate_particle( particle_example,led);
}
particle_example.reset = false; //reset turned off
delay(30);
}
void update_light(particle_type& particle_example,led_type led[]){ //updating led brightness
analogWrite(particle_example.led_position, particle_example.brightness); //updating led brightness
if (particle_example.brightness >= 160) {
particle_example.fadeAmount = -particle_example.fadeAmount;
}
if (particle_example.brightness <= 160 && particle_example.brightness >= 100 ) {
particle_example.brightness = particle_example.brightness + 2*particle_example.fadeAmount;
}
if (particle_example.brightness >= 50 && particle_example.brightness <= 100){
particle_example.brightness = particle_example.brightness + particle_example.fadeAmount;
}
if (particle_example.brightness >= 0 && particle_example.brightness <= 50){
particle_example.brightness = particle_example.brightness + 0.5*particle_example.fadeAmount;
}
if (particle_example.brightness <= 0){
particle_example.reset= true;
particle_example.brightness =0;
int number = particle_example.led_position;
particle_example.fadeAmount = -particle_example.fadeAmount;
}
}
void activate_particle(particle_type& particle_example,led_type led[]){ //function to keep led activated
if (particle_example.reset==true){ //if led not initiated or has finished its dimming down
initiate_particle(particle_example, led); //initiate it again
;}
else {
update_light(particle_example,led); //update its brightness
}
}
void setup() { //create led outputs
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
Serial.begin(9600); //serial for debug
}
void loop() {
activate_particle(particle[0],led);
activate_particle(particle[1],led);
activate_particle(particle[2],led);
// activate_particle(particle[3],led);
// activate_particle(particle[4],led);
// activate_particle(particle[5],led);
//
delay(10); // 20-17-15-12-10-7-2
}
'''Mytypes.h''''
typedef struct {
int id;
int led_position;
int brightness;
int fadeAmount;
bool reset;
} particle_type;
typedef struct {
int led_position;
bool busy;
} led_type;
The code runs and the particles go from led to led but they sometime merge into a single one and this makes the LED flickers.
The problems are almost non-existent when 1 to 3 particles are activated but become a problem starting at 4 up to 6.
How can I make sure that they don't 'take' the same led to output.
On top of that it seems that some leds are "discarded". How can I make sure that they stay active.
Related
This is my program:
#include "Arduino.h"
#include "MotorDriver.h"
#include "encoders.h"
//Define the global variables to configure the motors
//Right Motor Configuration Variables
int motR_pins[3] = {4, 15, 18}; //Define the Motor Pins
int motR_sign = -1; //Define the motor rotation sign
//Left Motor configuration variables
int motL_pins[3] = {2, 12, 0};
int motL_sign = 1;
//Encoders
Encoders Encr;
Encoders Encl;
int signal_R=-1;
int signal_L=1;
MotorDriver Mr;
MotorDriver Ml;
//Setup
void setup()
{
//Set up the Motors
//Setup the Right Motor object
Mr.SetBaseFreq(5000); //PWM base frequency setup
Mr.SetSign(motR_sign); //Setup motor sign
Mr.DriverSetup(motR_pins[0], 0, motR_pins[1], motR_pins[2]); //Setup motor pins and channel
Mr.MotorWrite(0); //Write 0 velocity to the motor when initialising
//Setup the Left Motor object
Ml.SetBaseFreq(5000);
Ml.SetSign(motL_sign);
Ml.DriverSetup(motL_pins[0], 1, motL_pins[1], motL_pins[2]);
Ml.MotorWrite(0);
//Encoder setup
Encr.EncodersSetup(34, 36);
Encl.EncodersSetup(35, 39);
//Begin Serial Communication
Serial.begin(9600);
}
long positionLeft = -999;
long positionRight = -999;
//Loop
void loop()
{
Mr.MotorWrite(-0.5); //Set Velocity percentage to the Motors (-1 to 1)
Ml.MotorWrite(0.4);
long newLeft, newRight;
newLeft = Encl.readenc(signal_L);
newRight = Encr.readenc(signal_R);
Serial.print("Left = ");
Serial.print(newLeft);
Serial.print(", Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight; //Delay before next loop iteration
}
This is my library which is supposed to read the rpm values and change them to the linear values so that I can work on PID implementation later on:
#ifndef Encoders_h
#define Encoders_h
#include <Arduino.h>
class Encoders
{
private:
int PinA;
int PinB;
float current_time=0;
int sample=10;
float ticks=1632.67;
float previous_time=0;
float pinAStateCurrent = LOW;
float pinAStateLast = LOW;
float rotation = 0;
float counter = 0;
public:
Encoders();
void EncodersSetup(int A, int B)
{
PinA=A;
PinB=B;
};
float readenc(int enc_signal)
{
pinMode(PinA, INPUT);
pinMode(PinB, INPUT);
pinAStateCurrent = digitalRead(PinA);
if ((digitalRead(PinA)) == HIGH)
{
update();
}
else
{
update();
}
current_time=millis();
if (current_time-previous_time > sample)
{
rotation = (counter*enc_signal)/ticks;
rotation = (rotation * 1000) / (current_time-previous_time);
previous_time=current_time;
counter=0;
}
pinAStateLast = pinAStateCurrent;
return rotation*0.1*3.1415;
};
void update()
{
if((pinAStateLast == LOW) && (pinAStateCurrent == HIGH))
{
if (digitalRead(PinB) == HIGH)
{
counter++;
}
else
{
counter--;
}
}
};
};
#endif
I'm getting errors which I can't understand:
sketch\task2.ino.cpp.o:(.literal.startup._GLOBAL__sub_I_motR_pins+0x4): undefined reference to `Encoders::Encoders()'
sketch\task2.ino.cpp.o: In function `_GLOBAL__sub_I_motR_pins':
C:\Users\hubno\OneDrive\Pulpit\ESP - reports\TD3\task2/task2.ino:67: undefined reference to `Encoders::Encoders()'
C:\Users\hubno\OneDrive\Pulpit\ESP - reports\TD3\task2/task2.ino:17: undefined reference to `Encoders::Encoders()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.
I can't notice anything wrong in the lines 17 and 67. The MotorDriver part should be alright since it was provided externally and it has been tested before and it proved to be working. So I guess the problem must be with my implementation of encoder library. I would be grateful for any help.
You have declared the default constructor for Encoders but not actually defined it anywhere. Either define it yourself or remove the declaration and let the compiler do it for you.
See Difference between a definition and a declaration.
You may also refer to Paul T.'s comment.
I'm on my first Arduino project... On this project, I use an RGB led and 2 Servomotors...
First off all, following OOP, I create a class to control my RGB led...
class StatusLED {
private:
int pinRed;
int pinGreen;
int pinBlue;
public:
StatusLED(int pinRed, int pinGreen, int pinBlue);
void RGB(int redValue, int greenValue, int blueValue);
};
It's working very well, without issues...
After there is all fine with the RGB led, I start to include my Servomotor code...
#include <Servo.h>
#define PIN_RGBLED_R 9
#define PIN_RGBLED_G 10
#define PIN_RGBLED_B 11
#define PIN_SERVO_H 12
#define PIN_SERVO_V 13
Servo servoH;
Servo servoV;
LED led(PIN_RGBLED_R, PIN_RGBLED_G, PIN_RGBLED_B);
void setup() {
servoH.attach(PIN_SERVO_H);
servoV.attach(PIN_SERVO_V);
}
And after I include the servo.attach() lines, my RBG led has a strange behavior, the colors that I used before, like Light Purple RGB(2, 0, 2);, doesn't work anymore, now when i try it, the led turns on with Red color.
If I comment the servo.attach() lines, the led works well.
Already tried:
Change Servo libraries version;
Change servos to another pins;
Someone can please help me?
EDIT:
Just to eliminate the doubt of my LED class are the problem, I create a new file...
#include <Servo.h>
#define PIN_SERVO_H 3
#define PIN_SERVO_V 4
#define PIN_RGBLED_R 9
#define PIN_RGBLED_G 10
#define PIN_RGBLED_B 11
Servo servoH;
Servo servoV;
void setup() {
pinMode(PIN_RGBLED_R, OUTPUT);
pinMode(PIN_RGBLED_G, OUTPUT);
pinMode(PIN_RGBLED_B, OUTPUT);
servoH.attach(PIN_SERVO_H);
servoV.attach(PIN_SERVO_V);
}
void loop() {
RGB(2,0,2);
delay(100);
RGB(4,0,4);
delay(100);
RGB(8,0,8);
delay(100);
RGB(16,0,16);
delay(100);
RGB(0,0,0);
delay(1000);
}
void 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;
}
// This is a common anode RGB Led.
// So 255 is OFF and 0 is Fully ON
analogWrite(PIN_RGBLED_R, 255 - redValue);
analogWrite(PIN_RGBLED_G, 255 - greenValue);
analogWrite(PIN_RGBLED_B, 255 - blueValue);
}
And the problem continues... if I comment the lines of attach() the led works fine, without comment, it only blinks in Red color...
Depending on the version of your arduino, servo.attach only works on Pin 9 and Pin 10, which conflicts with your RGB pins. See https://www.arduino.cc/en/Reference/ServoAttach
I'm a total C++ newbie, but I recently got an arduino and I'm doing my first project with it. I may be jumping into the deep end here, but I've got some experience with electronics already. I also have some experience with python.
I want to make a fancy interior lighting setup using two of these led strips and this arduino starter kit.
I need to make a whole bunch of presets that can be quickly activated using the IR remote included with the kit.
I need to be able to split the strips into individual zones , which I can use to easily set the presets. Maybe I want preset1 to be the ceiling lights a dark blue at 50% brightness and the window lights an orange at 25% brightness.
I know that to achieve this I want every button on the remote to call a different function. I've written some Psuedocode and I'd like some advice on the best way to learn how to implement this on an arduino.
I really need help with the software side of this project, the hardware side is all good.
disclaimer: I haven't touched coding since high school, so my psuedocode is probably trash. I just hope it's vaguely understandable
//Psuedocode attempt to make an LED thingy
// Importing needed libraries
Import fastled
Import IRremote
define num_led 300
define data_pin 5
// defining various zones. Zone 1 = Under cabinet, Zone 2 = Around window etc.
Global zone1 = num_led [0:24]
Global zone2 = num_led [24:172]
Global zone3 = num_led [173:277]
Global zone4 = num_led [278:299]
Def preset1()
for led in zone1:
ledRGB = (160, 0, 210)
for led in zone2:
ledRGB = (120, 0, 24)
for led in zone3:
ledRGB = (0, 0, 0)
for led in zone4:
ledRGB = (100,100,100)
// Make a whole bunch more of these normal presets
Def preset6()
for led in zone1:
do cool fastled animation
for led in zone2:
do different fastled animation
for led in zone3:
ledRGB = (0, 0, 0)
for led in zone4:
ledRGB = (0, 0, 0)
// Make a whole bunch more of these fancy presets
Def BrightnessUp()
i = 0
for num_led in zone1 and zone2 and zone3 and zone4:
if i in ledRGB > 0: //This is to minimise colour shift. Any RGB channel set at 0 won’t increase.
i = i+10
else:
return
// Similar for brightness down
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
{
//The following is mostly copy pasted C++ code with modifications
//It works to understand the IR remote inputs and call functions based on input
const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;
void setup(){
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
if (results.value == 0XFFFFFFFF)
results.value = key_value;
switch(results.value)
case 0xFF30CF:
Serial.println("1");
Call preset1()
break ;
case 0xFF18E7:
Serial.println("2");
Call preset2()
break ;
case 0xFF7A85:
Serial.println("3");
Call preset3()
break ;
//ETC for every number on remote
case 0xFFE01F:
Serial.println("-");
Call BrigtnessDown()
break ;
case 0xFFA857:
Serial.println("+");
Call BrightnessUp()
break ;
}
key_value = results.value;
irrecv.resume();
}
}
Steps to be followed are
Install the FastLED Library from GitHub: FastLED Lib
Install the IRremote Library from GitHub: IRremote Lib
Import it to your Arduino library folder via Arduino IDE: Importing a .zip Library
Here is a basic structure of the program which we will require.
#include "FastLED.h"
#include "IRremote.h"
#define DATA_PIN 5 // digital pin of your arduino
#define NUM_LEDS 300
CRGB leds[NUM_LEDS];
//Zones array
int zone1[2] = {0, 24}; //For zone1: Start,End
int zone2[2] = {25, 172}; //For zone2: Start,End
int zone3[2] = {173, 277}; //For zone3: Start,End
int zone4[2] = {278, 299}; //For zone4: Start,End
// For IR receiver
const int RECV_PIN = 3; // Hardware specs allows 3 or 9 pin to be used for ATmega328p
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
//Setup for FastLED
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
//Setup for IRremote
pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN is the inbuild led on Arduino UNO at Pin13
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true); // Enable blinking the LED when during reception
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
switch (results.value) {
case 0xFF30CF:
Serial.println("1");
preset1();
break;
case 0xFF18E7:
Serial.println("2");
//preset2(); //TODO: Complete this preset2
break;
case 0xFF7A85:
Serial.println("3");
//preset3(); //TODO: Complete this preset3
break;
//ETC for every number on remote
case 0xFFE01F:
Serial.println("-");
brightnessDown();
break;
case 0xFFA857:
Serial.println("+");
brightnessUp();
break;
default:
Serial.println("Not Understood.");
break;
}
irrecv.resume(); // Receive the next value
}
delay(100);
}
void preset1()
{
//Zone 1
for (int i = zone1[0]; i <= zone1[1]; i++) {
leds[i] = CRGB(160, 0, 210);
}
//Zone 2
for (int i = zone2[0]; i <= zone2[1]; i++) {
leds[i] = CRGB(120, 0, 24);
}
//Zone 3
for (int i = zone3[0]; i <= zone3[1]; i++) {
leds[i] = CRGB(0, 0, 0);
}
//Zone 4
for (int i = zone4[0]; i <= zone4[1]; i++) {
leds[i] = CRGB(100, 100, 100);
}
//To set the LEDs
FastLED.show();
}
void brightnessUp()
{
for (int i = 0; i < NUM_LEDS; i++) {
if (leds[i].r > 0)
leds[i].r = leds[i].r + 10;
if (leds[i].g > 0)
leds[i].g = leds[i].g + 10;
if (leds[i].b > 0)
leds[i].b = leds[i].b + 10;
}
//To set the LEDs
FastLED.show();
}
void brightnessDown()
{
for (int i = 0; i < NUM_LEDS; i++) {
if (leds[i].r > 10)
leds[i].r = leds[i].r - 10;
if (leds[i].g > 10)
leds[i].g = leds[i].g - 10;
if (leds[i].b > 10)
leds[i].b = leds[i].b - 10;
}
//To set the LEDs
FastLED.show();
}
Note: This code this created with the help of Pseudo-code provided and the logic implemented in it, you may have to change certain things in order for it to produce best results.
I am greeting you today with a program question for my project that uses light as an input for a servo on a wall. Light will enter the room and the shade will go down, and in the absence of light, the shade will recede. I would like to make two conditions in a void loop in the arduino code only applicable one time unless the condition changes. By saying this I mean that, I want this void loop to run continually,in which i have two conditions. And if the same condition is met twice in a row,(ie. sensor reading between 800 and 10000, like 5000 and and then at 6032), nothing will run. If one condition is met and then the other is met afterwards, that's ok. Here's my code and any help as to what reference commands I should use or my next course of action would be greatly appreciated.
// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article: http://bildr.org/2011/08/tsl230r-arduino/
#include <Servo.h>
Servo myservo1;
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 30; //higher = slower but more stable and accurate
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
if(lightLevel>800 && lightLevel<1000)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1300);delay(1000);
myservo1.writeMicroseconds(1500);delay(5000000);
}
else if(lightLevel<800)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000000);
}
}
void setupTSL230(){
pinMode(TSL230_s0, OUTPUT);
pinMode(TSL230_s1, OUTPUT);
//configure sensitivity - Can set to
//S1 LOW | S0 HIGH: low
//S1 HIGH | S0 LOW: med
//S1 HIGH | S0 HIGH: high
digitalWrite(TSL230_s1, LOW);
digitalWrite(TSL230_s0, HIGH);
}
float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter
float start = micros();
int readings = 0;
while(readings < samples){
pulseIn(TSL230_Pin, HIGH);
readings ++;
}
float length = micros() - start;
float freq = (1000000 / (length / samples)) * 10;
return freq;
}
First add this to your setup ...
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
myservo1.attach(5);
}
Then make a new variable and add it to your if statements
Boolean once; // declare this with your other int variables
if(lightLevel > 800 && lightLevel < 1000 && once==True)
{
myservo1.writeMicroseconds(1300);delay(1000);
myservo1.writeMicroseconds(1500);delay(1000);
once = False;
}
else if(lightLevel<800 && once == False)
{
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(1000);
once = True;
}
I am trying to test out Fade on multiple LEDs on an Arduino Uno. Here's the code i have written
int led[2] = {9,10}; // the pin that the LED is attached to
int brightness[2] = {0,0}; // how bright the LED is
int fadeAmount[2] = {5,15}; // how many points to fade the LED by
long previousMillis[2] = {0,0}; // will store last time LED was updated
long interval[2] = {30, 50};
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led[0], OUTPUT);
pinMode(led[1], OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pins:
for (int counter = 0; counter < 2; counter++) {
unsigned long currentMillis = millis();
analogWrite(led[counter], brightness[counter]);
Serial.print("LED ");
Serial.print(led[counter]);
Serial.print(": Brightness ");
Serial.println(brightness[counter]);
if (currentMillis - previousMillis[counter] > interval[counter]) {
// change the brightness for next time through the loop:
brightness[counter] = brightness[counter] + fadeAmount[counter];
// reverse the direction of the fading at the ends of the fade:
if (brightness[counter] == 0 || brightness[counter] == 255) {
fadeAmount[counter] = -fadeAmount[counter] ;
}
}
}
}
Here's the weird thing. If i comment out the Serial stuff (print and begin), the fades don't work. They just "flicker" a little.
Any idea what is wrong?
I figured it out. It looks like I forgot a line of code.
previousMillis[counter] = currentMillis;
I should have put that in on the first if statement in the code. I guess the act of observing by using the serial print slowed things down enough that the code seemed to work.
Boy, do i feel foolish.