Go Down

### Topic: Binary counter with trigger (Read 8547 times)previous topic - next topic

#### sam_cqdx

##### Sep 08, 2013, 06:56 am
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

Code: [Select]
`/*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 = 8To find out the total just add up all the valuesWe'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 handlingint 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/Debounceint reading;           // the current reading from the input pinint 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 toggledlong debounce = 200;   // the debounce time, increase if the output flickersbyte data = 0; // We'll use this byte to cast the counter intobyte mask = 1; // We'll use this byte kind of like the i in a standard for loopint counter = 0; // this will increment each time we push the buttonint x = 0; //we'll use this to get our index while iterating through the bytevoid 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++;  }}`

#### PaulS

#1
##### Sep 08, 2013, 05:05 pm
Quote
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.

#### sam_cqdx

#2
##### Sep 08, 2013, 09:58 pm
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

#### sam_cqdx

#3
##### Sep 09, 2013, 09:56 am
I am willing to throw in a few dollars for some beers ..for the one that can modify the code slightly for me :-)

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

Much appreciated

#### PaulS

#4
##### Sep 09, 2013, 11:12 am
Code: [Select]
`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.

Code: [Select]
`int inputPin = 12;  // choose the input pin (for a pushbutton)`
Making this an array, and adding 2 more pins isn't difficult.

Code: [Select]
`  for(int i=0;i++;i<4) pinMode(ledPins[i], OUTPUT); // make all the LED pins outputs`
Changing 4 to 12 isn't difficult.

Code: [Select]
`  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.

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

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

Can't even spell coding...

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.

#### bigmarck

#5
##### Oct 23, 2014, 10:18 pm
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]);
}
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
//
}

#### ttyman

#6
##### Feb 05, 2015, 09:45 pm
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.

#### Bainesbunch

#7
##### Feb 20, 2015, 03:37 pm

Code: [Select]
`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.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt

Go Up