Pages: [1]   Go Down
Author Topic: bit of help need to make it work  (Read 190 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi i am very new to this ( but having great fun  smiley-grin  ) i am trying to blend to bit of code together to get a counter with 7 segment led display
this is were i have got to ...
I would like to know how to get the counter to display on the led up to 9 and then start again.
i can get the two bit to work but not together
i have use google but i am missing something

here is the code

Code:

int A = 7;   //Defines all pins on the Arduino Uno board in order of connection.
int B = 6;
int C = 4;  // DOT is pin 5, not used in this example.
int D = 3;
int E = 2;
int F = 8;
int G = 9;
      
byte num0 = 0x3F;  //Hexadecimal format based upon the A-G, 0-9 Chart in excel and the wiring      // of the segment (refer to the on/off table image below).
byte num1 = 0x6;
byte num2 = 0x5B;
byte num3 = 0x4F;
byte num4 = 0x66;
byte num5 = 0x6D;
byte num6 = 0x7C;
byte num7 = 0x7;
byte num8 = 0x7F;
byte num9 = 0x6F;
// this constant won't change:
const int  buttonPin = 12;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
  
void on(byte num)   // This function turns on the correct pins to display numbers passed to it         // through the variable "num".
{
  int result = bitRead(num, 0);  // Read the first binary entry in num and stores it in result.
  
      if (result == 1)  // Check to see if this segment should be on.
  
    {digitalWrite(A, HIGH);}   // Turns on the segment.
    else   // Otherwise, it turns it off.
    {digitalWrite(A, LOW);}  // Turns segment off.
  
    
      result = bitRead( num, 1);  // Same thing for the 6 remaining segments.
  
      if (result == 1)
  
    {digitalWrite(B, HIGH);}
    else
    {digitalWrite(B, LOW);}    
    result = bitRead( num, 2);
  
      if (result == 1)
  
    {digitalWrite(C, HIGH);}
    else
    {digitalWrite(C, LOW);}    
   result = bitRead( num, 3);
  
      if (result == 1)
  
    {digitalWrite(D, HIGH);}
    else
    {digitalWrite(D, LOW);}    
   result = bitRead( num, 4);
  
      if (result == 1)
    {digitalWrite(E, HIGH);}
    else
    {digitalWrite(E, LOW);}
    
   result = bitRead( num, 5);  
      if (result == 1)
    {digitalWrite(F, HIGH);}
    else
    {digitalWrite(F, LOW);}    
    
   result = bitRead( num, 6);
      if (result == 1)
    {digitalWrite(G, HIGH);}
    else
    {digitalWrite(G, LOW);}
  }
void setup() {       // Our setup routine        
  pinMode(A, OUTPUT); // Making all pins outputs
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(10,OUTPUT);
 
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
 
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      on(num0 (buttonPushCounter));
      
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
  
}
« Last Edit: March 14, 2014, 02:05:10 pm by martintennant » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (result == 1)
  
    {digitalWrite(B, HIGH);}
    else
    {digitalWrite(B, LOW);}
So much simpler
Code:
digitalWrite(B, result ==1);
Or even skip the result assignment altogether.
What happened to the code tags?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 31
Posts: 2453
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

FYI, the "#" CODE button is for posting code.
CLICK the MODIFY button.
HIGHLIGHT the CODE.
CLICK the "#" CODE button. It will create a scrolling window for scrolling through the code.
Logged

Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,
DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

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

thank you
Logged

Pages: [1]   Go Up
Jump to: