Binary counter with trigger

Hi There

I would like to know if any of you brilliant guys out there could help me get this code to do the following ..
What i would like to have is 3 separate binary counters (4 bits) with 3 separate triggers respectively
1 trigger = 4 bits (3 triggers =12 bits ) ...so that the binary counter moves up on binaries and starts back at 0000 and so on with each of the separate "channels"
I have found this code but need it modified for three instances of the same thing for a mega 2560 arduino
I would be able to throw in a few beers for someone who could modify this excisting code for me thanks

/*
How do you make 4 LED's count by 1 up to 15? Use Binary!
  first LED = 1
 second LED = 2
  third LED = 4
 fourth LED = 8
To find out the total just add up all the values

We're going to use a byte to represent the number of times the push button has been pressed because we can then loop trhough that byte and light up a coresponding LED. 
The reason that binary counting is great is because it's compact. If we try to count in decimal we would need 15 LEDs and use up 15 pins on the arduino 
To be able to keep the same score that we can using 4 pins/LEDs in binary!
Also you're going to find it usefull to know how to manipulate bytes for when you start doing more complex projects. 
*/

int ledPins[] = {2,3,4,5}; //Lets just put all our LED pins in an array for easy handling

int inputPin = 12;  // choose the input pin (for a pushbutton)
//the next four variables are four debouncing the button, you can read all about it here:
// http://www.arduino.cc/en/Tutorial/Debounce
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

byte data = 0; // We'll use this byte to cast the counter into
byte mask = 1; // We'll use this byte kind of like the i in a standard for loop
int counter = 0; // this will increment each time we push the button
int x = 0; //we'll use this to get our index while iterating through the byte


void setup() {
  for(int i=0;i++;i<4) pinMode(ledPins[i], OUTPUT); // make all the LED pins outputs
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  reading = digitalRead(inputPin);  // read input value
  //This next line might look a little funny, but this is just checking if the button is pressed
  //See the debounce tutorial for more info on how/why
  if (reading == LOW && previous == HIGH && millis() - time > debounce) {  
    counter++; //The button was pressed so we increment the counter by 1
    time = millis(); // This is for debounce
  }
  previous = reading; // This is for debounce
  data = byte(counter); //this is where we take the counter holding all the button presses and turn it into a byte
  x = 0; // We'll use this variable to get the current position of the bitmask
  for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask. Read more about how this works here: http://www.arduino.cc/en/Tutorial/BitMask
    if (data & mask){ // if bitwise AND resolves to true
      digitalWrite(ledPins[x],HIGH); //Here x represents the current postion of the bitmask iteration, we use it to turn on the coresponding LED
    }
    else digitalWrite(ledPins[x],LOW); // If bitwise AND resolves to false turn the LED off
    x++;
  }
}

What i would like to have is 3 separate binary counters (4 bits) with 3 separate triggers respectively

What have you tried? That code is so over-commented an 8 year old could follow it.

Hi Paul

Thanks for the message .Would anybody be so kind to help me with modifying the code mentioned above so that i can have the 3 separate counter with there respective triggers ...instead of just one channel

I am not a coder but would just modifing the loop and void areas i cannot do.

Anything would be appreciated

Thank you

I am willing to throw in a few dollars for some beers ..for the one that can modify the code slightly for me :slight_smile:

Well codding is not my strong point but maybe someone out there would be willing to help out.

Much appreciated

int ledPins[] = {2,3,4,5}; //Lets just put all our LED pins in an array for easy handling

Adding 8 more pins isn't difficult.

int inputPin = 12;  // choose the input pin (for a pushbutton)

Making this an array, and adding 2 more pins isn't difficult.

  for(int i=0;i++;i<4) pinMode(ledPins[i], OUTPUT); // make all the LED pins outputs

Changing 4 to 12 isn't difficult.

  pinMode(inputPin, INPUT);     // declare pushbutton as input

Doing this in a loop, just like the above loop, isn't difficult.

Adding a for loop in the loop() function is not difficult.

I am willing to throw in a few dollars for some beers ..for the one that can modify the code slightly for me :slight_smile:

Well codding is not my strong point but maybe someone out there would be willing to help out.

Can't even spell coding... 8)

PM me, telling me which pins you want to use for the 8 additional LEDs and 2 additional switches, and I'll modify the code and e-mail it to you.

i have done my own version of the binary clock with six bit. it counts from 1 to 64. the LED are connected from PIN 2 to PIN 8.
I have used an array and a subroutine to translate the first Less Significant Bit of the integer into HIGH and LOW values.

here the code

const int switchPin =8 ;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int tempo;
int LED[6]; // Array of 6 elements. I do not think this is correct, but I had no complaint from Arduino.
long interval =1000; // one second step

void setup() {

for(int x=2;x<8;x++){
tempo=0;
pinMode(x,OUTPUT);
}

pinMode(switchPin,INPUT);
}

void loop() {

unsigned long currentTime = millis();

if( currentTime - previousTime> interval ) {
previousTime = currentTime;
tempo=tempo++;
}

for(int x=2;x<8;x++){
if(bitAt(tempo,x-2)){
LED[x-2]=HIGH; }
else{ LED[x-2]=LOW;
}
digitalWrite(x,LED[x-2]);
}
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState){
for (int x=2 ; x<8 ; x++){
digitalWrite(x, LOW);
}
previousTime= currentTime;
}
prevSwitchState=switchState;

}

boolean bitAt(int c,int pointer) { //I borrowed this routine somewhere
return((c&(1<<pointer))!=0); //see bitwise operation on WIKI for this
//
}

I am building a board that uses an old 4/16 decoder (CD4514b). Why, you ask? Because after 30+ years of electronics tinkering I've discovered the Arduino and want to use some of my obsolete chips. I have all kinds of old IC's and yes, I've bought the newer 74hc595 and pcf8574. I like to prototype and build, solder, or wirewrap h/w projects but come up short in programming. I've got a board built out with the 4 of 16 decoder being fed from 4 binary inputs off the Arduino Uno and the decoder feeds 16 transistors I've soldered to a 40 pin header. I can send a picture of the layout if interested. What I want to do is modify the code above so that I can use the serial monitor or my android tablet via a bluetooth module and select 1 thru 16 and have that outputted in binary to the 4/16 decoder to turn on that specific port. For this project I'm using 2n6426 darlington transistors ( again because I have a few dozen to burn up). I've been trying to read about write, and serial.write and things like char ch = serial.read(); if (isDigit(ch)) <is ch a number?> and if (ch >= '0' && ch <= '16' and stuff like that, but it seems there a lot of ways to accomplish the same thing and I get bogged down. Any suggestions?
All I really have for code is a sample sketch that counts 0 to 15 over and over, it doesn't take keyboard input.

loop
{

for(uint8_t nLoop = 0;nLoop < 16;nLoop++)
{

// your code to take nLoop 0-15 and do what you want with it.

}


}

or am i missing something

Cheers Pete.