noob here!

Hey everybody! I’m new to programming and fairly new to electronics, but I’m learning quick!

I took a trip to local Barnes and Noble today and picked up a book called, Arduino Cookbook, by Michael Margolis. It’s a thick book and is full of excellent information for setting up and programming your Arduino.

While reading I came across this code:

// blink an LED the number of times givein in the count parameter
void blink2(int count)
{
while(count > 0) //repeat until count is no longer greather than zero
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
count = count -1; //decrement count

}
}

That gives me a compiler error (no surprise to me)
core.a(main.cpp.o): In function main': C:\Users\Steve\Desktop\arduino-0022\arduino-0022\hardware\arduino\cores\arduino/main.cpp:7: undefined reference to setup’
C:\Users\Steve\Desktop\arduino-0022\arduino-0022\hardware\arduino\cores\arduino/main.cpp:10: undefined reference to `loop’

So I started to tinker with the code, and I got to work… But then I wanted to do a “count up” and “count down”,

So I changed the code so that when the count is going in one direction the LED blinks faster and then slower when going in the other direction. This bit of code does that…

int zero = 0;
int five = 5;

void setup(){
pinMode(13, OUTPUT);
}

void loop(){

while(zero < five)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(300);
zero = zero +1;
}
while(five = five)
{
digitalWrite(13, HIGH);
delay(1500);
digitalWrite(13, LOW);
delay(150);
five = five -1;
}
}

However, I want the count to go from it’s minimum of zero to five, then back down to zero at a different LED flashing speed, then back up to five at it’s faster speed, then back to zero, just a continual loop of zero to five at one flashing speed, five to zero at a slower flashing speed, then back up, then back down, etc.

Since I am new to all of this I am not even sure if my above code is efficient or even correct. It gives me the result of going up, then back down, but then it stops all together, as in it won’t keep looping as I would want/expect. I tried all different sorts of control structures but no luck.

What is the proper way to do this? I am just doing this to test and learn, I have no end use for it. My actual goal is to setup an environmental control system using the Arduino to act as a thermostat, control timing for 5 devices, and a CO2 controller through use of a CO2meter.com sensor. It’s for my Uncles greenhouse, since he is often away he wants to be able to do data logging and web-interface and so I am working on the basic control system first and then later I can figure out the web-interface and data logging.

Any help is very much appreciated it. I’m not sure why, but when I tried to do a forum copy on that second bit of code, it just came up with a ton of java errors in the IDE and wouldn’t let me copy it that way.

P.S. I’m also not looking for “answers”, rather I want to learn how be proficient in programming so that I can build and expand on my design(s) as I learn.

KE7GKP:
Your problem is here: while(five = five)
That means the second part of your code will run forever (since it is ALWAYS true, by definition)

wrong, he used =, not ==, see http://codepad.org/IXU0rEgS

Can someone just give me some direct help here? I've been toying with this for the past hour now lol. I'm managing to get really close, but can't quite reach the solution :(

your code will not execute after loop is finished, since five will equal 0 and 0 is equal to 0, also the condition five = five will return 0 which is a boolean false

why not just use two for-loops? one to count up from 1 to 5 while blinking at one speed, and then another one after that blinks at a different speed?

Why would I do this or do something else? Because I'm a noob and I'm trying to figure out something through trial and error because that is the best way that I learn lol. I am trying to look at the code and write it so it makes sense and it is logical, just that since I am new to this, I'm not "seeing" it properly. I'm going to step away from this for the night and get some sleep, I'm way burned out. I'll try this again in the morning and hopefully by using the advice you guys are giving me I will be able to make better sense of this.

Thanks!

frank26080115:
your code will not execute after loop is finished, since five will equal 0 and 0 is equal to 0, also the condition five = five will return 0 which is a boolean false

why not just use two for-loops? one to count up from 1 to 5 while blinking at one speed, and then another one after that blinks at a different speed?

I’m going to try this approach tomorrow. I know that once I figure this out and see the simplicity I will smack myself lol :slight_smile:

while(five = five)

Aren’t you troubled by this? You may as well say “while a cat is a cat”. And that’s putting aside the fact that to compare you use == rather than =.

five = five -1;

And this? Surely five - 1 is four?

If you write code like that you could confuse the greatest programmers in the world … and yourself. Try to write clearly and think about what you are putting on paper.

Another problem is that your variables “zero” and “five”, apart from their confusing names, are never reset. So next time through the loop “zero” will actually be 5. And “five” will be 0. My head hurts even thinking about it.

What is the proper way to do this?

void setup ()
{
  pinMode (13, OUTPUT);
}  // end of setup

const int iterations = 5;  // how many times to loop

void loop(){
  int count;  // for counting stuff

  for (count = 0; count < iterations; count++)
  {
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(300);
  }  // end of for 


  for (count = 0; count < iterations; count++)
  {
    digitalWrite(13, HIGH);
    delay(1500);
    digitalWrite(13, LOW);
    delay(150);
  } // end of for 

}  // end of loop

lol Nick, be easy on the me :slight_smile:

Thanks for the help, I realize that my code was total BS, but like I said, I’m 100% new to this and that is the first bit that I tried to ‘create’ on my own.

Nick, now that I see how you did it, that is the way I “thought” of doing it, but wasn’t sure how. It’s like learning a new language, I know some words, but I don’t know how to construct
them into a coherent sentence just yet.

When you say ‘const int iterations = 5’, that means that you are setting the word ‘iterations’ as a constant numerical (integer) value of 5, right?

And ‘count’ always sets the count back to 0 for each ‘for-loop’, right?

Can you guys recommend any articles online or a specific book that might help me get the fundamentals? I have the Arduino Cookbook, and it’s a pretty good book, but is there anything else specifically that will help teach me the foundation of programming the Arduino? I seem to get lost not on the “how” this works, but more on understanding the when and why you use certain commands, etc. (I’m currently reading through the tutorial I found on ladyada.net)

Thanks so much!

** I’m going to put this in for my clarification, as I think I’m starting to gain more understanding of the code that you posted, Nick.

for (count = 0; count < iterations; count++) \ This means that each time the loop comes to the ‘for’ statement, that the count is reset to 0 (which is why the loop function continuously loops), when the ‘count’ is less than the ‘iterations’ which we set as a constant value of ‘5’, add (count++) till the count is = to the set number of iterations.

Did I say that right?

So then, just as a test to clarify my understanding I went and did something confusing, or stupid as you guys might say lol…

int zero;
for (zero = 0; zero < iterations; zero++)

I ran that and it still works just as count does, but the reason we don’t do this is because it’s confusing… zero does = 0, no duh lol, and now I see why that is a dumbass idea. Besides, what is zero++? 0++ is still 0, so it makes no sense… I see the error of my ways now.

What I’m still wondering about, and tell me if I have this right, is you set ‘const(constant) int(integer) iterations = 5’ because the iterations are not a variable, but rather a fixed value and must remain a fixed value. But, count is made to be ‘int(integer) count;’ because ‘count’ is a variable, not a constant… Am I understanding that part correctly?

Furthermore, I understand now that when you set a ‘const’(constant) it has to be a numerical value (that does not change)… Whereas, ‘count’ can be a word because it is just telling the program that anytime it comes across ‘count’ that it is a variable and to add 1 until count is = to ‘iterations’…

The reason that count can’t be done the same as iterations, e.g. const int count = 0; is because that would fix ‘count’ as a 0 and not allow it to be a variable…?

Is this right? Or am I flawed somewhere in my thinking?

I really do appreciate you guys helping me out, the best way I learn is by iterating what I think is right, which is why I typed this all up the way I did.

FlyingSteve:
When you say ‘const int iterations = 5’, that means that you are setting the word ‘iterations’ as a constant numerical (integer) value of 5, right?

Yes. I initially had it as:

 for (count = 0; count < 5; count++)
  {
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(300);
  }  // end of for

That is clearer “locally” but makes it harder later on to change if you decide you really want 10 flashes rather than 5. Particularly as you have the number 5 in two places.

Can you guys recommend any articles online or a specific book that might help me get the fundamentals?

I would Google “C tutorial” and then follow links until you get one in the style you personally prefer (or even “C++ tutorial” as strictly speaking, your sketches are in C++ and not C). However for now probably C is fine, as C++ is a superset of C, and if you tackle C++ too early they may throw a lot of stuff about classes and things at you.

A lot of tutorials will assume you have a PC (rather than an Arduino) but you can either adapt the examples, or simply install Cygwin and play on your PC to get the fundamentals right. As an example, instead of:

printf ("Hello world");

you might have to use:

Serial.print ("Hello world");

Good idea to summarize what you have learned. I often do that to make sure I get things right.