fixed value changes when first digit is 0

Hey guys,

I had some help here with my code, and just when i thought it worked, another problem arises...

Short explenation is that i make a display work a specific way with a external value.

Since the display didnt show the digits i expected, i made a "fixed" value in my program, instead of a external being read in.

For example, when my "fixed" value is 3546, everything works.
But when my value is 0546, the value in my program is adjusted. (the external value can send value up to 1023, so below 1000 the first digit is always 0)
The printed value is then 358, and the display shows 358 instead of (0)546.

Here is the code:

#include <stdlib.h>

int counter = 0;
bool clockpulse;
int analogValue = 1546;
uint8_t num1 = 0;
uint8_t num2 = 0;
uint8_t num3 = 0;
uint8_t num4 = 0;



void setup() {
  // put your setup code here, to run once:
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:



  if (clockpulse, HIGH) {  //clockpuls counter

    counter ++ ;
  }

  digitalWrite(clockpulse, LOW);  // 1ms clock LOW
  digitalWrite(11, LOW);

  if (counter == 36) {     //reset counter
    digitalWrite(13, HIGH);
    delayMicroseconds(100);
    digitalWrite(13, LOW);
    counter = 0;
  }
  Serial.print("counter: ");
  Serial.println(counter, DEC);


  if (counter == 25) {
    //analogValue = analogRead(1);   //read analog value and split up the digits
    Serial.println(analogValue);

    num1 = (analogValue / 1000) % 10;            // <== THIS IS WHERE I EXPECT THE FAULT TO BE
    num2 = (analogValue / 100) % 10;
    num3 = (analogValue / 10) % 10;
    num4 = (analogValue) % 10;


    int bcdNum = (num1 << 12) | (num2 << 8) | (num3 << 4) | num4; // convert numbers to BCD
    Serial.println(bcdNum, BIN);
  }

  for (uint8_t i = 0; i < 4; i++) {

    uint8_t bit = (num1 >> i) & 1;              //read BITS and send output if required
    //Some code here to use the bit
    if ((i == 0) && (bit == 1) && (counter == 6)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i0 = 1");
    }
    if ((i == 1) && (bit == 1) && (counter == 7)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i1 = 1") ;

    }
    if ((i == 2) && (bit == 1) && (counter == 8)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i2 = 1");
    }
    if ((i == 3) && (bit == 1) && (counter == 9)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i3 = 1");
    }
    Serial.print(bit);
    if (i < 3) Serial.print(",");

  }
  Serial.println();
 
  
  for (uint8_t i = 0; i < 4; i++) {                  //num2
    uint8_t bit = (num2 >> i) & 1;
    //Some code here to use the bit
    if ((i == 0) && (bit == 1) && (counter == 2)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i0 num2 = 1");
    }
    if ((i == 1) && (bit == 1) && (counter == 3)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i1 num2 = 1");
    }
    if ((i == 2) && (bit == 1) && (counter == 4)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i2 num2 = 1");
    }
    if ((i == 3) && (bit == 1) && (counter == 5)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i3 num2 = 1");
    }
    Serial.print(bit);
    if (i < 3) Serial.print(",");

  }
  Serial.println();

  
  for (uint8_t i = 0; i < 4; i++) {              //num3

    uint8_t bit = (num3 >> i) & 1;
    //Some code here to use the bit
    if ((i == 0) && (bit == 1) && (counter == 34)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i0 num3 = 1");
    }
    if ((i == 1) && (bit == 1) && (counter == 35)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i1 num3 = 1");
    }
    if ((i == 2) && (bit == 1) && (counter == 0)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i2 num3 = 1");
    }
    if ((i == 3) && (bit == 1) && (counter == 1)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i3 num3 = 1");
    }
    Serial.print(bit);
    if (i < 3) Serial.print(",");

  }
  Serial.println();

  
  for (uint8_t i = 0; i < 4; i++) {            //num4

    uint8_t bit = (num4 >> i) & 1;
    //Some code here to use the bit

    if ((i == 0) && (bit == 1) && (counter == 30)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i0 num4 = 1");
    }
    if ((i == 1) && (bit == 1) && (counter == 31)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i1 num4 = 1");
    }
    if ((i == 2) && (bit == 1) && (counter == 32)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i2 num4 = 1");
    }
    if ((i == 3) && (bit == 1) && (counter == 33)) {
      digitalWrite(12, HIGH);
      delayMicroseconds(50);

      Serial.println("i3 num4 = 1");
    }
    Serial.print(bit);
    if (i < 3) Serial.print(",");
  }
  Serial.println();

  digitalWrite(clockpulse, HIGH); //1ms clock HIGH
  digitalWrite(11, HIGH);
  delayMicroseconds(100);
  digitalWrite(12, LOW);   


}

What exactly are you driving? And what's up with that counter variable?

The problem is that whatever you're driving does nothing with a leading zero.

And two tips, give pins useful variable names, makes it a lot clearer.

And you do note that you almost do the same thing 4 times (for num1 to num4)? That screams for a function :wink:

DeathAspire:
Hey guys,

I had some help here with my code, and just when i thought it worked, another problem arises...

Short explenation is that i make a display work a specific way with a external value.

Since the display didnt show the digits i expected, i made a "fixed" value in my program, instead of a external being read in.

But when my value is 0546, the value in my program is adjusted. (the external value can send value up to 1023, so below 1000 the first digit is always 0)
The printed value is then 358, and the display shows 358 instead of (0)546.

'0' is the prefix for "octal number

similar as prefix '0x' is the prefix for a hexadecimal number.

Octal number 0546 is in decimal:588+4*8+6
Everything is OK.

If you want a decimal number, do NOT use a prefix like '0'(octal) or '0x'(hexadecimal) or '0b'(binary)!
Decimal numbers start with a digit in the '1'...'9' range only.
D

And you do note that you almost do the same thing 4 times (for num1 to num4)? That screams for a function

And an array!

Thx for the replies guys, the problem is fixed! :stuck_out_tongue:

The counter variable is used to send out data on the right clockpuls, since i can only send 1 bit/clockpuls.
So using the value that i read out, my display was wrong, because my data was being send on the wrong counter.

we "debug'd" this using a fixed digit, but then it indeed went haywire with the 0 in front of it.

Nothing wrong! :stuck_out_tongue:

Oh, and i'm still not used to functions, thats why i dont use them all to much :stuck_out_tongue:

DeathAspire:
The counter variable is used to send out data on the right clockpuls, since i can only send 1 bit/clockpuls.

Doesn't really make sense. Just send one bit and one clock, do that for all 24 bits and done!

DeathAspire:
Oh, and i'm still not used to functions, thats why i dont use them all to much :stuck_out_tongue:

That's stupid, there is absolutely nothing hard about functions. You use them all the time.

But he, done for today because you're not willing to give use more details :slight_smile:

Its a long story to explain what it is exactly that i am doing :smiley:
The part i have now is only part of the end result, in the end i will need all 36 counter cycles.

And i am learning as i go with this, it is all self-studie, so functions will come, just not yet :stuck_out_tongue:

I would say the right time to learn about functions already has passed... And really, it only takes 5 minutes tops which you now already spend on duplicating pieces of code over and over...

    int bcdNum = (num1 << 12) | (num2 << 8) | (num3 << 4) | num4; // convert numbers to BCD
    Serial.println(bcdNum, BIN);

BCD is much easier to read in HEX.

Be warned that storing a BCD value over 7999 in a signed integer will result in a negative number. Better to use an UNSIGNED int.