Hi, I'm new to ardu and in general to the micro controllers.
I've purchased Arduino Mega 2560
and I've started to play with the "Blink"
I've encountered a small problem, not sure if it is my approach to the programming,
or what....?
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
int led = 5; //init for pin 5 connected led, leds also connected 6&7
int i = 5; //counter for "for" loop
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
i=5;
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
for(i=5;i<8;i++){
led=i;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
if (i==8){
i=5;
}
}
}
what i wanted to get is 3 LED's connected to the pins 5,6,7 to light up, one after another.
And LED on pin 5 works ok, but 6 and 7 are very, very, dim. they are tested and working the same way if i'll swap their places.
even in tutorial there is LED stuck in pin 13 without any resistor
That doesn't make it right. We've been trying for years to get the team to fix that.
An LED draws as much current as it can. That can damage the Arduino. A current limiting resistor limits how much current the LED can draw, protecting the Arduino.
Use them or plan on replacing the Arduino when the pin quits working because it has been abused too often. Your choice.
Your code only works if the LEDs are attached to sequentially numbered pins, which lacks flexibility. Here I've held the pin numbers in an array and then I can just process the array. With this approach the LEDs do not have to be on sequential pins and another LED could be added easily by adding it's pin number to the ledPin array and changing MAX_PINS to suit.
I did not compile or otherwise test the following:
byte ledPin[] = {5, 6, 7} // array of pin numbers
define MAX_PINS 3 // change to match number of pins used
void setup(){
for (byte i = 0; i < MAX_PINS; i++)
{
pinMode(ledPin[i], OUTPUT); // set up all the pins as output
}
}
void loop(){
for (byte i = 0; i < MAX_PINS; i++) // blink each led separately one after the other
{
digitalWrite(ledPin[i], HIGH);
delay(1000);
digitalWrite(ledPin[i], LOW);
delay(1000);
}
}
even in tutorial there is LED stuck in pin 13 without any resistor
That doesn't make it right. We've been trying for years to get the team to fix that.
An LED draws as much current as it can. That can damage the Arduino. A current limiting resistor limits how much current the LED can draw, protecting the Arduino.
Use them or plan on replacing the Arduino when the pin quits working because it has been abused too often. Your choice.