Wednesday, 2 July 2014

Controlling Arduino through the SERIAL MONITOR

This is a continuation to my previous project of Using 74HC595. In this project we interact with the arduino through the serial monitor.

Link To My Previous Project

THE SERIAL MONITOR:
The Serial Monitor is basically a tether between the computer and the arduino. It is marked with the red circle below.

New Keywords in the code:
  • Serial.begin(9600): It starts serial communication with the arduino at 9600. This 9600 is the baud rate which basically defines the speed of communication.
  • Serial.print("text to be printed on the serial monitor"): It is like the cout command of c++ 
  • Serial.println("txt followed by new line"): It prints the text and moves to a new line.
  • Serial.available() : It checks if the serial monitor is on (available)
CODE:



int DS_pin=8;
int SH_pin=10;
int ST_pin=9;

void setup()
{
  pinMode(DS_pin,OUTPUT);
  pinMode(ST_pin,OUTPUT);
  pinMode(SH_pin,OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter '0' to '7' or 'x' to clear");
}

byte registers;

void loop()
{
  char ch;
  int led;
  if(Serial.available() > 0)
  {  
    ch= Serial.read();
    if(ch >='0' && ch <='7')
    {
      led= ch- '0';
      bitSet(registers, led);
      Serial.print("turned on led");
     Serial.println( ch );
      writereg();
    }
    if(ch=='x')
    {
      registers=0;
      writereg();
      Serial.println("cleared");
    }
  }
}

void writereg()
{
  digitalWrite(ST_pin, LOW);
  shiftOut(DS_pin, SH_pin, LSBFIRST, registers);
  digitalWrite(ST_pin, HIGH);
}
------------------------------------------------------------------------------------------------------------
Remarks:

This is a self explanatory code. If the serial monitor is enabled, it prompts to enter a value from 0 to 7 or x. This is inputted as character. If the value is '0' to '7', then a variable "led" of integer datatype is assigned as the difference of ASCII  values of entered character and that of '0' and will turn on that led using bitSet( ).
If 'x' is entered , "registers" byte will be assigned to zero and the shift register is updated to "00000000". Thus, all LEDs will be off.



No comments:

Post a Comment