How many ways can we make this code?

Right, this is bar sport so how many ways can we make this code?
The stranger and more conveluted the better. As long as the function is the same then you can do anything.

Here is the code:

const int button = 2;
const int led =  13;

void setup() {
  pinMode(led, OUTPUT);      
  pinMode(button, INPUT);     
}

void loop(){  
    digitalWrite(led, HIGH); 
    delay(1000); 
    digitalWrite(led, LOW);
    delay(1000);
    if(digitalRead(button) == HIGH){
    digitalWrite(led, HIGH);
    delay(200); 
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200); 
    digitalWrite(led, LOW);
    delay(200);
    }
}
const int button = 2;
const int led =  13;

void setup() {
  pinMode(led, OUTPUT);      
  pinMode(button, INPUT);     
}

void loop(){  
    digitalWrite(led, HIGH); 
    delay(1000); 
    digitalWrite(led, LOW);
    delay(1000);
    if(digitalRead(button) == HIGH){
      for(int i = 0; i <2; i++){
    digitalWrite(led, HIGH);
    delay(200); 
    digitalWrite(led, LOW);
    delay(200);
      }
    }
}

Boring starter one for you.

const int button = 2;
int delays = 0;
int delaycount = 1000;


void setup() {   
  pinMode(button, INPUT);
  while(delays < delaycount){
   delays++;
  } 
}

void loop(){  
    PORTB = B00100000; 
    delay(delays); 
    PORTB = B00000000;
    delay(delays);
    if(digitalRead(button) == HIGH){
      for(int i = 0; i <2; i++){
    PORTB = B00100000; 
    delay(200); 
    PORTB = B00000000;
    delay(200);
      }
    }
}

:wink:

void setup() {
  DDRD&=0xFB;
  DDRB| =0x20;
}

void loop(){
    PORTB| =0x20
    delay(1000);
    PORTB&=0xDF;
    delay(1000);
    if(PORTD&0x04){
     PORTB| =0x20
    delay(200);
    PORTB&=0xDF;
    delay(200);
     PORTB| =0x20
    delay(200);
    PORTB&=0xDF;
    delay(200);
    }
}

;D I like this way

const int button = 2;
const int led =  13;

void define()
{
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

void ledBlink()
{  
  for(int i = 0; i <2; i++)
  {
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
  }
}

void setup()
{
  define();
}

void loop()
{
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
    
    if(digitalRead(button) == HIGH)
    {
      ledBlink();
    }
}

Why, isn’t it obvious one needs a two-dimensional state machine, combined with a hashtable to handle the delays? ;D
Sorry, won’t write it out, a bit too much work for something a bit too trivial for me :wink:

Why, isn’t it obvious one needs a two-dimensional state machine, combined with a hashtable to handle the delays? ;D
Sorry, won’t write it out, a bit too much work for something a bit too trivial for me

Sounds like fun to me :smiley:

ITeads code does not the same. It does almost the same but it is faster :wink:

Udo

ITeads code does not the same. It does almost the same but it is faster

As long as the function is roughly the same… :wink:

Just some ideas, too lazy to write code:

Use a RTC module to keep track of time
Make an accompanying processing sketch that tells the arduino what to do

Use a RTC module to keep track of time
Make an accompanying processing sketch that tells the arduino what to do

Haha that’s good.

I think suggestions are just as good. Writing code can be a bore…

Roughly the same:

const int button = 2;
const int led =  13;


void inline s(uint8_t d1, uint8_t d0) {
      digitalWrite(led, 1);
      delay(d1*200);
      digitalWrite(led, 0);
      delay(d0*200);
}

boolean inline b() {
  return digitalRead(button);
}


void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}


void loop() {
    s(5,5);
    if(!b) {
      s(1,1);
      s(1,1);
    }
}

Udo

Maybe a geiger counter with a known counts per minute radioactive substance and some code to account for the half life to make sure that counts are accurate :stuck_out_tongue:

Then average the counts out over a few days and work out delay timing from that :smiley:

void loop() {
s(5,5);
if(!b) {
s(1,1);
s(1,1);
}

Ohh, someone’s trying to be confusing now :slight_smile:

Improved version :wink:

const int button = 2;
const int led =  13;


void inline s(uint8_t d) {
      digitalWrite(led, 1);
      delay(d*200);
      digitalWrite(led, 0);
      delay(d*200);
}

boolean inline b() {
  return digitalRead(button);
}


void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}


void loop() {
    for(s(5);!b();s(5));s(1);!b();s(1);
}

I tested it and I am very impressed. Some people have done this before ;D

Hmm - maybe there should be an IOCCC category for microcontrollers…?

:smiley:

you really did it!
here mine:

const int button = 2;
const int led =  13;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

void loop(){
    int delay1 = 1000, i, state=1;
    for (i=0;i<2;i++){
      light(delay1, state);
      state=!state;
    }
    delay1=200;
    digitalRead(button) == (!state)?
    for (i=0;i<4;i++){
      light(delay1, state);
      state=!state;
    }
} 

void light(int d, int s){
  digitalWrite(led, s);
   delay(d);
}

or we can try to use a Neural Network…

edit: untested becouse of my atmega…

To think one could learn something from this thread :slight_smile:
I like Udo’s code! Also the neural net idea.

For lack of imagination, this is s quick (untested) rewrite of the original post with a few #define’s thrown in. Based on something similar I did a while back.

Strangely, “#define vo void” would not work. (well, it worked, but I could not use the “vo” define).

Sorry for the teletype instead of code tags, but it wouldn’t show correctly otherwise.

const int bt = 2;
const int ld =  13;
#define su setup
#define pM pinMode
#define OP OUTPUT
#define IP INPUT
#define LO LOW
#define HI HIGH
#define dR digitalRead
#define dl delay
#define dW digitalWrite
#define lp loop
#define k1 1000
#define h2 200


void     su           (){    pM     (    ld     ,
OP  );   pM           (bt    ,IP    )    ;     }
void     lp           (){    dW (  ld    , HI);
dl (k1   );           dW(    ld  , LO    );
dl  (k1  );           if(    dR  ( bt    )== 
HI  ){   dW           (ld   ,HI   ) ;    dl (h2
); dW    (ld,         LO)   ;dl    (     h2   );
dW(      ld,HI);dl(   h2);   dW   (ld,   LO    );   dl(h2);}}

I think this one is a winner, raron.