Notifications
Clear all

Theatre chase in opposite directions.

155 Posts
6 Users
2 Likes
11.2 K Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2506
 
Posted by: @davy-ps

@will 

Fair comment Mate. How do I go about indenting the code? I have been using the 'Copy for Forum" function in Arduino, but that obviously doesn't transpose to this platform.

@robotBuilder gave some instructions a few pages back ...

You can make your code nicer if you are using the Arduino IDE.

In the IDE click text with right mouse button and Select All and save as HTML

In your post make a few CRLF with the enter key where you want to insert the code.

Select the {;} in the top bar of your post.

Insert between CRLF control codes.

The CRLF he's talking about refers to the appearance of the post after you click the "{;}" icon at the top. The  post will, at that point, be displayed in html and blank lines will be shown as <p></p> pairs. When you hit the carriage return a couple of times BEFORE you select the "{;}" icon, when you enter the html source, you'll find a couple of sequential lines with only <p></p> showing.

You want to position the cursor between those two lines and then paste the code from the Arduino. That will result in code formatted the way @robotBuilder's code is shown. Neatly indented and properly coloured.

As for the bloated format, it's what I can understand at this stage of my learning curve. 😉

Understood, but way back in davy3, I introduced a subroutine blinkIt which collects all of the code for each  lump of code in your sketch to a single line code.

Now think about it for a moment. Can you see that if every blink event can be properly handled by the blinkIt subroutine and all the rest of the sketch has to do is call it with the proper parameters (led index, 3 colour components and the delay time in ms) then you'd only have that ONE 7 line segment of code to write and debug and every blink event produced by it would be perfect.

All you'd need to do is verify that you're sending it the right parameters. If you'd converted the lumps to calls of that routine your original program would have worked (because all of the blinks would have the second call to FastLED.show();

Also, you'd be removing pages of error-prone code. If you were volunteering your time and somebody asked you to look through an acre of paper to help them find a bug, you'd probably move on  to the next article 🙂 Wouldn't you rather look at one page of single-line calls to a subroutine than the same number of 6-line code lumps, each of which may have one or more errors ?

I'll glean knowledge in bite size chunks and apply them as I progress. EG a random snip for (int j = 0; j < 18; j++) No idea what the hell this means, but Lights On /Lights off I can grasp.

Agreed, but unless you expect robotBuilder and I to write all your software, you'll need to take a more hands-on approach. That's why we're showing you different ways to do the same thing, you're supposed to be learning to think about coding as we go. We are expecting to help you as you learn.

Some things you can look up for yourself, like Googling "Arduino for statement" to see what it is and how it works. The above says start value j off at zero and proceeding as long as j is less than 18, do whatever commands are in the loop. When you're done that, use the next number (j is now replaced by the value j+1) and do it again as long as the new j is still less than 18.

You will also need to learn to be neat (or at least precise). Using a computer is like having an extremely fast but stupid friend who'll do whatever you tell it to do, whether that's what you meant it to do or not. For instance, you say that you added the code from rb2 to the sketch you posted, but that's not true.

Your posted sketch shows NUM_LEDS to be 11, whereas rb2 defines NUM_LEDS as 10. So you have already introduced an error by being sloppy in just the 4th line of the sketch ! Any computer, not just the Arduino will crucify you for even small mistakes, you'll have to take more care working on code. These are mistakes made by everybody as they learn to program, so learn from them (or you'll be doomed to repeat them).

Lastly, I don't know how you transferred the code into the sketch above, but I suspect that you've oopsed some more of the rb2 code during the move, so I'd suggest that you scan it line by line comparing the code in rb2 with the front part of the posted sketch. Learn to debug.

If that doesn't work, then swap out the code to use the blinkIt routine for the other blink events and post the new code for us to look at.

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 Mate,

                                You sent these instructions to me.

You can make your code nicer if you are using the Arduino IDE.

In the IDE click text with right mouse button and Select All and save as HTML

  • So, using Select All and right clicking again there is only a "Copy as HTML" command. Not Save. Is this what you mean?

In your post make a few CRLF with the enter key where you want to insert the code.

Select the {;} in the top bar of your post.

Insert between CRLF control codes.

  • Can you explain this process? IE Define a CRLF in this context.  I'm assuming CRLF ...Code....{;}

Thanks for your help. It's always a pain in the neck trying to impart information and making it stick.

 

Cheers,

Davy.


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

@will Thanks Mate. I will withdraw into my cave and spend the next few days going over the codes you guys have so kindly provided and try and make sense of them.  The internet is a fantastic tool, but too many times when you ask a question you finish up with 20 others 😉

 

Thanks again.

Davy.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2506
 
Posted by: @davy-ps

@will Thanks Mate. I will withdraw into my cave and spend the next few days going over the codes you guys have so kindly provided and try and make sense of them.  The internet is a fantastic tool, but too many times when you ask a question you finish up with 20 others 😉

If there's something you can't look up or can't understand after you've looked it up, then by all means ask here in the forum.

Experiment 🙂

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


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

@davy-ps 

The reason I wrote crlf was because when I tried in the past to type what you actually see the post would treat them as an actual action because they are control codes.  Somehow @will didn't have that problem. So forget about the crlf.

In the Arduino IDE click the text with the right mouse button and Select All and again click the text with the right mouse button and select save as HTML.

In your forum post place the cursor where you want to insert the code and hit the enter key a few times.

Select the {;} in the top bar of your forum post.

The post will, at that point, be displayed in html and blank lines will be shown as

pairs. 

Now see it has done it again. The P's have gone!  How did you do it @will ?

