Notifications
Clear all

Theatre chase in opposite directions.

155 Posts
6 Users
2 Likes
12 K Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder FYIΒ @davy-ps

I remember when I first started teaching myself programming it took me a while to "get" for/next loops.

for (initialization, condition, update) // set up for loop

Full example not sure if it will work...

int greenLED = 4;

I tried that as well, but @davy-ps pointed out that it's not a PWM pin, so pin 5 is needed.

Β Β Β // THIS BLOCK OF CODE WILL START WITH i = 1

No, actually it will start with i=0 πŸ™‚

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Davy PS
(@davy-ps)
Member
Joined: 2 years ago
Posts: 74
Topic starter  

@willΒ 

Yup. This is helping no end. I will sit and go through each bit.

Β Regarding the exercise.....With the added commands the LED's cycle through from dimmest to brightest and then straight back to dimmest instead of gradually. IE only cycles one way.

Β 

Β 

Thanks Mate. Will spend time getting my head around this.

Β 

Davy.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@willΒ 

You certainly have an eye for detail.

I didn't even look at the pin numbers assuming they must be correct and if not easily fixed.

Another exercise might be to write a program to go through every possible colour using 3 nested loops.

Another might be to add a 3 colour sensor to control the rgb led colour.Β  Hold say a red object near the sensor and the LED will turn red. Hold say a blue object near the sensor and the LED will turn blue.

Interactive light sculpture.

Β 

Β 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder

@willΒ 

You certainly have an eye for detail.

Yes, I've already made all the mistakes there are, so I know what to look for πŸ™‚

Another exercise might be to write a program to go through every possible colour using 3 nested loops.

Yes, as long as he doesn't try to blink 'em. At 100ms on and 100 ms off, it would take a bit over 38 days to show them all.

Another might be to add a 3 colour sensor to control the rgb led colour.Β  Hold say a red object near the sensor and the LED will turn red. Hold say a blue object near the sensor and the LED will turn blue.

Interactive light sculpture.

Or even using 3 potentiometers, one each for red, green and blue.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Davy PS
(@davy-ps)
Member
Joined: 2 years ago
Posts: 74
Topic starter  

@robotbuilderΒ 

Hi robotBuilder. I have done a couple of online lessons regarding loops. Been trying to apply my learnings to existing sketches. Some things making sense, some not. The sketch you sent me {attached} runs with two different colours. Blue and Green. In the void loop it states:

leds[i] = colourList[ patterns[j][i] ];

Is this how the colours are selected and if so how do I add separate patterns? IE. Other colours.

Β 

PSΒ 

I thought about the comment you made in passing about the LEDs bouncing off each other and modified the patterns accordingly. Worked well.Β  Even blind squirrels find nuts now and again. ;-).

Β 

Β 

#include <FastLED.h>

#define LED_PIN 4
#define NUM_LEDS 10
#define NUM_COLOURS 6
#define NUM_REPEATS 1
// Define colours used
//CRGB colourOn = CRGB(255, 0, 0), colourBack;
//CRGB colourOff = CRGB(0, 0, 0);
CRGB colourList[6] = {CRGB(0,0,0),CRGB(255,0,0),CRGB(0,0,255),CRGB(255, 0, 0),CRGB(0,255, 0),CRGB(0,0,255),};
CRGB leds[NUM_LEDS];
// Define times for colour on and colour off in millis
int lengthOn=10, lengthOff = 10;

int patterns[18][10] = {{1,0,0,0,0,0,0,0,0,2},
{0,1,0,0,0,0,0,0,2,0},
{0,0,1,0,0,0,0,2,0,0},
{0,0,0,1,0,0,2,0,0,0},
{0,0,0,0,1,2,0,0,0,0},
{0,0,0,0,2,1,0,0,0,0},
{0,0,0,2,0,0,1,0,0,0},
{0,0,2,0,0,0,0,1,0,0},
{0,2,0,0,0,0,0,0,1,0},
{2,0,0,0,0,0,0,0,0,1},
{0,2,0,0,0,0,0,0,1,0},
{0,0,2,0,0,0,0,1,0,0},
{0,0,0,2,0,0,1,0,0,0},
{0,0,0,0,2,1,0,0,0,0},
{0,0,0,0,1,2,0,0,0,0},
{0,0,0,1,0,0,2,0,0,0},
{0,0,1,0,0,0,0,2,0,0},
{0,1,0,0,0,0,0,0,2,0}};


void setup() {
Serial.begin(9600);
// Setup FastLED object
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(100);
for (int i = 0; i < NUM_LEDS; ++i) {
leds[i] = colourList[0];
}
}

void loop() {

for (int j = 0; j < 18; j++)
{
// set color for every LED
for (int i = 0; i < 10; i++){
leds[i] = colourList[ patterns[j][i] ];
}
FastLED.show();
delay(1000);
}
}

Β 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@davy-psΒ 

I haven't replied straight away because I intend to explain it very clearly with illustrations rather than long winded verbal descriptions so you should understand exactly how the program works and how to modify it.Β  Might take a couple of hours.

Β 


   
ReplyQuote
Davy PS
(@davy-ps)
Member
Joined: 2 years ago
Posts: 74
Topic starter  

