Monday, 27 October 2014

Creating A Command prompt In Arduino

Using an Arduino for turning on and off an LED is not a very big deal, I guess. But how about this:
You are basically telling your microcontroller to plain English statements to turn the green led on, turn the blue led on and so on. For it you simply require to know some few functions available in the arduino development environment. It is really cool. I personally believe that this is more futuristic than just commanding your arduino board with just a key on your keyboard. In real life we use command prompt in our desktop computers to workout our stuffs and there we use meaningful english commands like print run etc to make things work. people dont want a robot who has been design to raise his hands when say some number in the num [pad is pressed. Instead you might find your bot more humanistic if it works on commands like "Raise_Left_Hand". That sounds more mature from.automation point of view. So here is a small code for the arduino developers who wants a personalised small command prompt. This small code is just a quintessance of a bigger picture I just talked about. 
You basically require a breadboard, two leds, one green, one blue, some hook up wires and an arduino. 

int ledGreen = 7;            
int ledBlue = 8;

void setup()
{
  Serial.begin(9600);             // Setting up baud rate. We keeep the standard to be 9600
  pinMode(ledGreen, OUTPUT);      // LED Green pin set to output
  pinMode(ledBlue, OUTPUT);      // LED Blue pin set to output
}

void loop()
{
  if(Serial.available() > 0)             // Checking for the buffer for input value
  {
    String command;                      // Creating a string variuable
    while(Serial.available() > 0)         // creating a while loop for reading buffer
    {
      command += char(Serial.read());    // creating the string   
      delay(250);
      
      if (command == "GREEN_ON")              // Creting condition
      {
        digitalWrite(ledGreen, HIGH);
      }
      if (command == "GREEN_OFF")
      {
        digitalWrite(ledGreen, LOW);
      }
      if (command == "BLUE_ON")
      {
        digitalWrite(ledBlue, HIGH);
      }
      if (command == "BLUE_OFF")
      {
        digitalWrite(ledBlue, LOW);
      }
    }
  
  Serial.print(command);                         // printing command
  }
}




1 comment: