Saturday, March 4, 2017

Use PC to Control or Read via Arduino

Use PC to Control or Read via Arduino

3/4/2017

Object: 
  Experience PC communicate with Arduino via COM.  Use command code to control LED on the Arduino, and read photo sensor value from Arduino too.  Interrupt the process as needed with a command code.

Arduino Setup:

  • Connect D9 ~ D13 with LED.
  • Connect Photo resistor to A1
  • Connect Vcc and GND




Demo 1 (control LED):

  • Below example shows control of control LED at pin 9, blanking 5 times with 200ms cycle time.
  • Command code: 9 5 200



Demo 2 (Read photo sensor value):

  • Read the photo sensor value from A1 10 time with 200ms cycle time.
  • Command code: 1 10 200





Arduino Code:

/*
By Samson Yang 3/4/2017
This is a simple demo of how to use PC to control or read Arduino 'via' Arduino.

Arduino Setup:
Connect LED with current resistor (say 500ohm) to pint 8, and repeat the same with pin 9, to 13.
Connect 10K ohm resistor to photo sensor, and connect A1 to photo sensor.

Operation instruction:
Control LED 9, blanking 20 time with 200ms cycle time
9 20 200

Read the photo sensor value from A1 25 time with 500ms cycle time.
1 25 500

Enter 999 to interrupt the process if needed.
*/


void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.println();
  Serial.print("Enter your command code:");
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    int Pin = Serial.parseInt();
    int Count = Serial.parseInt();
    int delay_time = Serial.parseInt();
    if((Pin>7)*(Pin<14)|(Pin==1)){
      Serial.print("Pin=");Serial.print(Pin, DEC);
      Serial.print(", Count=");Serial.print(Count, DEC);
      Serial.print(", Time=");Serial.println(delay_time, DEC);Serial.println();
      if((Pin>7)*(Pin<14)){LED_Blinking(Pin,Count,delay_time);}
      if(Pin==1){Photo_Reading(Pin,Count,delay_time);}
    }
  }
}

//Control LED blinking on pin 8 ~ 13
void LED_Blinking(int Pin,int Count,int delay_time){
  //Serial.println("Control LED blinking:");
  for (int i = 0;i<Count;i++){
    //Check for Interuption command, 999
    if(Serial.available() > 0){if(Serial.parseInt()==999){Serial.println("Interuptted");Count=-1;}}
    //Blainking LED
    digitalWrite(Pin, HIGH);delay(delay_time/2);digitalWrite(Pin, LOW);delay(delay_time/2);
  }
}

//Reading photo sensor value from A1
void Photo_Reading(int Pin,int Count,int delay_time){
  Serial.println("Reading photo sensor value:");
  for (int i = 0;i<Count;i++){
    //Check for Interuption command, 999
    if(Serial.available() > 0){if(Serial.parseInt()==999){Serial.println("Interuptted");Count=-1;}}
    //Read and send out the photo sensor value
    Serial.print("Photo Sensor=");Serial.println(analogRead(A1), DEC);delay(delay_time);        
  }
}

No comments:

Post a Comment