Other Arduino Codes and Videos by Robojax

Arduino Code and Video for PowerShell command line Control Relay via USB Robojax.com

Arduino Code to Control relay via USB from command line using Windows PowerShell

This Arduino Code is to read and decode serial monitor code sent from USB to UART or USB to UART FTDI chip command sent from windows PowerShell or other systems commands. When the code is detected and matched with the present codes for example 6 is to Turn relay ON and 2 to turn OFF the relay in turn will turn ON AC bulb.
http://robojax.com/learn/arduino/
  1. Download PowerShell Scripts

 /*
 * This Arduino Code is to read and decode serial monitor
 * code sent from USB to UART or USB to UART FTDI chip
 * command sent from windows PowerShell or other systems commands.
 * When the code is detected and matched with the present codes
 * for example 6 is to Turn relay ON and 2 to turn OFF the relay
 * in turn will turn ON AC bulb.
 Watch the video for this code: https://youtu.be/HzLCkzX5q3w
 * 
 */
// Written by Ahmad S. Robojax at 19:11 on Saturday April 01, 2018
byte RX_Value = 0;        // stores received byte

#define relay 8 // pin 8 for relay

int comON = 6;//code to turn ON the light
int comOFF = 2;// code to turn OFF the light

void setup() {
  // Robojax code for USB command to Relay
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  pinMode(relay,OUTPUT);// define pin 8 as output
  digitalWrite(relay,HIGH);// turn the relay OFF
  Serial.println("Robojax USB Command");
}

void loop() { // run over and over
  // Robojax code for USB command to Relay
  if (Serial.available()) {


    RX_Value = Serial.read();
    if(Serial.read() == comON)
    {
          digitalWrite(relay,LOW);// turn the relay ON
          Serial.print("Code to Turn ON:");
          Serial.println(RX_Value);// print the received command
    }
    if(RX_Value == comOFF)
    {
         digitalWrite(relay,HIGH);// turn the relay OFF
         Serial.print("Code to Turn OFF:");
         Serial.println(RX_Value); // print the received command     
    }

  }
}// loop