Blinky!

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);
}
//Constants
const int ledPin = 13;

//Variables
int ledOn = HIGH;

//Sketch
void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, ledOn);
  ledOn = ledOn?LOW:HIGH;
  delay(500);
}
//Constants
const int ledPin = 13;

//Variables
int ledOn = HIGH;

//Sketch
void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, ledOn=ledOn?LOW:HIGH);
  delay(500);
}
int ledOn = HIGH;

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

void loop()
{
  digitalWrite(13, ledOn=ledOn?LOW:HIGH);
  delay(500);
}
void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  digitalWrite(13, digitalRead(13)?LOW:HIGH);
  delay(250);
}

anyone got an easier way of doing it?

anyone got an easier way of doing it?

Nope, but I may have a faster way:

#define TOGGLE 2
boolean digitalReadB(int pin){
  return PINB & (1 << pin - 8);
}
boolean digitalReadC(int pin){
  return PINC & (1 << pin);
}
boolean digitalReadD(int pin){
  return PIND & (1 << pin);
}
void digitalWriteB(int pin, boolean state){
  if(state == 1) PORTB = (PORTB | ( 1 << pin - 8));
  else if(!state) PORTB = PORTB & (~(1 << pin - 8));
  else PORTB = (PORTB ^ ( 1 << pin - 8));
}
void digitalWriteC(int pin, boolean state){
  if(state == 1) PORTC = (PORTC | ( 1 << pin));
  else if(!state) PORTC = PORTC & (~(1 << pin));
  else PORTC = (PORTC ^ ( 1 << pin));
}
void digitalWriteD(int pin, boolean state){
  if(state == 1) PORTD = (PORTD | ( 1 << pin));
  else if(!state) PORTD = PORTD & (~(1 << pin));
  else PORTD = (PORTD ^ ( 1 << pin));
}


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

void loop(){
  digitalWriteB(13, TOGGLE);
  delay(1000);
}

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:

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

void loop(){
  PORTB=(PORTB^(1<<5));
  delay(1000);
}

Which is faster, and simpler.

Onions.

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

void loop()
{
digitalWrite(13, ! digitalRead(13) );
delay(250);
}

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

void loop()
{
  digitalWrite(13, 1 / !digitalRead(13) );
}

]:smiley:

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

void loop()
{
  digitalWrite(13, (millis()/250)%2);
}

:wink:

Software people are just plain strange.

I bet you all loved diagramming sentences in the eighth grade. :wink:

retrolefty:
Software people are just plain strange.

I bet you all loved diagramming sentences in the eighth grade. :wink:

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 :slight_smile:

A hardware guy will try to initialize a timer and drive the pin with the timer I suppose? :smiley:

retrolefty:
Software people are just plain strange.

Haha, and proud of it XD You may be aware of http://www.ioccc.org/, too bad it seems to have fallen by the wayside a bit.

Very extremely geeky even for me. Well, I can't understand most of it :fearful: I might appreciate self-morphing assembly code a bit better.

liudr:
Well, I can't understand most of it :fearful:

Of course that is the exact goal! XD

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? :slight_smile:

Onions.

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? :slight_smile:

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.)

Don't think I saw

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

void loop() {
    PINB = _BV(PINB5);
    delay(1000);
}
byte count;

void setup(){
  DDRB = B00100000;
  TCCR1B |= (1 << CS10);
}

void loop(){
  if(TCNT1 >= 250){
    count++;
    TCNT1=0;
  }
  if(count >= 250){
    count = 0;
    TCNT1=0;
    PORTB=PORTB^B00100000;
  }
}

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!

Onions.

Bookmarked. I've thought about using timers for a while. Thanks for sharing.

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!

Onions.

Onions:
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 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) {
}