Binary LED Counter

11/04/2022

The LED Binary counter was a fun one day project. The resistors are all 220 and the code is below:

I know the code is far from optimal, leave any comments under the YouTube video.

int led1=2;

int led2=3;

int led3=4;

int led4=5;

int led5=6;

int led6=7;

int led7=8;

int led8=9;

int led9=10;

int led10=11;

float dt;

int potpin=A1;

float j;

float h;

int count = 0;

unsigned long prevTime_1 = millis();

void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);

pinMode(led2,OUTPUT);

pinMode(led3,OUTPUT);

pinMode(led4, OUTPUT);

pinMode(led5, OUTPUT);

pinMode(led6,OUTPUT);

pinMode(led7, OUTPUT);

pinMode(led8, OUTPUT);

pinMode(led9, OUTPUT);

pinMode(led10, OUTPUT);

pinMode(potpin, INPUT); }

void loop() {

unsigned long currentTime = millis();

j = analogRead(potpin);

dt = j * 1.5 + 1.5;

if ( j > 900){

dt = 10000000;

}

Serial.print(j);

Serial.println(dt);

if (currentTime - prevTime_1 > dt){

prevTime_1 = currentTime;

count = count + 1;

if ((count & 0b0000000001)==0){

digitalWrite(led1,0);

} else {

digitalWrite(led1,1);

}

if ((count & 0b0000000010)==0){

digitalWrite(led2,0);

} else {

digitalWrite(led2,1);

}

if ((count & 0b0000100)==0){

digitalWrite(led3,0);

} else {

digitalWrite(led3,1);

}

if ((count & 0b0001000)==0){

digitalWrite(led4,0);

} else {

digitalWrite(led4,1);

}

if ((count & 0b0000010000)==0){

digitalWrite(led5,0);

} else {

digitalWrite(led5,1);

}

if ((count & 0b0000100000)==0){

digitalWrite(led6,0);

} else {

digitalWrite(led6,1);

}

if ((count & 0b0001000000)==0){

digitalWrite(led7,0);

} else {

digitalWrite(led7,1);

}

if ((count & 0b0010000000)==0){

digitalWrite(led8,0);

} else {

digitalWrite(led8,1);

}

if ((count & 0b0100000000)==0){

digitalWrite(led9,0);

} else {

digitalWrite(led9,1);

}

if ((count & 0b1000000000)==0){

digitalWrite(led10,0);

} else {

digitalWrite(led10,1);

} } }

Create your website for free!