Pages: [1]   Go Down
Author Topic: Two Byte (Serial)  (Read 333 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Jr. Member
**
Karma: 1
Posts: 55
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I program 2 bytes?

but not works smiley-sad

if (val == 10){
    digitalWrite(LED1, LOW);
  }
  else if (val == 11)
  {
  digitalWrite(LED1, HIGH);
}


Here Code:
Code:
int LED1 = 9;
int LED2 = 8;
int LED3 = 7;
int LED4 = 6;
int LED5 = 5;
int LED6 = 4;
int LED7 = 3;
int LED8 = 2;

void setup(){
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
}

void loop(){
  while (Serial.available() == 0);
  int val = Serial.read();
  
  //-----------------------------------------------LED1
  if (val == 10){
    digitalWrite(LED1, LOW);
  }
  else if (val == 11)
  {
  digitalWrite(LED1, HIGH);
}

  //-----------------------------------------------LED2
  if (val == 20){
    digitalWrite(LED2, LOW);
  }
  else if (val == 21)
  {
  digitalWrite(LED2, HIGH);
}

  //-----------------------------------------------LED3
  if (val == 30){
    digitalWrite(LED3, LOW);
  }
  else if (val == 31)
  {
  digitalWrite(LED3, HIGH);
}

  //-----------------------------------------------LED4
  if (val == 40){
    digitalWrite(LED4, LOW);
  }
  else if (val == 41)
  {
  digitalWrite(LED4, HIGH);
}

  //-----------------------------------------------LED5
  if (val == 50){
    digitalWrite(LED5, LOW);
  }
  else if (val == 51)
  {
  digitalWrite(LED5, HIGH);
}

  //-----------------------------------------------LED6
  if (val == 60){
    digitalWrite(LED6, LOW);
  }
  else if (val == 61)
  {
  digitalWrite(LED6, HIGH);
}

  //-----------------------------------------------LED7
  if (val == 70){
    digitalWrite(LED7, LOW);
  }
  else if (val == 71)
  {
  digitalWrite(LED7, HIGH);
}

  //-----------------------------------------------LED8
  if (val == 80){
    digitalWrite(LED8, LOW);
  }
  else if (val == 81)
  {
  digitalWrite(LED8, HIGH);
}
else
{
}
  Serial.flush();
}

Logged


Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34213
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Look up serial flush, it does not do what you think.

How are you inputting the serial data, normally it reads ASCII values so things like 10 and 11 are non printing control codes.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this code.
It's a start. The code could be a lot easier by calculating the led number instead of a switch statement for every value. Write A...H and a...h to the serial port to switch leds on and off.

Code:
// Example code, not tested.

int LED1 = 9;
int LED2 = 8;
int LED3 = 7;
int LED4 = 6;
int LED5 = 5;
int LED6 = 4;
int LED7 = 3;
int LED8 = 2;

void setup(){
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
}

void loop(){
  if( Serial.available() != 0) {
    int val = Serial.read();
 
    switch (val) {
    //-----------------------------------------------LED1
    case 'A':
      digitalWrite(LED1, LOW);
      break;
    case 'a':
      digitalWrite(LED1, HIGH);
      break;
    //-----------------------------------------------LED2
    case 'B':
      digitalWrite(LED2, LOW);
      break;
    case 'b':
      digitalWrite(LED2, HIGH);
      break;
    //-----------------------------------------------LED3
    case 'C':
      digitalWrite(LED3, LOW);
      break;
    case 'c':
      digitalWrite(LED3, HIGH);
      break;
    //-----------------------------------------------LED4
    case 'D':
      digitalWrite(LED4, LOW);
      break;
    case 'd':
      digitalWrite(LED4, HIGH);
      break;
    //-----------------------------------------------LED5
    case 'E':
      digitalWrite(LED5, LOW);
      break;
    case 'e':
      digitalWrite(LED5, HIGH);
      break;
    //-----------------------------------------------LED6
    case 'F':
      digitalWrite(LED6, LOW);
      break;
    case 'f':
      digitalWrite(LED6, HIGH);
      break;
    //-----------------------------------------------LED7
    case 'G':
      digitalWrite(LED7, LOW);
      break;
    case 'g':
      digitalWrite(LED7, HIGH);
      break;
    //-----------------------------------------------LED8
    case 'H':
      digitalWrite(LED8, LOW);
      break;
    case 'h':
      digitalWrite(LED8, HIGH);
      break;
    default:
      break;
    }
  }
}
Logged

Pages: [1]   Go Up
Jump to: