blinking LED's in series, trying it in c way

Hello everybody,

just read a book about c language and trying to sharpen those newley found skills in practice. (pardon my english becouse it isnt my first language).

So ive put some leds and resistors in series on a "protoboard" and the positive terminals on the pins from 2 to 12 .

Anyway ive been trying to implement a c code in the arduino ide, cuz i dont wanna copy paste digitalWrite etc a million times...So im posting my code here and if anyone can help me where did i go wrong in my c logic.

long long counter = 1000;
int main () {
 int ledPin; //maybe i should have picked only one, tried several versions and this logic worked in my head the most
 int oldVal;
 for( oldVal=2; oldVal <= 12; oldVal++)  {
   ledPin=oldVal;
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);
   
  while(counter < 1000000) {     //just a manual delay funct cuz the original one did not work at all
   counter++;
   }
  
   }
 return 0;
 }

So basiclly my code for now Works this way;

  1. Led on the pin2 turns on
  2. After few seconds all of them(12 of them) turn on simultaneously

I want my code to turn one led on, than wait a certain period of thime than turn the second one, and the third and etc.

Post your code using code tags please :slight_smile:

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom... :slight_smile:

Where is your "setup()" and "loop()" methods? You should not use "main()" on an arduino..

Danois90:
You should not use "main()" on an arduino..

You can, although I can see no advantage in this case. If you do though, you need to call init() to setup the hardware before you start trying to use it. Might explain the behavior the OP is seeing.

almirlalicic:
Hello everybody,

just read a book about c language and trying to sharpen those newley found skills in practice. (pardon my english becouse it isnt my first language).

So ive put some leds and resistors in series on a "protoboard" and the positive terminals on the pins from 2 to 12 .

Anyway ive been trying to implement a c code in the arduino ide, cuz i dont wanna copy paste digitalWrite etc a million times...So im posting my code here and if anyone can help me where did i go wrong in my c logic.

long long counter = 1000;

int main () {
  int ledPin; //maybe i should have picked only one, tried several versions and this logic worked in my head the most
  int oldVal;
  for( oldVal=2; oldVal <= 12; oldVal++)  {
    ledPin=oldVal;
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
   
  while(counter < 1000000) {    //just a manual delay funct cuz the original one did not work at all
    counter++;
    }
 
    }
  return 0;
  }





So basiclly my code for now Works this way;
1. Led on the pin2 turns on
2. After few seconds all of them(12 of them) turn on simultaneously

I want my code to turn one led on, than wait a certain period of thime than turn the second one, and the third and etc.

Your code should turn each led off after the ... count loop delay and before the next is turned on? Because how else will it change?

And your counter gets from 0 to 1000000 for the first led and keeps ++ for the rest. If you use loop() then you can declare counter inside of loop() it will be a temporary variable that is made only for one run through loop() that ends when loop() does, it would not need resetting.

And since counter is declared as an int (16 bit signed on Arduino), it can't count to 1000000.

Did that book teach you about arrays?

I went to link to the page that shows simply how Arduino sketches are made (with setup() and loop() not main()) and I come back feeling that Arduino crew are Microsoft trained to make documentation ever more USELESS.

Hi,

i have an intermediate understanding in electronics, and semi beginner at arduino. Im trying to learn c through arduino but using things like leds as output not printf. Maybe im wrong in trying to mesh these two things togther, dunno.

I didnt use a setup and loop becouse it seemed redundant to me to make a loop function if im already trying to use a for loop. And cant use pinMode in setup becouse i want to increment a pinMode variable by one every time it passes true through a loop...

It Works like this: The first led turns on, waits a few seconds than every led connected turns on simultaniously. I was hoping, because it increments the variable in the pinMode(oldVal, OUTPUT) it Will keep only one led ON at any moment because the code only specifys only one pinMode output at a time..

Go for smoke u are a genious. You gave me an idea and i added :

while(counter < 1000000) {     //just a manual delay funct cuz the original one did not work at all
    counter++;
    }
    counter=0; //reset which i added <-------------------------
    }
  return 0;
  }

Probably should have just use a constant long long "variable" but still..

Now my delay actually works:).It turnes one led on, waits a period of time than the other,waits a period of time, than the other etc till i end up at the last led.

Still dosent work as it should or should i say as i want it to. But its a starter.
Still i dont understand if i increment the pinMode(oldVal, OUTPUT) why are all the leds still on or i should say the led before dosent lose the OUTPUT status.

And my semi product 12 leds that turn in series one after another from pin 2 to 12 than turn of from 12 to 2 and do that forever...

long long counter = 1000;
int main () {
  int ledPin; //maybe i should have picked only one, tried several versions and this logic worked in my head the most
  int oldVal;
  while(1) {
  for( oldVal=2; oldVal <= 12; oldVal++)  {
    ledPin=oldVal;
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
    
   while(counter < 100000) {     //just a manual delay funct cuz the original one did not work at all
    counter++;
    }
    counter=0; //reset which i added
    }
  
if (ledPin==12){
  
  for( oldVal=12; oldVal >= 2; oldVal--)  {
    ledPin=oldVal;
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
    
   while(counter < 100000) {     //just a manual delay funct cuz the original one did not work at all
    counter++;
    }
    counter=0; //reset which i added
    }
} 

else  {
  
  }
  }
  return 0;
  }

And my final product, thanks everybody for reading my posts. Led blinking in series, that means led 1 blinks then led 2 then led 3 to led 12 and then back again.

long long counter = 1000;
int main () {
  int ledPin; //maybe i should have picked only one, tried several versions and this logic worked in my head the most
  int oldVal;
  while(1) {
  for( oldVal=2; oldVal <= 12; oldVal++)  {
    ledPin=oldVal;
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
    
   while(counter < 100000) {     //just a manual delay funct cuz the original one did not work at all
    counter++;
    }
    digitalWrite(ledPin, LOW);
    
    counter=0; //reset which i added
    }
  

  
  for( oldVal=12; oldVal >= 2; oldVal--)  {
    ledPin=oldVal;
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
    
   while(counter < 100000) {     //just a manual delay funct cuz the original one did not work at all
    counter++;
    }
    
    
    counter=0; //reset which i added
    digitalWrite(ledPin, LOW);
    }
} 


  
  
  
  return 0;
}

almirlalicic:
I didnt use a setup and loop becouse it seemed redundant to me to make a loop function if im already trying to use a for loop. And cant use pinMode in setup becouse i want to increment a pinMode variable by one every time it passes true through a loop...

You set the mode of all the pins once and you don't need to repeat them again.
What you increment is the variable you use to access pins through their numbers, that doesn't set the modes of those pins.

I -do- change pin modes in loop() when I am for example polling buttons in a row&column matrix but that is on need to only have 2 pins acting on the matrix at a time.

Arduino setup() and loop() are automation-friendly and the chips on the boards are microcontrollers made for automation.

When/If you do learn cooperative tasking on Arduinos you will see how using void loop() to interate through processes can give you very fast responses to all sensed events.

Also be aware that on small Arduinos there is not much RAM. Small values like pin numbers are best held in type byte variables, to use type int wastes a byte per pin. Sure in a small sketch it matters not but the habit does not end with sketch size, it gets stronger.

There is a book titled Processing and Wiring about PC+MCU. There is a Processing language (in Java) side you can get a processing.org and Arduino came about as a version of Wiring but most of the discussion has nothing to do with Processing.

You have a tool that can be used in many ways. The default way has definite benefits.

Also when standard C main() function ends you are dropped back into the OS. Arduino has no OS.

In order to write in C you should move your code into a file called 'main.c' in your sketch folder. That way to C compiler will be used and not the C++ compiler. Keep the sketch file itself (.ino) empty.

You will have to include Arduino.h explicitly since that is something that Arduino does automatically for .ino files. If you don't include it, constants like "OUTPUT" and "HIGH" will not be defined. As you have been told before, it's not safe to use any Arduino features without first calling 'init()'. After you call init(), the delay() function should work. Also note that Arduino adds forward declarations in Arduino sketches. In C code you will have to declare functions before you call them.

#include <Arduino.h>


void delayAWhile();  // Forward Declaration (not supplied by Arduino on .c files)


int main () {
  init();  // Initilize the Arduino libraries


  // Set pins 2 through 12 to OUTPUT mode
  for (byte ledPin = 2; ledPin <= 12; ledPin++)
    pinMode(ledPin, OUTPUT);


  while (true) {
    // Light 2 through 12 in sequence
    for (byte ledPin = 2; ledPin <= 12; ledPin++)  {
      digitalWrite(ledPin, HIGH);
      delayAWhile();
      digitalWrite(ledPin, LOW);
    }


    // Light 12 through 2 in sequence
    for (byte ledPin = 12; ledPin >= 2; ledPin--)  {
      digitalWrite(ledPin, HIGH);
      delayAWhile();
      digitalWrite(ledPin, LOW);
    }
  }
  return 0; // Never reached
}


//just a manual delay funct cuz the original one did not work at all
void delayAWhile() {
  long long counter = 0;
  while (counter < 100000UL) {
    counter++;
  }
}

John, did you use type long long because they're slower to process than type long?

GoForSmoke:
John, did you use type long long because they're slower to process than type long?

I used 'long long' because that is what the OP used. My guess is the same as yours: he did it to get the delay he wanted.
Since my code calls 'init()' it should be safe to replace "delayAWhile()" with "delay()" and use a time in milliseconds. I left his delay code in (as a function) because:
A) He didn't bother to indicate what length of delay he was aiming for, and
B) It demonstrates the need to forward declare functions in C code.

It's nice seeing again that we can use a main() function but once again I don't see any good reason to do that.

There are other compilers that can program AVR's, more professional ones that handle classes right or so I've been informed.

GoForSmoke:
It's nice seeing again that we can use a main() function but once again I don't see any good reason to do that.

He said in the original post: "just read a book about c language and trying to sharpen those newley found skills in practice." I think it's a bit silly to use Arduino to practice C programming but it appeared to be his desire.

How many free C compilers are there for the PC?

I hope that the OP has more ideas about using Arduino.

johnwasser:
A) He didn't bother to indicate what length of delay he was aiming for, and

Hello

Yup that's true as its my first more serious exercise since blink just one led so i didnt bother on predicted timing just as long as it looks nice. Im sure my code is messy right now, but i got the output i aimed for. With ur guys help and more exercises i hope to optimize my coding and overall efficency. My end goal is to learn programming and controlling pins by bytewise operators and shifting and stuff.
Still have a long way to go but the feeling when u code something and it Works is awesome so i Will keep on trying.

Thank u for advices.

johnwasser:
He said in the original post: "just read a book about c language and trying to sharpen those newley found skills in practice." I think it's a bit silly to use Arduino to practice C programming but it appeared to be his desire.

I know its bit silly but i had it from way back. Im trying to become more comfortable with loops, functions and programming logic overall before i venture into more serious Waters. I tried programming in c in codeblocks but find more pleasure in outputing a led or something than a printf and such.