help with coding (10 leds)

hey,

a part of my project is to sent a kind of binair code with leds. sounds strange... i know it is.
i have 10 leds and i want to display all possible combination of the 10 leds.. (a bit of math shows that there are 1024 possible combinations , 2^10) for example (0000000000, 0000000001, 0000000011 and so on, the '0' being the led is LOW and '1' being HIGH)
i've troubles figuring out how could do that..

any help is welcome :slight_smile:

cheers

've troubles figuring out how could do that..

Why ?

int display = B0000001000
       for (i=0; i<1024; i++)
             display ++;

and how should i write that to every individual led?

i'm pretty new to working with an arduino :s

Heavens no. That gives you the current value.
You need to use a mask for each bit.
For example:

Let's say the bits are assigned like this:
d2 = 0
d3=1
d4=2
d5=4
d6=8
d7=16
d8=32
d9=64
d10=128
d11=256

then for d6,
display = B0000001000; // NOTE : THE SEMICOLON WAS MISSING IN THIS LINE IN MY PREVIOUS POST.
int d6_bit = B0000001000 & display; //BITWISE "AND" operation with current value yields the fourth bit HIGH for any value for which that bit is high, and all the other bits 0, which will always equal decimal 8, or B0000001000.

   if (d6_bit =8)
       digitalWrite(d6,HIGH);
int delayt=500;
      delay(delayt);

You need to duplicate the above code for each bit position

found it!!!!

for those who are interested

but thanks for the effort raschemmel
code:

int switchstate = 0;
int led[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

void setup() {
pinMode(13, INPUT);
for (int instl = 0; instl < 10; instl++) {
pinMode(led[instl], OUTPUT );
}
}

void loop() {
switchstate = digitalRead(13);

if (switchstate == HIGH) {
for (byte counter =0;counter<=1024; counter++)
{
displayBinary(counter);
delay(100);
}
}

}

void displayBinary(byte numToShow)
{
for (int i =0;i<10;i++)
{
if (bitRead(numToShow, i)==1)
{
digitalWrite(led*, HIGH);*

  • }*
  • else*
  • {*
    _ digitalWrite(led*, LOW);_
    _
    }_
    _
    }_
    _
    }_
    _
    [/color]*_

Re: help with coding (10 leds)
« Reply #4 on: Today at 11:15:50 pm »
Bigger Bigger Smaller Smaller Reset Reset Reply with quoteQuote
found it!!!!

for those who are interested

but thanks for the effort raschemmel
code:

int switchstate = 0;
int led[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

void setup() {  
  pinMode(13, INPUT);
  for (int instl = 0; instl < 10; instl++) {
      pinMode(led[instl], OUTPUT );
    }
}

void loop() {
  switchstate = digitalRead(13);
  
  if (switchstate == HIGH) {
     for (byte counter =0;counter<=1024; counter++)
  {
    displayBinary(counter);
    delay(100); 
  }
    }
    
  }
  
void displayBinary(byte numToShow)
{
  for (int i =0;i<10;i++)
  {
    if (bitRead(numToShow, i)==1)
    {
      digitalWrite(led, HIGH); 
    }
    else
    {
      digitalWrite(led, LOW); 
    }
  }

}

FYI, YELLOW IS IMPOSSIBLE TO READ.

That code in displayBinary() is not going to work.
These lines are wrong - or at least will not do what you want:

      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);

They will be sending a garbage pin value to digitalWrite()
If you turn on warnings in the IDE you will probably get a warning on the lines
since you are sending it an address rather than an int.

You need to use i as index into your led[] array.

--- bill

i found an other way to do it... maybe not the best method but it worked for me

int switchstate = 0;
int led[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

void setup() {  
  pinMode(13, INPUT);
  for (int instl = 0; instl < 10; instl++) {
      pinMode(led[instl], OUTPUT );
    }
}

void loop() {
  switchstate = digitalRead(13);
  
  if (switchstate == HIGH) {
    for(int i=0;i<1024;i++)  // increment automatically from 0 to 15 
  {  
   int a=i%2;      // calculate LSB   
   int b=i/2 %2;     
   int c=i/4 %2;        
   int d=i/8 %2; 
   int e=i/16 %2; 
   int f=i/32 %2; 
   int g=i/64 %2; 
   int h=i/128 %2; 
   int j=i/256 %2; 
   int k=i/512 %2; //claculate MSB
   digitalWrite(led[9],k);
   digitalWrite(led[8],j);
   digitalWrite(led[7],h);
   digitalWrite(led[6],g);
   digitalWrite(led[5],f);
   digitalWrite(led[4],e);
   digitalWrite(led[3],d);   //write MSB
   digitalWrite(led[2],c);   
   digitalWrite(led[1],b);    
   digitalWrite(led[0],a);  // write LSB 
   tone(12, 440,10); 
   
   delay(40);     // wait for a second  
  }  
    
  }
    
  }