Go Down

Topic: Two Byte (Serial) (Read 461 times) previous topic - next topic

DEAFBOY

How can I program 2 bytes?

but not works :(

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


Here Code:
Code: [Select]
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();
}

I'm deaf.

www.christian-elektronik.de

Grumpy_Mike

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.

Krodal

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: [Select]

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy