Skip to content

A simple command processor

So far, you have been creating sketches and uploading them where they run independently. You can also interact with the sketch while it is running by taking advantage of the (virtual) UART connection provided by the USB connection with the PC.

Set up the circuit as shown.

Copy and upload the code below, then open the serial monitor and check that the commands work as expected.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    const int ledPin = 13;             // Onboard LED
    const int REDLed    = 3;           // PINs for RGB LED
    const int GREENLed  = 4;
    const int BLUELed   = 5;

    const unsigned long baudrate = 9600;
    char command ;                    // Stores the command read from the serial channel

    void setup()                      // IO Initialisation
    {
       Serial.begin(baudrate);        // Identifies USB Virtual Comport (VCP)

       pinMode(ledPin, OUTPUT);       // Configure GPIO pins
       pinMode(REDLed,  OUTPUT);
       pinMode(GREENLed,OUTPUT);
       pinMode(BLUELed, OUTPUT);

       digitalWrite(REDLed, LOW);    // Turn the RGB Led OFF
       digitalWrite(GREENLed, LOW);
       digitalWrite(BLUELed, LOW);

     }    // end of setup

    void loop()
    {
     if  (Serial.available())         // Read command char from  VCP
      {
        command = (char)Serial.read();
        Serial.print("Command Received >> ");
        Serial.println(command);

        switch (command)
        {
          case  '1' : {                              // Red on
                         digitalWrite(REDLed, HIGH);
                         digitalWrite(GREENLed, LOW);
                         digitalWrite(BLUELed, LOW);
                         break;
                       }
           case  '2' : {                             // Green on
                         digitalWrite(REDLed, LOW);
                         digitalWrite(GREENLed, HIGH);
                         digitalWrite(BLUELed, LOW);
                         break;
                        }

            case  '3' : {                             // Blue
                          digitalWrite(REDLed, LOW);
                          digitalWrite(GREENLed, LOW);
                          digitalWrite(BLUELed, HIGH);
                         break;
                        }
            case  '4' : {                             // Yellow
                          digitalWrite(REDLed, HIGH);
                          digitalWrite(GREENLed, HIGH);
                          digitalWrite(BLUELed, LOW);
                          break;
                        }
            case  '5' : {                             // White
                          digitalWrite(REDLed, HIGH);
                          digitalWrite(GREENLed, HIGH);
                          digitalWrite(BLUELed, HIGH);
                          break;
                        }
             default  : {                               // Set LED OFF
                           digitalWrite(REDLed, LOW);
                           digitalWrite(GREENLed, LOW);
                           digitalWrite(BLUELed, LOW);
                        }

         }  // End processing received command


        digitalWrite(ledPin, HIGH);                   // Blink onboard LED to show activity
        delay(200);
        digitalWrite(ledPin, LOW);
        delay(200);
        Serial.println("\n << Command Complete >> ");
       }   // End of  serial data available

     }
  • Change the command set to 'A', 'B', 'C', 'D', ā€˜Eā€™ and, in addition to setting the RGB LED, get the Arduino to respond with a short message, say Processing Command X when commands are received. Use the terminal application Realterm to do the same thing ā€“ (it is much better than the Arduino Monitor).