robotBuilder. Thanks Mate. Really appreciate your help.

Β 

Davy.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@davy-psΒ 

The method @will used was an algorithm for generating the desired sequence of patterns and is probably the preferred method when the displays become more exotic.Β  However you have to be clever enough to work out the algorithm to produce the displays you want. With a few lines of code and some mathematics some incredible images can be produced on a computer screen.

Β 


   
ReplyQuote
Davy PS
(@davy-ps)
Member
Joined: 2 years ago
Posts: 74
Topic starter  

@robotbuilderΒ 

  • Mate. What an effort. Thanks so much. I will spend tonight and tomorrow and try and make head and tail of it. Two quick ones.........ReΒ  CRGB, when you say insert that value into leds[0], where is that? I can see the colour list on the sketch is copied verbatim from the patterns chart you sent but nothing associated with the 10 leds on the chart.
  • I downloaded the sketch you sent. When I compiled it I had to remove the curly bracket in front of the semi colon at the end of the colour list. In the void set-up under Serial.begin where it says Setup FastLED object, FastLED was highlighted orange but wasn't accepted by the compiler. Assumed it was a comment and added two forward slashes. Next bit I'm stuck at. Tried a few different things but error message persists. Please see attached snip.
  • Thanks again for you help.

Davy.

image

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@davy-psΒ 

I don't have the eagle eye of @will to spot my errors πŸ™‚Β  So I need to run the program to find them.

I don't have the hardware or FastLED library to actually compile the code I just copied the code you provided.
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
A quick look online perhaps it should be,
FastLED.addLeds<WS2812, LED_PIN>(leds, NUM_LEDS);

Finding errors is part of programming although the best programmers don't make many errors.

when you say insert that value into leds[0], where is that?

I was being the computer and "rolling out" the for loop which is exactly what the computer does when it executes the statement Serial.print( led[i] ); 10 times but with a different value of i.

The statement Serial.print( led[i] ); within the curly brackets is repeated 10 times but each time the value of i changes.

The computer sees the variable i and looks up its value and substitutes that in the statement.

The first time it reads leds[i] Β as leds[0] because i=0

The second time it reads leds[i] Β as leds[1] because now i=1

and so on until i=10 and it exits the for loop and carries on with the next instruction.

I was just being the computer doing what it would be doing if it were reading the code.

for (i = 0; i<10; i++)
{
Serial.print( led[i] );Β  // this is done 10 times but i changes each time thus references a different item in the led list
}

is the same thing as writing

Serial.print( led[0] );
Serial.print( led[1] );
Serial.print( led[2] );
Serial.print( led[3] );
Serial.print( led[4] );
Serial.print( led[5] );
Serial.print( led[6] );
Serial.print( led[7] );
Serial.print( led[8] );
Serial.print( led[9] );

Say the variable is stored as value in a counter.

Here are the instructions to place an apple in boxes numbered 0 to 9:

1. set counter value to zero

2. place apple in a box with the same number as shown on the counter

3. add one to the counter

4. If the counter is less than 10 then go to instruction 2.Β 

5.Β  ....

So instruction number 2 is repeated 10 times but with a different action because the counter value is different each time.

If you want to better grasp programming it would be worth your while to learn the basics so you know what variables are and how they are used in things like loops.

http://engineering.nyu.edu/gk12/amps-cbri/pdf/ArduinoBooks/Arduino%20Programming%20Notebook.pdf

Β 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

   
Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder

@davy-psΒ 

This is the first part of the explanation, will wait your comments.

Reference the image below while reading the description.Β  You could print it out to make it easier.

So let us start with the first pattern in the patterns array which is the top row of index numbers.

The first number at patterns(0,0) = 5 so we look at colourList[5] and find it is CRGB(255,255,0) which is the color yellow. We copy that value into leds[0]
The second number at patterns(1,0) = 5 so we look at colourList[5] and find it is CRGB(255,255,0) which is the color yellow. We copy that value into leds[1].
The third number at patterns(2,0) = so we look at colourList[8] and find it is CRGB(192,192,192) which is the color silver. We copy that value into leds[2].
...
...
...
The last number at patterns(9,0) = 15 so we look at colourList[15] and find it is CRGB(0,0,128) which is the color navy. We copy that value into leds[9]

So now we have all the colour values for the first pattern copied into the leds list.
We execute the instruction FastLEDS.show() which means give all the 10 leds the colors in the leds list.
We delay a while before getting the next pattern which is row 1 in the patterns array.

We repeat with the next row until all the patterns have been shown and then start all over again.

To enlarge image right mouse click image and choose Open link in new window.

ledLights

Β 

Here is a compilable and runnable version of the sketch that was included in the original post ...

Β 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder

@davy-psΒ 

The method @will used was an algorithm for generating the desired sequence of patterns and is probably the preferred method when the displays become more exotic.

And especially larger numbers of LEDs. You can see that storing a matrix of LED colours for each row would become unusable with a full 5 m (300 LED) light strip. it would need to be 300 columns across and 599 rows long.

Also, note that the method requires a significant amount of work to retool it for using 11 LEDs instead of 10, but this solution's presentation it is MUCH easier for someone new to programming to understand πŸ™‚

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Page 6 / 11