binary to decimal converter using potentiometer and led

Hi,

Im in desperate need of help. I have a project where I need to convert a decimal input value to a binary value and display the binary number as the output.

The decimal number has to be between 0 and 1023, which is read with the call analogRead () from a potentiometer connected to my Arduino.
As an output I should light up 10 LED lights that symbolize each bit in number. An LED lamp symbolizes a piece of number.
I also have to print both the decimal value and the binary number to the console on the computer (in order to double check your system)

The potentiometer should be connected to the analog input A0 of your Arduino.

The LED lights should be connected to the digital outputs 2 to 11 on your Arduino.

So, until now I have manage to connect all the LEDs and potentiometer to board and microkontrollercard. But have I no idea how to code this so it works. Im attaching a ino. file of what I have come up with until now. PLEASEEE HELP!!!!

project2019BD.ino (510 Bytes)

Hint: the number is already binary.
Clue: try bitRead.

When do we have to hand in our assignment?

The deadline is on 14th March, I suck at coding, I didnt get your hint.... :cry: :fearful:

is something like this u after?

void setup()
{

  //initialse pins 2-11 as output and set them low
  for (int i = 2; i <= 11; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }

}

void loop()
{
  uint16_t x = analogRead(A0); //read pot input

  //output x value to pins 2-11. pin2 is bit0 and pin11 is bit9
  for (uint8_t i = 0; i <= 9; i++) {
    digitalWrite(i + 2, (x >> i) & 0x01); // i+2 : selects output pin
                                          //(x>>i)&0x01 : shift right 'x' by 'i' bits logical AND with '1' (0x01)
    delay(1000); //arbitrary 1s delay to see what's going on
  }

  delay(2000); //arbitrary 2s loop delay

}

khan_96:
The deadline is on 14th March, I suck at coding, I didnt get your hint.... :cry: :fearful:

The hint was that the number is already a binary value - there is no need for a conversion.

is something like this u after?

Hey, you could make good money on https://homeworkhelper.net/

Unless, of course, the student actually needs to LEARN something...

@OP

1. This is what you have as your hardware setup.
ledpotx.png
2. Bring the wiper of the Pot at a position so that you get about 3.45V. Measure the voltage with DVM and record it.

3. Connect the wiper point with A0-pin of the UNO.

4. Now, work on paper to see which Led should be ON, and which one should be OFF.
(1) Input Dc voltage = 3.45V.
(2) The voltage of Step-4.1 will pass through the 10-bit ADC of UNO. We expect to get the following bit pattern:
(1023/5)*3.45 = 706 = 1011000010

But, the ADC has about 2-bit error; so, the last 2-bit could be 00 or 01 or 10 or 10. Let us forget these last 2 bits. Now, the Led condition would be:

LED9  LED8  LED7  LED6  LED5  LED4  LED3  LED2  LED1  LED0
ON     OFF    ON     ON     OFF    OFF    OFF    OFF    dont---care

4. Let us save the bit pattern of Step-2 into variable x by executing the following instruction:

unsigned int x = analogRead(A0);

5. Now, let us do the following tasks:

Read bit-0 of x and store it onto Dpin-2 (digitalWrite(2, bitRead(x, 0));
..................................................................................................
Read bit-9 of x and store it onto DPin-11 (bitWrite(PORTB, 3, bitRead(x, 9));

3. Upload the following sketch which is a modified version of your program (untested)

void setup() 
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);  
  pinMode(5, OUTPUT);  
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);  
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{   
  unsigned x = ananlogRead(A0);
  for(int i=2, j=0; i<12, j<9; i+, j++)
  {
    digitalWrite(i, bitRead(x, j));
    delay(100); //for stability
  }
  /*
  for (int i = 2; i <= 11; i++) {
    digitalWrite(i, HIGH);
    delay(1 + analogRead(A0));
    digitalWrite(i, LOW);
  }
  for (int i = 11; i >= 2; i--) {
    digitalWrite(i, HIGH);
    delay(1 + analogRead(A0));
    digitalWrite(i, LOW);
  }
  */
  delay(2000);   //for smoothness
}

4. Check that the states/statuses of Leds agree with your calculation.

5. Vary the Pot, measure the voltage, do the calculation and check that Led states changes correctly.

ledpotx.png

 unsigned x = ananlogRead(A0);
for(int i=2, j=0; i<12, j<9; i+, j++)

Left, no doubt, as exercises for the reader.

I admire your subtle aids to tracing plagiarism, @GolamMostafa

AWOL:

 unsigned x = ananlogRead(A0);

for(int i=2, j=0; i<12, j<9; i+, j++)



Left, no doubt, as exercises for the reader.

I admire your subtle aids to tracing plagiarism, @GolamMostafa

You are always .................! But, the codes are 'untested' and even it was not put under the compiler -- this cannot be an excuse? This time, my good luck has not come at my rescue? (four mistakes; four penalties for me; four K++++ for you.)

I don't think thats what the assignment is about. The idea is that you should read the value from the potentiometer. To then convert that value to binary and light a line for each bit. You should therefore not convert a fixed number, without a number that is read from the potentiometer. If the value that the potentiometer reads is 267. Then the joints should be: Light on, Light off, Light off, Light off, Light off, light On light, light Off, light on, light on.

(100001011 = 263)

khan_96:
I don't think thats what the assignment is about. The idea is that you should read the value from the potentiometer. To then convert that value to binary and light a line for each bit. You should therefore not convert a fixed number, without a number that is read from the potentiometer. If the value that the potentiometer reads is 267. Then the joints should be: Light on, Light off, Light off, Light off, Light off, light On light, light Off, light on, light on.

