Pages: [1] 2   Go Down
Author Topic: Blinky!  (Read 2003 times)
0 Members and 1 Guest are viewing this topic.
Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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:
//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:
//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:
int ledOn = HIGH;

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

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

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

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

anyone got an easier way of doing it?
Logged

captain-slow.dk | non contagious!

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
anyone got an easier way of doing it?

Nope, but I may have a faster way:

Code:
#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:
void setup(){
  pinMode(13, OUTPUT);
}

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

Which is faster, and simpler.

Onions.
Logged

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

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 208
Posts: 12939
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Dubuque, Iowa, USA
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2521
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

 smiley-twist
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

 smiley-wink
Logged


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Software people are just plain strange.

I bet you all loved diagramming sentences in the eighth grade.  smiley-wink
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Software people are just plain strange.

I bet you all loved diagramming sentences in the eighth grade.  smiley-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 smiley

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


Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Software people are just plain strange.

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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


Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Of course that is the exact goal!  smiley-lol
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?   smiley

Onions.
Logged

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?   smiley

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?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

CO, USA
Offline Offline
God Member
*****
Karma: 5
Posts: 711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Software people are just plain strange.

Haha, and proud of it smiley-lol  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.)
Logged

... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't think I saw

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

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

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

Pages: [1] 2   Go Up
Jump to: