Saturday, March 16, 2013

Gizduino lesson 4 » communicate with your Gizduino

You can program the Gizduino to do certain actions only when you type in a specified key to the serial monitor. It is also possible for you to read the data sent by the Gizduino. This experiment allows you to control which LED will turn on when you type in a certain key to the serial monitor, also you will be able to read which LED is on in the serial monitor.

You will need the following:

 











Schemaic Diagram:










Connect positive end of LED to pin 9 and negative end of LED to a 100 ohm resistor which is connected to the ground pin. Do the same to the other LEDs to any digital pin. I used digital pins 13, 11, and 7 for easier editing of the diagram.




Sketch, Compile, then Upload:

Copy the program below to your Arduino IDE.

int ledPin1 = 13;
int ledPin2 = 11;
int ledPin3 = 7;
int serial;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0)
  {
    serial = Serial.read();

    switch(serial)
    {
      case 'q':
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        Serial.println("ledPin1 on");
        break;

      case 'w':
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, LOW);
        Serial.println("ledPin2 on");
        break;

      case 'e':
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        Serial.println("ledPin3 on");
        break;  

      default:
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        Serial.println("all LEDs are off");
        break;
    }
  }
}

This program allows you to control which LED will turn with an enter of a key in the serial monitor. It also lets you know which LED is on through the serial monitor.



Code Overview:

Serial.begin(9600);
This function prepares the Gizduino for receiving and sending serial data. 9600 bits per second is the usual speed of data trasfer used in the Gizduino.

if (Serial.available() > 0)
This will let the Gizduino  that you have type something in the serial monitor.

serial = Serial.read();
The Gizduino retrieves a byte of Serial data using he Serial.read() function. Named as “serial” variable for convenience later on the program.

switch(serial)
This line specifies what variable the switch case statement refers to. In this case it refers to the “serial” variable which is the Serial.read() function.

case 'q':
digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  Serial.println("ledPin1 on");
  break;
In a switch case statement there are several directions depending on the value of the variable; in this case, the variable is the data you send to the Gizduino through the serial monitor. Every time the value of the variable matches one of the cases, the Gizduino executes all the instructions contained in the case. In the example above, everytime you key in the letter “q” to your serial monitor, ledPin1 will turn on, while the other LEDs turn off, also a text saying “ledPin1 on” will be posted in the serial monitor. The break; function separates the different sections of an switch case statement.

Serial.print(data, encoding);
Sends data from Gizduino to the serial port which can be read in the serial monitor. You have the option on what encoding scheme will be seen in the serial monitor. Serial.println has the same function with Serial.print, but has a line break everytime it is printed.

Example:

Serial.print(100);            // prints "100"
Serial.print(100, DEC);       // same as above
Serial.print(100, HEX);       // "64" (100 in hexadecimal)
Serial.print(100, OCT);       // "144" (100 in octal)
Serial.print(100, BIN);       // "1100100" (100 in binary)
Serial.print(100, BYTE);      // "d" (100 in ASCII set)

 

     

default:
digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  Serial.println("all LEDs are off");
  break;
When the value does not match any of the specified cases, the Gizduino will follow the instructions in the default command.







No comments:

Post a Comment