Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

This program is supposed to act as a Decimal To Binary Converter, the LeD's acting as output. The problem is that this function returns Garbage Value

const int LED1 =1, LED2 =3, LED3 =5;
void setup()
{
  Serial.begin(9600);
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);
}
void loop()
{
  int x = 0;
  x = Serial.read();
  x = x-48;
  switch(x){
    case 0:
      digitalWrite (LED1, LOW);
      digitalWrite (LED2, LOW);
      digitalWrite (LED3, LOW);
      break;
    case 1:
      digitalWrite (LED1, HIGH);
      digitalWrite (LED2, LOW);    
      digitalWrite (LED3, LOW);
      break;
      case 2:
      digitalWrite (LED1, LOW);
      digitalWrite (LED2, HIGH);
      digitalWrite (LED3, LOW);
      break;
      case 3:
      digitalWrite (LED1, HIGH);
      digitalWrite (LED2, HIGH);
      digitalWrite (LED3, LOW);
      break;
      case 4:
      digitalWrite (LED1, LOW);
      digitalWrite (LED2, LOW);
      digitalWrite (LED3, HIGH);
      break;
      case 5:
      digitalWrite (LED1, HIGH);
      digitalWrite (LED2, LOW);
      digitalWrite (LED3, HIGH);
      break;
      case 6:
      digitalWrite (LED1, LOW);
      digitalWrite (LED2, HIGH);
      digitalWrite (LED3, HIGH);
      break;
      case 7:
      digitalWrite (LED1, HIGH);
      digitalWrite (LED2, HIGH);
      digitalWrite (LED3, HIGH);
      break;
  }
 Serial.write(x); 
}
share|improve this question
3  
If you want some help, you should at least put some effort in writing a decent question: What are you trying to achieve? What have you done so far? What do you expect your program to do? What is it doing instead? –  Edgar Bonet Jun 6 at 16:32

2 Answers 2

There are a couple of issues:

  • The function loop() doesn't return anything, by definition. Functions that do return a value return it with a statement like `return(someValue); Did you mean to say that it prints garbage on the terminal?

  • If you want to print a value on the terminal, you need to use the the Serial.print(someValue) function; Serial.write() sends a binary value and that is probably not what you wanted. The binary values 0 - 9 represent non-printing characters so I'm not surprised that the terminal printed some odd graphic characters.

      -
share|improve this answer

That massive program can be compressed into just a handful of lines:

const int LED1 = 1, 
          LED2 = 3, 
          LED3 = 5;

void setup() {
    Serial.begin(9600);
    pinMode (LED1, OUTPUT);
    pinMode (LED2, OUTPUT);
    pinMode (LED3, OUTPUT);
}

void loop() {
    if (Serial.available()) {
        int x = Serial.read();
        x = x - '0';
        digitalWrite(LED1, x & 0x01);
        digitalWrite(LED2, x & 0x02);
        digitalWrite(LED3, x & 0x04);
        Serial.print(x);
    }
}

Note the use of Serial.print() not Serial.write(). If you use Serial.write() then it prints the ASCII character associated with the number you feed it - in your case characters 0 to 7 (none of which are printable).

Also, unless you check for there actually being something available for you to read from the serial you'll only get a brief flicker of something meaningful (too fast for the eye to see) and then it will just return -1, which with 48 subtracted from it would be -49, which would be "complete garbage".

share|improve this answer
    
How does x & 0x01 works? –  Clarskon Jun 8 at 5:56
    
It's called a bitwise operation. I returns just the first bit of x, so 0 or 1. With 0x02 it returns the second bit, so 0 or 2. 0x04 returns the third bit, so 0 or 4. 0 is LOW, anything above 0 is HIGH. –  Majenko Jun 8 at 10:07
    
why do we use 0X04 and not use 0X03 if we want the third bit? –  Clarskon Jun 12 at 7:37
    
Because 0x03 is the first two bits combined. 1+2=3. Binary. –  Majenko Jun 12 at 8:17
    
So we are moving according to according to numbers that are 2^(something) ?? –  Clarskon Jun 12 at 8:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.