I have been having some fun, finding different ways of doing the blink sketch, mostly to play around with condition, and to see what was possible to do with it.
//Constants
const int ledPin = 13; //The pin we have the LED on
//Variables
boolean ledOn = true; //Store the last status of the LED
//Sketch
void setup()
{
pinMode(ledPin, OUTPUT); //Set the pin we got the LED on to OUTPUT
}
void loop()
{
digitalWrite(ledPin, ledOn?HIGH:LOW); //Turn the LED on or off, depending on if ledOn is true or false
ledOn = ledOn?false:true; //Toggle between false and true
delay(500);
}
It allows you to read, toggle or set any pin on the arduino to high or low. Ignoring the read/write bits, and the stuff for non-pin 13 pins, it can be simplified down to this:
retrolefty:
Software people are just plain strange.
I bet you all loved diagramming sentences in the eighth grade.
I can't speak for others. I'm kind of software person from age of 6. I don't think I liked diagramming sentences but I do draw flow charts for complex code I intend to write
A hardware guy will try to initialize a timer and drive the pin with the timer I suppose?
What would the assembly language soloution to this be? That would be quick and eficient. The bit that would take up the most memory would be the delay(1000) part. Shortening it to while(millis() - lastMillis < 1000) may be better, or may not be. If there was some simpler way to do that, then who knows how fast/simple/small the code could be?
Onions:
What would the assembly language soloution to this be? That would be quick and eficient. The bit that would take up the most memory would be the delay(1000) part. Shortening it to while(millis() - lastMillis < 1000) may be better, or may not be. If there was some simpler way to do that, then who knows how fast/simple/small the code could be?
Onions.
I'm not up on AVR assembly language, and I'm not used to RISC machines in general, but it sure doesn't take much to set up a timer to count milliseconds. Maybe a dozen or two instructions to set up the timer and interrupt?
Kid stuff. Try BrainF*ck (No, I don't write that, nor would I ever try it.) Then there's Intercal.
If I knew what the binary representation of HIGH and LOW is (yes, I could be non-lazy and attempt to look that up), I'd use XOR, if those values are bit-complementary. (So there's probably a reason nobody has suggested that yet.)
Blinks the LED using timers and prot mainpulation, taking away the need for digitalWrite, pinMode, and delay. The code compiles to 524 bytes, as oppose to 1010 bytes for normal blink. I learned about the timers on here: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106 . Lots of good info for a timer newbie!
I'm up to the pre-scaler bit, and trying to set up an 8 bit timer with a /1024 pre-scaler. You need some of the info hidden in the datasheet for it; the datasheet can be found here: http://www.atmel.com/dyn/resources/prod_documents/doc8025.pdf . At over 400 pages long, finding the relevant pages is extremely difficult... I've saved a copy to my computer though, for future reference.
Learning about timers is something I have also wanted to do for a long time, but I only got round to finding that page yesterday. It provides for interesting reading though!
I read that a few months back; very well written, I'd like to see the guy do more! If we're not hung up on using the "pin 13" LED, then a timer can do the blinking for us with no additional code once it's set up:
//Turns an LED connected to Arduino pin 9 on for one second,
//then off for one second, repeatedly. Uses Timer/Counter1 to divide the 16MHz clock
//by 256 with the prescaler (to 62500 Hz) and then toggles the pin after
//62500 counts (i.e. 0 to 62499).
//
//478 bytes with Arduino 0022
void setup(void) {
DDRB = _BV(DDB1); //set OC1A/PB1 as output (Arduino pin D9, DIP pin 15)
TCCR1A = _BV(COM1A0); //toggle OC1A on compare match
OCR1A = 62499; //top value for counter
TCCR1B = _BV(WGM12) | _BV(CS12); //CTC mode, prescaler clock/256
}
void loop(void) {
}