I need help on this task!

I tried a lot of codes and none of them worked! I need help with the code. I am new and this very advanced for me.

Here is what i need to do:

When the program starts, the number "0" appears in the display.
When the right button is pressed, the number increases by 1 to a maximum of "F" (hexadecimal).
When the left button is pressed, the number decreases by 1 to a minimum of "0". Pay attention to the bounce of the buttons – build in time delays.
image

Welcome.

When is this assignment due?

What have you tried? Post your best attempt and tell us what the code actually does and how that differs from what you want.

Read the forum guidelines to see how to properly post code and some good information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

If you have compile errors, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

What a weird suggestions...
Are you expecting that somebody write the code for you?
This is not the way that this forum is works.

Please read and follow the forum guidelines.

What codes did you try? What were the results?

Google "arduino 7 segment display 2 bugttons". I see nearly 1 million hits.

Some Hints:
1. Check that the diagram of Fig-1 agrees with your pictorial view.


Figure-1:

2. Start With a Blank Sketch like this:

void setup()
{

}

void loop()
{

}

3. Write codes using pinMode() function to set the directions DPin-10, DPin-11 as inputs and DPin-2 to DPin-8 as outputs and keep them under setup() function.

4. Create cc-codes (you are using CC-type 7-segment display unit) for the digits 0 - F using the following Table (Fig-2).


Figure-2:

5. The cc-codes computed in Section-4 can now be represented by the following array which could be declared at the top of setup() function to help you in getting the cc-codes for the digits to be displayed.

byte ccTable[] = 
{
   0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 	//0	1   2	3	4  	5  	6	7
   0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71	//8  9   A     B   C    D    E    F	
};

6. Write code to show 0 (initial value) on DP0 (DisPlay Number 0) and keep it in the setup() function.

//3F = 0011 1111 = is the cc-code for 0
byte y = 0b00111111; // y = y7 y6 y5 y4 y3 y2 y1 y0 = 0x3F;
digitalWrite(4, y0);
===> digitalWrite(4, bitRead(y, 0));
.........................
digitalWrite(2, bitRead(y, 6));

7. Use for() to reduce the number of lines of Section-6.

byte y = ccTable[0];   //y = 0x3F = 00111111
for(int i = 4; i < 9; i++)
{
    digitalWrite(i, bitRead(y, i));
}
digitalWrite(3, bitRead(y, 5));
digitalWrite(2, bitRead(y, 6));

8.
...pending.

So you need to start with simpler projects and build up to where this will be a challenge but not a major can't even think about how to begin other than finding it on the internet or getting some nice ppl to write it for me kinda thing.

You have to start with the basics. As should have your course of instruction if you are indeed a student.

a7

I did a small project like that. Count up and count down using push buttons. My reason .... I have a few single 7 segments and I want to try to light them up and practice my coding skills. I dont mind sharing my code. Before the coding is done, you have to determine if the 7 segments is common cathode or common anode. To light up a common cathode, a 1 / true / HIGH will light up, to light up a common anode, a 0 / false / LOW will light up. And the push buttons that I connected, a false / 0 / LOW is a button is pressed. So one of the raison the code that you have may not work is because it simply not matching your circuit, the way you connected. So check each segments to know what pins on the display will light up.

Here the code. It may help others new to Arduino to make the same circuit.

byte segs[7] = {12,11,10,9,8,7,6}; // a,b,c,d,e,f,g

byte button_one = 2;
byte button_two = 3;

boolean button_one_state;
boolean button_two_state;
boolean button_one_press;
boolean button_two_press;
boolean bits;

int counter;

// commun anode display - a zero will light up the segment - format GFEDCBA
// display - 0 1 2 3 4 5 6 7 8 9 A B C D E F
const byte numbers[16] = { B1000000, B1111001, B0100100, B0110000, B0011001, 
B0010010, B0000010, B1111000, B0000000, B0010000, B0001000, B0000011,
B1000110, B0100001, B0000110, B0001110 };

void setup()
{
  for (byte i=0;i<7;i++)
  {
    pinMode(segs[i], OUTPUT);
  }
  pinMode(button_one, INPUT);
  pinMode(button_two, INPUT);
  
  for (byte i=0;i<7;i++)
  {
    digitalWrite(segs[i], HIGH);
  }

  button_one_state = 1;
  button_two_state = 1;
  button_one_press = 0;
  button_two_press = 0;
  
  delay(2000);
  counter = 0;

}

void loop() 
{
  button_one_state = digitalRead(button_one);
  delay(50);
  button_two_state = digitalRead(button_two);
  delay(50);
  if (button_one_state == 0 && button_one_press == 0)
  {
    count_up();
    button_one_press = 1;
    button_two_press = 0;
  }
  else
  {
    if (button_one_press == 1)
    {
      count_up();
    }
  }
  if (button_two_state == 0 && button_two_press == 0)
  {
    count_down();
    button_two_press = 1;
    button_one_press = 0;
  }
  else 
  {
     if (button_two_press == 1)
     {
       count_down();
     }
  }
 
}

void count_up()
{
   if (counter>15) counter = 0;
  
  for (byte i=0;i<7;i++)
  {
     bits = bitRead(numbers[counter], i);
     digitalWrite(segs[i], bits);
  }
  delay(1000);
  
  counter++;
}

void count_down()
{
  if (counter<0) counter = 15;
  
  for (byte i=0;i<7;i++)
  {
     bits = bitRead(numbers[counter], i);
     digitalWrite(segs[i], bits);
  }
  delay(1000);
  
  counter--;
}

The key trick is to use arrays for the pins of the display and a look up table ( array of data ) for the 7 segment pattern. And to display the leds on the 7 segments, you use a for loop and use bitread function to read a bit of a byte - the data pattern that is the look up table of the array.

The same technique can be use to put 8 bits of data into a parallel data bus like a LCD display ( without using the library ). Instead reading a data array, you simply do a bitread of the byte you want to send to a 8 bits data bus. Same technique also for a 4 bits data bus, like a 74138 or other multiplex chip. But because of the lack of pins on the Arduino, it better off using a 74595 shift register ( use shiftOut function ) or PCF8574 a I2C chip that use the Wire library.

Here a 7 segments data table the I made. I made it for one of my clock projects to use as a reference to help place the proper data in the look up table.
seven_segment_data_table.pdf (96.5 KB)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.