-4

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); 
}
1
  • 7
    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? Commented Jun 6, 2015 at 16:32

2 Answers 2

7

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".

6
  • How does x & 0x01 works? Commented Jun 8, 2015 at 5:56
  • 2
    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. Commented Jun 8, 2015 at 10:07
  • why do we use 0X04 and not use 0X03 if we want the third bit? Commented Jun 12, 2015 at 7:37
  • Because 0x03 is the first two bits combined. 1+2=3. Binary. Commented Jun 12, 2015 at 8:17
  • So we are moving according to according to numbers that are 2^(something) ?? Commented Jun 12, 2015 at 8:51
0

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.

      -

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.