-3

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); 
}

closed as unclear what you're asking by gre_gor, sempaiscuba, MatsK, Dmitry Grigoryev, leoc7 Jan 22 at 16:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    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 '15 at 16:32
6

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

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

      -

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