Pages: [1]   Go Down
Author Topic: Binary counter with trigger  (Read 2602 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
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++;
  }
}
   
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
int inputPin = 12;  // choose the input pin (for a pushbutton)
Making this an array, and adding 2 more pins isn't difficult.

Code:
  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:
  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... smiley-cool

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.
Logged

Pages: [1]   Go Up
Jump to: