C Program Arduino Serial PIN control for Relay

Arduino Code for relay

  Turns given PIN (exmaple 12) on for a third of a second when receiving any input through serial connection
  Recommend not using PIN13 due to signals being sent to it durning startup


  To send signal to Arduino from computer (Linux):
  echo '1' > /dev/ttyUSB0 #as root

  Created By
  Kris Occhipinti
  June 19th, 2015
  http://www.filmsbykris.com

  License GPLv3
  http://www.gnu.org/licenses/gpl-3.0.txt

Solution:

int inByte = 0;
int led = 12;

void setup()
{
  // start serial port at 9600 bps and wait for port to open:
  Serial.begin(9600);

  pinMode(led, OUTPUT);
  establishContact();  // send a byte to establish contact until receiver responds
}

void loop()
{
  inByte = Serial.read();
  // if we get a valid byte, read analog ins:
  if (inByte != -1) {
    Serial.println(inByte);
    digitalWrite(led, HIGH);
    delay(300);
  }else{

    digitalWrite(led, LOW);
  }


}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("waiting...");   // send an initial string
    digitalWrite(led, LOW); //switch off
    delay(300);
  }
}

C Program Quadratic equation


Learn More :