Go Down

Topic: Blinky! (Read 2 times) previous topic - next topic

bld

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.

Code: [Select]
//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);
}


Code: [Select]
//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);
}


Code: [Select]
//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);
}


Code: [Select]
int ledOn = HIGH;

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

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


Code: [Select]
void setup()
{
 pinMode(13, OUTPUT);
}

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


anyone got an easier way of doing it?
captain-slow.dk | non contagious!

Onions

Quote
anyone got an easier way of doing it?


Nope, but I may have a faster way:

Code: [Select]
#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:

Code: [Select]
void setup(){
  pinMode(13, OUTPUT);
}

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


Which is faster, and simpler.

Onions.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

Coding Badly

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

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

Chagrin

Code: [Select]
void setup()
{
  pinMode(13, OUTPUT);
}

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


]:D

liudr


Code: [Select]
void setup()
{
  pinMode(13, OUTPUT);
}

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


;)

retrolefty

Software people are just plain strange.

I bet you all loved diagramming sentences in the eighth grade.  ;)

liudr


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

Jack Christensen


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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

liudr



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  :smiley-eek-blue: I might appreciate self-morphing assembly code a bit better.

Jack Christensen


Well, I can't understand most of it  :smiley-eek-blue:


Of course that is the exact goal!  XD
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

Jack Christensen


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?
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

justjed



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.


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.)
... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier

Jack Christensen

Don't think I saw

Code: [Select]
void setup(void) {               
    pinMode(13, OUTPUT);     
}

void loop() {
    PINB = _BV(PINB5);
    delay(1000);
}
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Onions

Code: [Select]
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.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

Go Up