Advertisement
metalx1000

Arduino Serial PIN control for Relay

Jun 19th, 2015
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. /*
  2.   Arduino Code for relay
  3.   Turns given PIN (exmaple 12) on for a third of a second when receiving any input through serial connection
  4.   Recommend not using PIN13 due to signals being sent to it durning startup
  5.  
  6.   To send signal to Arduino from computer (Linux):
  7.   echo '1' > /dev/ttyUSB0 #as root
  8.  
  9.   Created By
  10.   Kris Occhipinti
  11.   June 19th, 2015
  12.   http://www.filmsbykris.com
  13.  
  14.   License GPLv3
  15.   http://www.gnu.org/licenses/gpl-3.0.txt
  16.  
  17.  */
  18. int inByte = 0;
  19. int led = 12;
  20.  
  21. void setup()
  22. {
  23.   // start serial port at 9600 bps and wait for port to open:
  24.   Serial.begin(9600);
  25.  
  26.   pinMode(led, OUTPUT);  
  27.   establishContact();  // send a byte to establish contact until receiver responds
  28. }
  29.  
  30. void loop()
  31. {
  32.   inByte = Serial.read();
  33.   // if we get a valid byte, read analog ins:
  34.   if (inByte != -1) {
  35.     Serial.println(inByte);
  36.     digitalWrite(led, HIGH);
  37.     delay(300);
  38.   }else{
  39.  
  40.     digitalWrite(led, LOW);
  41.   }
  42.  
  43.  
  44. }
  45.  
  46. void establishContact() {
  47.   while (Serial.available() <= 0) {
  48.     Serial.println("waiting...");   // send an initial string
  49.     digitalWrite(led, LOW); //switch off
  50.     delay(300);
  51.   }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement