digitalWrite pins in sequence from 0001 to 9999?

Hi I was wondering if there is a way to make this program trigger the pins by counting the number 00001 to 10000 like 0001 Enter 0002 Enter 0003 Enter 0004 Enter etc... until 9999 is reached and then reset back to 0001. I think this might be a bit too advanced for my lil programming knowledge and think there is a much easier way to do this then type out the below code for "0001 Enter" and then have to make 49995 other entries :(

digitalWrite(Pin11, HIGH);
delay(1000);
digitalWrite(Pin11, LOW);  
delay(1000);

digitalWrite(Pin11, HIGH);
delay(1000);
digitalWrite(Pin11, LOW);  
delay(1000);

digitalWrite(Pin11, HIGH);
delay(1000);
digitalWrite(Pin11, LOW);  
delay(1000);

digitalWrite(Pin2, HIGH);
delay(1000);
digitalWrite(Pin2, LOW); 
delay(1000)

digitalWrite(Pin12, HIGH);
delay(1000);
digitalWrite(Pin12, LOW);  
delay(1000);

Here is the code I have now that will trigger the numbers 0 to 9 with a 1 second delay. I would like to keep the delay placed after enter. Hope someone can make my week :)

const int Pin2 = 2; //1
const int Pin3 = 3; //2
const int Pin4 = 4; //3
const int Pin5 = 5; //4
const int Pin6 = 6; //5
const int Pin7 = 7; //6
const int Pin8 = 8; //7
const int Pin9 = 9; //8
const int Pin10 = 10; //9
const int Pin11 = 11; //0
const int Pin11 = 12; //Enter

void setup() {
  pinMode(Pin2, OUTPUT);  
  pinMode(Pin3, OUTPUT); 
  pinMode(Pin4, OUTPUT); 
  pinMode(Pin5, OUTPUT); 
  pinMode(Pin6, OUTPUT); 
  pinMode(Pin7, OUTPUT); 
  pinMode(Pin8, OUTPUT); 
  pinMode(Pin9, OUTPUT); 
  pinMode(Pin10, OUTPUT); 
  pinMode(Pin11, OUTPUT);  
  pinMode(Pin12, OUTPUT); 
}

void loop(){

digitalWrite(Pin2, HIGH);
delay(1000);
digitalWrite(Pin2, LOW); 
delay(1000)

digitalWrite(Pin3, HIGH);
delay(1000);
digitalWrite(Pin3, LOW);  
delay(1000);

digitalWrite(Pin4, HIGH);
delay(1000);
digitalWrite(Pin4, LOW);  
delay(1000);

digitalWrite(Pin5, HIGH);
delay(1000);
digitalWrite(Pin5, LOW);  
delay(1000);

digitalWrite(Pin6, HIGH);
delay(1000);
digitalWrite(Pin6, LOW);  
delay(1000);

digitalWrite(Pin7, HIGH);
delay(1000);
digitalWrite(Pin7, LOW);  
delay(1000);

digitalWrite(Pin8, HIGH);
delay(1000);
digitalWrite(Pin8, LOW);  
delay(1000);

digitalWrite(Pin9, HIGH);
delay(1000);
digitalWrite(Pin9, LOW);  
delay(1000);

digitalWrite(Pin10, HIGH);
delay(1000);
digitalWrite(Pin10, LOW);  
delay(1000);

digitalWrite(Pin11, HIGH);
delay(1000);
digitalWrite(Pin11, LOW);  
delay(1000);

digitalWrite(Pin12, HIGH);
delay(1000);
digitalWrite(Pin12, LOW);  
delay(1000);

I think you need to explain in more detail what it is you are trying to achieve.

From what you wrote in your first post i do not have the sligthest idea of what you are trying to do :-)

And what du you men when you write "Enter" ??

the code you posted will turn 11 different pins on for a second then off, and wait for a second before the next pin is given the same treatment, but where does the counting to 10000 come into the picture ?

I think you are looking for: http://www.arduino.cc/en/Reference/For but I would agree that I have not got a lot of ideas about what you are trying to do!

Mowcius

The code I posted does that correct but thats all I am capable of programing. I really want it to count to 9999 where the following pins = the following numbers:

const int Pin2 = 2; //1 const int Pin3 = 3; //2 const int Pin4 = 4; //3 const int Pin5 = 5; //4 const int Pin6 = 6; //5 const int Pin7 = 7; //6 const int Pin8 = 8; //7 const int Pin9 = 9; //8 const int Pin10 = 10; //9 const int Pin11 = 11; //0 const int Pin11 = 12; //Enter

So it will first trigger the following: Pin11 Pin11 Pin11 Pin2 Pin12 = 0001 Pin11 Pin11 Pin11 Pin2 Pin12 = 0002

The enter will be at the end of each number.

Does this make a bit more sense?

Does this make a bit more sense?

Not really although it might be because I've just woken up...

Mowcius

I’m also completely lost as to what you’re trying to accomplish… lol my best guess right now, is you want to count from 0 - 10000 and print that.

What is all this with counting to 10,000 with the pins? That I don’t understand. :stuck_out_tongue: Are you using 7 segment displays? Are you using LEDs and want to show the binary number?

Everything is labeled “pin#” so no real idea… it’s always a good idea to name it what it’s actually using… like
const byte TRANS_PIN = 13;
const byte LED_1 = 2;
const byte motorON = 4;
or something of the sorts. :stuck_out_tongue:

And if you want to count to 10,000, like I’m guessing… but still no idea where pins come into play lol… try the for link like was suggested previously, here’s an example of counting to 10,000 in the Serial Monitor:
We do it in setup so it’s done only once. We don’t need to keep printing to 10,000, unless you do, move it to the loop() function.

void setup()
{
Serial.begin(9600); // start serial connection

for(int x = 0; x <= 10,000; x++) // create an integer value 'x', as long as x is less than or equal to 10,000, x will increment by one each time.(x + 1 is the same as x++)
   {
        Serial.println(x);
   }
}


void loop()
{
//don't need anything here
}

It has to be done with pins. :( Using it for an led display. It has to be done this way. Already have everything tested and ready to go. I just need the program to count in that way. So rather than write digitalWrite a hundred thousand times, I was wondering if the program can do this for me.

I guess you can use the counter thing that prints the 1 to 10000 and say something like

instead of printing 1 do this= digitalWrite(Pin2, HIGH); delay(1000); digitalWrite(Pin2, LOW); delay(1000)

instead of printing 2 do this = digitalWrite(Pin3, HIGH); delay(1000); digitalWrite(Pin3, LOW); delay(1000)

etc, so everytime 1 is displayed, whether in "1" or "2001" or "1111" it knows that 1 will trigger = digitalWrite(Pin2, HIGH); delay(1000); digitalWrite(Pin2, LOW); delay(1000)

etc...

Using it for an led display

Ahh, so you are trying to print the numbers to an LED display but becuase it is an LED display, you need to turn on lots of outputs rather than just one. Correct?

Mowcius

You could put the code for each digit in a subroutine:

void show1()
{
   digitalWrite(Pin2, HIGH);
   delay(1000);
   digitalWrite(Pin2, LOW);
   delay(1000);
}

Repeat for the other 9 digits. Then, create a loop:

int num, digit1, digit2, digit3, digit4;
for(int i=0; i<10000; i++)
{
   num = i;

   digit1 = num % 1000;
   num -= digit1 * 1000;

   digit2 = num % 100;
   num -= digit2 * 100;

   digit3 = num % 10;
   num -= digit3 * 10;

   digit4 = num;
   
   // Now display the number
   switch(digit1)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
// Add cases for the remaining values
   }

   // Add switch blocks for the other digits
}

The switch blocks could be replaced with a subroutine:

void showDigit(int digit)
{
   switch(digit)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
// Add cases for the remaining values
   }
}

So should this completed code start automatically counting from 0-10000 or do I have to trigger it somehow? Did I get the code right?

//The led triggers
const int Pin2 = 2;
const int Pin3 = 3; 
const int Pin4 = 4;
const int Pin5 = 5;
const int Pin6 = 6;
const int Pin7 = 7;
const int Pin8 = 8;
const int Pin9 = 9;
const int Pin10 = 10;
const int Pin11 = 11;
const int Pin12 = 12;

void setup() {
  // initialize the pins as an output:
  pinMode(Pin2, OUTPUT);  
  pinMode(Pin3, OUTPUT); 
  pinMode(Pin4, OUTPUT); 
  pinMode(Pin5, OUTPUT); 
  pinMode(Pin6, OUTPUT); 
  pinMode(Pin7, OUTPUT); 
  pinMode(Pin8, OUTPUT); 
  pinMode(Pin9, OUTPUT); 
  pinMode(Pin10, OUTPUT); 
  pinMode(Pin11, OUTPUT); 
  pinMode(Pin12, OUTPUT); 
 
}

void show0()
{
   digitalWrite(Pin2, HIGH);
   delay(1000);
   digitalWrite(Pin2, LOW);
   delay(1000);
} 
void show1()
{
   digitalWrite(Pin3, HIGH);
   delay(1000);
   digitalWrite(Pin3, LOW);
   delay(1000);
} 
void show2()
{
digitalWrite(Pin4, HIGH);
delay(1000);
digitalWrite(Pin4, LOW);  
delay(1000);
} 
void show3()
{
digitalWrite(Pin5, HIGH);
delay(1000);
digitalWrite(Pin5, LOW);  
delay(1000);
} 
void show4()
{
digitalWrite(Pin6, HIGH);
delay(1000);
digitalWrite(Pin6, LOW);  
delay(1000);
} 
void show5()
{
digitalWrite(Pin7, HIGH);
delay(1000);
digitalWrite(Pin7, LOW);  
delay(1000);
} 
void show6()
{
digitalWrite(Pin8, HIGH);
delay(1000);
digitalWrite(Pin8, LOW);  
delay(1000);
} 
void show7()
{
digitalWrite(Pin9, HIGH);
delay(1000);
digitalWrite(Pin9, LOW);  
delay(1000);
} 
void show8()
{
digitalWrite(Pin10, HIGH);
delay(1000);
digitalWrite(Pin10, LOW);  
delay(1000);
} 
void show9()
{
digitalWrite(Pin11, HIGH);
delay(1000);
digitalWrite(Pin11, LOW);  
delay(1000);
} 
void show10()
{
digitalWrite(Pin12, HIGH);
delay(1000);
digitalWrite(Pin12, LOW);  
delay(1000);
}
void loop(){
 
  int num, digit1, digit2, digit3, digit4;
for(int i=0; i<10000; i++)
{
   num = i;

   digit1 = num % 1000;
   num -= digit1 * 1000;

   digit2 = num % 100;
   num -= digit2 * 100;

   digit3 = num % 10;
   num -= digit3 * 10;

   digit4 = num;

   // Now display the number
   switch(digit1)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
}
}
void showDigit(int digit)
{
   switch(digit)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;
   

} 

}

I have a feeling I had an option for the switch(digit) thing but put them both in, not sure which ones right… Also will this add the Pin12 (Enter) trigger after each 4 digit number? 0001 Enter, 0002 Enter, 0003 Enter, etc. where Enter is pin12.

It should start counting automatically. You did have a choice of ways of implementing the switch statement.

As the code it, only one digit gets displayed (the first one) which will be 0 for a long time.

I kind of figured that. I put this code in now and am building the test circuit as we speak, i mean type :)

   digit4 = num;

   // Now display the number
   switch(digit1)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
   
     switch(digit2)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
   
     switch(digit3)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
   
     switch(digit4)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
}
}

I am getting some weird results

I connected my arduino to 4 led’s for testing

Pin2 = 0
Pin3 = 1
Pin4 = 2
Pin12 = 10 (Enter)

When I start the program with everything connected it will go:

Pin2 Pin2 Pin2 Pin2 (ok so far) Pin3 Pin3 and then stop for a bit and then do some random other stuff but it won’t do Pin1 Pin1 Pin1 every few seconds like it should to count to 0001 0002 0003 0004 etc.

So it basically goes like this

000011 (no delays)
waits a several seconds
enter
0 0 0 0
0 1
0 1
0 1
0 0 1 1
0 0 0 1
0 1

With several delays in between…

It should go
0001 Enter
0002 Enter
0003 Enter
etc.

//The led triggers
const int Pin2 = 2; // 0
const int Pin3 = 3;  //1
const int Pin4 = 4; //2
const int Pin5 = 5;
const int Pin6 = 6;
const int Pin7 = 7;
const int Pin8 = 8;
const int Pin9 = 9;
const int Pin10 = 10;
const int Pin11 = 11;
const int Pin12 = 12; //enter

void setup() {
  // initialize the pins as an output:
  pinMode(Pin2, OUTPUT);  
  pinMode(Pin3, OUTPUT); 
  pinMode(Pin4, OUTPUT); 
  pinMode(Pin5, OUTPUT); 
  pinMode(Pin6, OUTPUT); 
  pinMode(Pin7, OUTPUT); 
  pinMode(Pin8, OUTPUT); 
  pinMode(Pin9, OUTPUT); 
  pinMode(Pin10, OUTPUT); 
  pinMode(Pin11, OUTPUT); 
  pinMode(Pin12, OUTPUT); 
 
}

void show0()
{
   digitalWrite(Pin2, HIGH);
   delay(1000);
   digitalWrite(Pin2, LOW);
   delay(1000);
} 
void show1()
{
   digitalWrite(Pin3, HIGH);
   delay(1000);
   digitalWrite(Pin3, LOW);
   delay(1000);
} 
void show2()
{
digitalWrite(Pin4, HIGH);
delay(1000);
digitalWrite(Pin4, LOW);  
delay(1000);
} 
void show3()
{
digitalWrite(Pin5, HIGH);
delay(1000);
digitalWrite(Pin5, LOW);  
delay(1000);
} 
void show4()
{
digitalWrite(Pin6, HIGH);
delay(1000);
digitalWrite(Pin6, LOW);  
delay(1000);
} 
void show5()
{
digitalWrite(Pin7, HIGH);
delay(1000);
digitalWrite(Pin7, LOW);  
delay(1000);
} 
void show6()
{
digitalWrite(Pin8, HIGH);
delay(1000);
digitalWrite(Pin8, LOW);  
delay(1000);
} 
void show7()
{
digitalWrite(Pin9, HIGH);
delay(1000);
digitalWrite(Pin9, LOW);  
delay(1000);
} 
void show8()
{
digitalWrite(Pin10, HIGH);
delay(1000);
digitalWrite(Pin10, LOW);  
delay(1000);
} 
void show9()
{
digitalWrite(Pin11, HIGH);
delay(1000);
digitalWrite(Pin11, LOW);  
delay(1000);
} 
void show10()
{
digitalWrite(Pin12, HIGH);
delay(1000);
digitalWrite(Pin12, LOW);  
delay(1000);
}
void loop(){
 
  int num, digit1, digit2, digit3, digit4;
for(int i=0; i<10000; i++)
{
   num = i;

   digit1 = num % 1000;
   num -= digit1 * 1000;

   digit2 = num % 100;
   num -= digit2 * 100;

   digit3 = num % 10;
   num -= digit3 * 10;

   digit4 = num;

   // Now display the number
   switch(digit1)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
   
     switch(digit2)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
   
     switch(digit3)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
   
     switch(digit4)
   {
       case 0:
          show0();
          break;
       case 1:
          show1();
          break;
       case 2:
          show2();
          break;
       case 3:
          show3();
          break;
       case 4:
          show4();
          break;
       case 5:
          show5();
          break;
       case 6:
          show6();
          break;
       case 7:
          show7();
          break;
       case 8:
          show8();
          break;
       case 9:
          show9();
          break;
       case 10:
          show10();
          break;

   }
}
}

They way you have this hardware set up, when i is 0, the led on pin 2 should go on for one second, then off for one second, to display the 1st 0 in 0000. Then, it should go one for one second and off for one second, to display the second 0 in 0000. Repeat for the 3rd and 4th 0's in 0000.

Then, i should be incremented to 1, and the led on pin 2 should go on for one second, and off for one second, three times. Then the led on pin 3 should go on for one second, and then off.

Seems like a strange way of displaying values, but, hey, it's your code.

I'm not sure how the above matches what you are actually seeing, since I don't understand this:

So it basically goes like this

000011 (no delays) waits a several seconds enter 0 0 0 0 0 1 0 1 0 1 0 0 1 1 0 0 0 1 0 1

With several delays in between...

By the way, show10 will never get called. None of the digits (digit1, digit2, digit3, or digit4 are ever 10.

For the sake of figuring out what the hecks going on I changed the cases to Serial.print the number instead of digitalWrite

void show0()
{
Serial.print("0");
delay(200);
} 
void show1()
{
Serial.print("1");
delay(200);
} 
void show2()
{
etc etc.

and got the following results in serial monitor:

1234567890123456789012001123456789000123456789012345678901230011223456789012003456789012345678901234500112233445678901230045678901234567890123456001122334455678901234006789012345678901234567800112233445566778901234560007890123456789012345678900112233445566778890123456700090123456789012345678900112233445566778899 Enter
023456789000123456789012345678900112233445566778899 Enter
01345678900023456789012345678900112233445566778899 Enter
01235678901200456789012345678900112233445566778899 Enter
0123478901230056789012345678900112233445566778899 Enter
012345689012345000789012345678900112233445566778899 Enter
0123456701234560009012345678900112233445566778899 Enter
012345678012345678000012345678900112233445566778899 Enter
0123456789034567890002345678900112233445566778899 Enter
012345678901567890000112233445566778899 Enter
0 Enter
000123456789012345678901230011223456789010023456789012345678901234001122334567890120045678901234567890123456001122334455678901234005678901234567890123456700112233445566789012345

It should be printing:

0001 Enter
0002 Enter
0003 Enter
0004 Enter
0005 Enter
0006 Enter
0007 Enter
0008 Enter
0009 Enter
0010 Enter
0011 Enter

etc. all the way up to 9999 and then reset back to 0000. Not quite sure what to do :(

and in regards to "By the way, show10 will never get called. None of the digits (digit1, digit2, digit3, or digit4 are ever 10. " I basically need an Enter pin trigger to go off after every number

0000 Enter trigger 0001 Enter trigger

Might not have to make it a case but definitely need that 10th trigger to come after every result for the application.

Pin2 = 0
Pin3 = 1
Pin4 = 2
Pin12 = 10 (Enter)

Ok…
I think I actually understand what you are trying to do now…
It really isn’t clear…

So you want it to count to 1000, and you want it to go up one number every 4 seconds (1 second for each character)

Then you want to display each character by lighting up an LED from 0-9, then light up an LED after each ‘number’ to show that the number has finished (the enter LED or a trigger but an LED for testing…).

Correct me if i’m wrong?

Mowcius

There is a rule when writing software.

If you find yourself writing nearly the same lines over and over you are doing something drastically wrong.

However, as what you actually want to do still remains shrouded in mystery despite me following this thread all day you will have to put up with it.

All the posters are trying to be helpful and modifying code that is fundamentally bad.

So to start off rather than have lots of functions called show0(), show1() and so on you need only one function show(val) and pass it a parameter val to indicate what number to show.

void show(val){ Serial.print(val); delay(200); }

The instead of a huge case statement you simply call the show() function with the correct value passed to it.

Look up on how to use arrays, these are ways of making a number respond to an index and an index can be a program variable.

But please please please say what the heck you are trying to do.

However, as what you actually want to do still remains shrouded in mystery despite me following this thread all day you will have to put up with it.

All the posters are trying to be helpful and modifying code that is fundamentally bad.

But please please please say what the heck you are trying to do.

;D

Ok…
I think I actually understand what you are trying to do now…
It really isn’t clear…

So you want it to count to 1000, and you want it to go up one number every 4 seconds (1 second for each character)

Then you want to display each character by lighting up an LED from 0-9, then light up an LED after each ‘number’ to show that the number has finished (the enter LED or a trigger but an LED for testing…).

Correct me if i’m wrong?

Mowcius

Exactly, except I need it to count to 9999 not 1000. But yes. Everything is working except for the counting. Pauls posted the code that I thought would do it but it counts wrong = “12345678901234567890120011234567890001234567” so I just need it to count properly:
0001 Enter
0002 Enter
0003 Enter

so 5 results each round, enter is also a charactor just like 0 1 2 etc, just named as enter and put after every count. I have it set in serial.print for testing and have the digitalwrite script working fine so once i get it working in serial then I will swap it back and I am good to go :slight_smile:

I am assuming the problem lies somewhere in this code. Everything else is fine:

  int num, digit1, digit2, digit3, digit4;
for(int i=0; i<10000; i++)
{
   num = i;

   digit1 = num % 1000;
   num -= digit1 * 1000;

   digit2 = num % 100;
   num -= digit2 * 100;

   digit3 = num % 10;
   num -= digit3 * 10;

   digit4 = num;