i want the basic project of arduino with full programmming
i want the basic project of arduino with full programmming
But its always fun to just mess about with code for just fun. Anyways it has been a while since I've played with Arduino's compiler but I've just had a quick 10 min play while waiting for something else to finish.
While the first delay(1000); adds 190 bytes to the program every other delay(1000); only adds 12 bytes where each _delay_ms(1000); adds 18 bytes to the program so depending on how many delays you have in your program you might find its more efficient to use the Arduino delay(); over the AVR Libc _delay_ms(); A quick of the top of my head calculation would put that number around the 30 mark.
void setup() {
DDRB = DDRB | B00100000; // set pin 13 to output
}
void loop() {
PORTB = PORTB ^ B00100000;
delay(1000);
}Binary sketch size: 666 bytes (of a 32256 byte maximum, 2.06 percent).
void setup() {
DDRB = DDRB | B00100000; // set pin 13 to output
}
void loop() {
PORTB = PORTB ^ B00100000;
delay(1000);
delay(1000);
}Binary sketch size: 678 bytes (of a 32256 byte maximum, 2.10 percent).
void setup() {
DDRB = DDRB | B00100000; // set pin 13 to output
}
void loop() {
PORTB = PORTB ^ B00100000;
delay(1000);
delay(1000);
delay(1000);
}Binary sketch size: 690 bytes (of a 32256 byte maximum, 2.14 percent).
By easy, I did not mean short. Short helps, but sensible, easy to read code is just as important. Binary sketches are a little to difficult for a first project. When I made that post I didn't mean it as a challenge, but it's nice to see that you can do so much to such a simple program.
Yeah we got a little carried away with just messing around with code. Using digitalWrite(); is a lot simpler as the Arduino core handles all the bit handling and pin addressing for you. Sure on the ATMega328/168 DDRB = DDRB | B00100000; will set Arduino Pin 13 as an output but the Arduino is not just based around that ATMega. If you were using the Leonardo which is based around the ATMega32U4 pin 13 is actually C7 so if you wanted the same blink program running on the Leonardo you would have to change the code to:-
void setup() {
DDRC = DDRC | B10000000; // set pin 13 to output
}
void loop() {
PORTC = PORTC ^ B10000000;
_delay_ms(1000);
}Which isn't friendly to newcomers. digitalWrite(); gets rid of the headache and just handles all that for you.
That is an interesting observation, I would have expected the low level call to be better in all cases or certainly no worse after the first higher level call
That is an interesting observation, I would have expected the low level call to be better in all cases or certainly no worse after the first higher level call
But you can get even smaller than _delay_ms(); and pretty efficient for the simple tests we are doing
void setup() {
DDRB = DDRB | B00100000; // set pin 13 to output
}
void loop() {
PORTB = PORTB ^ B00100000;
delnms(1000);
}
void delnms(unsigned int n){
//delay n ms
unsigned int x;
while(n--){
x=2600; //empirically determined fudge factor 16mhz
while(x--);
}
}Binary sketch size: 476 bytes (of a 32256 byte maximum, 1.48 percent).
Source: View topic - AVR Delay Functions :: AVR Freaks
void setup() {
DDRB = DDRB | B00100000; // set pin 13 to output
}
void loop() {
PORTB = PORTB ^ B00100000;
delnms(1000);
delnms(500);
delnms(1000);
}
void delnms(unsigned int n){
//delay n ms
unsigned int x;
while(n--){
x=2600; //empirically determined fudge factor 16mhz
while(x--);
}
}Binary sketch size: 476 bytes (of a 32256 byte maximum, 1.48 percent).
Yup even with more calls to the delay and even different delays didn't increase the compile size, but using this method can be a bit finicky. For example interrupts during the delay will cause the delay to increase in length.