Arduino blink with condition

Just a small example on another way to do the default blink sketch

//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);
}

or with even less functions

//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);
}

or maybe inside the digitalWrite itself?

//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);
}

or to really strip it down

int ledOn = HIGH;

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

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

or

int ledOn;

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

void loop()
{
  digitalWrite(13, ledOn=1-ledOn);
  delay(500);
}

or minimum

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

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

Leave a Reply