With the mouse right button click between those pairs and select paste. It will paste at that position in your forum post the code you selected in the Arduino IDE and copied as html.

If it doesn't work for some reason just delete the forum post and try again.

 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 
Posted by: @davy-ps

@will Thanks Mate. I will withdraw into my cave and spend the next few days going over the codes you guys have so kindly provided and try and make sense of them.

You have to walk before you can run.  I started with simple code and circuits to read a button and turn on a LED.  Then I added 8 buttons and 8 LEDS and played with writing code to do neat stuff with that circuit. That way you can learn to code with understanding. One step at a time. You can make the LEDS turn on/off in different sequences. No need for those fastLED libraries which you use without understanding. I must get some more RGB LEDS and play with those as well.  Later I played with stepper motors and other sensors. (A button is a simple "sensor").

Really, you love this stuff or you don't.  Often we would like to be able to do something but aren't really interested enough to put the time and effort into learning how to do it.

experimentalSetup

 


   
ReplyQuote
Melbul
(@melbul)
Member
Joined: 3 years ago
Posts: 43
 

@davy-ps 

If you want to learn a bit more about the coding, take a look at Paul McWhorters youTube channel or his website TopTechBoy.com he explains most of the basics in his Arduino series 🙂

regards,

mel.


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

@melbul

Thanks Mel. Just watched first lesson. Great having Guys like yourself and Will and robotBuilder giving me direction. I initially had what I thought was a simple project in mind and am now holding a tiger by the tail. 😉

 

Cheers Mate,

 

Davy.


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

@will

@will Hi Will,

                   I visited the "Arduino for statement" page as you suggested. There was a small code  at bottom of page which fades an LED in and  out. It used a PWM pin in the analogWrite command which I changed to blueLED. The sketch works. I added two more LED's on separate pins. What happened was the blue LED faded in and out but the red and green LED'S simply went HIGH. I messed about with the int in void loop but to no avail. My question is, what part of the void loop would I change to apply the fading process to the three LED's.

 

Cheers Mate,

 

Davy.

 

int blueLED = 9;
void setup() {
// put your setup code here, to run once:
pinMode(blueLED, OUTPUT);
}
void loop() {
int x = 1;
for (int i = 0; i > -1; i = i + x) {
analogWrite(blueLED, i);
if (i == 255) {
x = -1; // switch direction at peak
}
delay(10);
}
}

 

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2506
 

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  

@willin 4 doesn't work either 

 

Ha! Working now. I had it set up the way you did with three pinModes but was using pins 7, 8, and 9. Incidentally, Pin 4 doesn't work either ,(stays High) so changed to pin 5. Did not realise significance of PWM for fading function.

So pins 3,5,6,9,10 and 11 are PWM.

I understand now that the loop is the loop and will act on the information supplied in the set up.

Thanks again. 

Davy.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2506
 

@davy-ps 

Aw ... I screwed up on pin 4; bad programmer - NO BISCUIT 🙂

Yeah, the other digital pins are either HIGH or LOW, only those marked ~ can supply the pulsed PWM power.

 

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 

Will further to my question. I f you have time can you look at questions I have attached to code?

Thanks Mate.

Davy.

 

void loop() {
int x = 1; //What is significance of "int x = 1;How is this derived?
for (int i = 0; i > -1; i = i + x) { // Likewise for this line. Where does "i" come from?
analogWrite(blueLED, i);
if (i == 255) {
x = -1; /switch direction at peak// Assuming here that when maximum brightness is reached
} // it drops off again.
delay(10);
}
}

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2506
 

@davy-ps 

int x = 1; //What is significance of "int x = 1;How is this derived?

This is telling the compiler that you want to create a variable named x which will contain an int (16 bit integer) value and that you wish to initialize the variable x with the value of 1. It's not really derived from anything, it's something that you are creating for your own use later in the sketch.

---

for (int i = 0; i > -1; i = i + x) { // Likewise for this line. Where does "i" come from?

You have chosen the variable name "i". Traditionally, programmers have used letters I,j,k,l, ... n to indicate integer values and,y,z etc for real (floating point) numbers. That is numbers which may have an integer and decimal part (like pi = 3.14159265...).

This statement tells the compiler that you wish to execute the following code with vales starting at 0 (the i=0) on the condition that i is greater than -1 (>-1) and each time the code segment is executed, you wish to replace the current value of i with the value of i+x (i=i+x). You can then use the current value i in the code group as you need.

The compiler will then execute the code group which consists of the next LINE of code, or the next set of code that is enclosed by a "{" and "}" pair.

The loop will generally execute until the condition (i>-1)  is no longer true - which may be never, so be careful specifying conditions for looping commands like for (...), while (...) and until (...).

This command lets you tell the compiler that you wish to execute some set of code from a specified starting value to a specified end condition with a specified progression of values.

---

if (i==255)

   x = -1;

This tells the computer that if, on this pass through the for loop, the current value of i is exactly 255 then you want to reset the value of x to -1. You'll note that this now changes the way the new for loop variable is generated. Before this, the next value of i was i+1 (i+x where x was 1) but now the next value of i will be i-1 (i+x where x is now -1).

This will now cause the loop to run backwards through the power levels from 254 down to 0 and will stop there, since 0-1 = -1 and that no longer satisfies the condition i>-1.

The effect is clearly to ramp the LED intensity up through the complete range of the PWM levels and then ramp back down to 'off'.

---

As an exercise to the reader, what would happen if you were to add the commands

if (i==0)

   x = 1;

before the delay(10) command ?

--

Try it and see if you're right.

 

Is this helping ?

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


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

   
ReplyQuote
Page 5 / 11