(100001011 = 263)

so I hope post #3 was of help to you :slight_smile:

khan_96:
To then convert that value to binary

You're still not getting it.

The number is already in binary; the processor knows no other number base.

(100001011 = 263)

Back to Binary:101 for you.

(Or should that be be "Binary:1100101" ?)

okay, can you please explain to me what exactly did @sherzaad on post#3 so I understand the coding part. Thanks..... @AWOL

But, seriously,

(the best explanation I know of about different number bases)

There is more to this project than just the conversion. You also need to know how to write the result to the Serial monitor, and display the result on the lights.

Do you know how to use the Serial monitor? Do you know how to use Serial.print ?

You want the result in the form of a 10-bit binary number.
What decimal number will give you 1000000000 in binary? (Notice that I have written that number with exactly ten bits.)
What decimal number will give you 1111111111 in binary?
What to do if the conversion gives you fewer than ten bits? For example, suppose that the binary number is 101011. What to do then?

khan_96:
I don't think thats what the assignment is about. The idea is that you should read the value from the potentiometer. To then convert that value to binary and light a line for each bit. You should therefore not convert a fixed number, without a number that is read from the potentiometer.

Indeed, you need to read the value before doing any conversion!

[/quote]
If the value that the potentiometer reads is 267. Then the joints should be: Light on, Light off, Light off, Light off, Light off, light On light, light Off, light on, light on.
[/quote]
How many LEDs is that again?

(100001011 = 263)

That conversion is incorrect. I think you meant 267, not 263.
But, either way, that's only nine bits. But we have ten LEDs. So, what do we do?

No, don't know how to use serial.print.... @dometer

odometer:
Indeed, you need to read the value before doing any conversion!

If the value that the potentiometer reads is 267. Then the joints should be: Light on, Light off, Light off, Light off, Light off, light On light, light Off, light on, light on.

How many LEDs is that again?
That conversion is incorrect. I think you meant 267, not 263.
But, either way, that's only nine bits. But we have ten LEDs. So, what do we do?

i dont you, u tell me. What do I do?

GolamMostafa:
@OP

1. This is what you have as your hardware setup.
ledpotx.png
2. Bring the wiper of the Pot at a position so that you get about 3.45V. Measure the voltage with DVM and record it.

3. Connect the wiper point with A0-pin of the UNO.

4. Now, work on paper to see which Led should be ON, and which one should be OFF.
(1) Input Dc voltage = 3.45V.
(2) The voltage of Step-4.1 will pass through the 10-bit ADC of UNO. We expect to get the following bit pattern:
(1023/5)*3.45 = 706 = 1011000010

But, the ADC has about 2-bit error; so, the last 2-bit could be 00 or 01 or 10 or 10. Let us forget these last 2 bits. Now, the Led condition would be:

LED9  LED8  LED7  LED6  LED5  LED4  LED3  LED2  LED1  LED0

ON    OFF    ON    ON    OFF    OFF    OFF    OFF    dont---care




**4.** Let us save the bit pattern of Step-2 into variable __*x*__ by executing the following instruction:


unsigned int x = analogRead(A0);




**5.** Now, let us do the following tasks:


Read bit-0 of x and store it onto Dpin-2 (digitalWrite(2, bitRead(x, 0));
..................................................................................................
Read bit-9 of x and store it onto DPin-11 (bitWrite(PORTB, 3, bitRead(x, 9));




**3.** Upload the following sketch which is a modified version of your program (untested)


void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT); 
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()

  unsigned x = ananlogRead(A0);
  for(int i=2, j=0; i<12, j<9; i+, j++)
  {
    digitalWrite(i, bitRead(x, j));
    delay(100); //for stability
  }
  /*
  for (int i = 2; i <= 11; i++) {
    digitalWrite(i, HIGH);
    delay(1 + analogRead(A0));
    digitalWrite(i, LOW);
  }
  for (int i = 11; i >= 2; i--) {
    digitalWrite(i, HIGH);
    delay(1 + analogRead(A0));
    digitalWrite(i, LOW);
  }
  */
  delay(2000);  //for smoothness
}




**4.** Check that the states/statuses of Leds agree with your calculation.

**5.** Vary the Pot, measure the voltage, do the calculation and check that Led states changes correctly.

Not allowed to use bitread or sting in my coding. Other suggestions?

khan_96:
Not allowed to use bitread or sting in my coding. Other suggestions?

Try the following codes. If they work, reduce them using for() loop.

void loop() 
{
  digitalWrite(2, (bool)analogRead(A0)>>0 & 0x0001);
  digitalWrite(3, (bool)analogRead(A0)>>1 & 0x0001);
  digitalWrite(4, (bool)analogRead(A0)>>2 & 0x0001);
  digitalWrite(5, (bool)analogRead(A0)>>3 & 0x0001);
  digitalWrite(6, (bool)analogRead(A0)>>4 & 0x0001);
  digitalWrite(7, (bool)analogRead(A0>>5) & 0x0001);
  digitalWrite(8, (bool)analogRead(A0)>>6 & 0x0001);
  digitalWrite(9, (bool)analogRead(A0)>>7 & 0x0001);
  digitalWrite(10, (bool)analogRead(A0)>>8 & 0x0001);
  digitalWrite(11, (bool)analogRead(A0)>>9 & 0x0001);

  delay(1000);
}

If they work, reduce them using for() loop.

...and a single analogRead