bobmarley12345

arduinoserialcodethingy

Apr 14th, 2019
313
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String readString;
  2.  
  3. void setup() {
  4.   Serial.begin(115200);
  5.   pinMode(2, OUTPUT);
  6.   pinMode(3, OUTPUT);  
  7.   pinMode(4, OUTPUT);
  8.   pinMode(5, OUTPUT);
  9.   pinMode(6, OUTPUT);
  10.   pinMode(7, OUTPUT);
  11.   pinMode(8, OUTPUT);
  12.   pinMode(9, OUTPUT);
  13.   pinMode(10, OUTPUT);
  14.   pinMode(11, OUTPUT);
  15.   pinMode(12, OUTPUT);
  16.   pinMode(13, OUTPUT);
  17.   Serial.println("Arduin-Serial thingy - made by TheR ;)");
  18.   Serial.println("type 'help for a list of commands");
  19. }
  20.  
  21. void loop()
  22. {
  23.  
  24.   if (readString.length() >0)
  25.   {
  26.     //prints the incomming serial text
  27.     Serial.println(readString);
  28.     //Command Handling
  29.  
  30.     if (readString == "help")
  31.     {
  32.         Serial.println("pin(2-13)(on or off) - e.g, 'pin13on' or 'pin7off'");
  33.     }
  34.  
  35.     //Pins on
  36.     //0x1 is short for HIGH
  37.     if (readString == "pin2on") { Pin(2, 0x1); }
  38.     if (readString == "pin3on") { Pin(3, 0x1); }
  39.     if (readString == "pin4on") { Pin(4, 0x1); }
  40.     if (readString == "pin5on") { Pin(5, 0x1); }
  41.     if (readString == "pin6on") { Pin(6, 0x1); }
  42.     if (readString == "pin7on") { Pin(7, 0x1); }
  43.     if (readString == "pin8on") { Pin(8, 0x1); }
  44.     if (readString == "pin9on") { Pin(9, 0x1); }
  45.     if (readString == "pin10on") { Pin(10, 0x1); }
  46.     if (readString == "pin11on") { Pin(11, 0x1); }
  47.     if (readString == "pin12on") { Pin(12, 0x1); }
  48.     if (readString == "pin13on") { Pin(13, 0x1); }
  49.  
  50.     //Pins off
  51.     //0x0 is short for LOW
  52.     if (readString == "pin2off") { Pin(2, 0x0); }
  53.     if (readString == "pin3off") { Pin(3, 0x0); }
  54.     if (readString == "pin4off") { Pin(4, 0x0); }
  55.     if (readString == "pin5off") { Pin(5, 0x0); }
  56.     if (readString == "pin6off") { Pin(6, 0x0); }
  57.     if (readString == "pin7off") { Pin(7, 0x0); }
  58.     if (readString == "pin8off") { Pin(8, 0x0); }
  59.     if (readString == "pin9off") { Pin(9, 0x0); }
  60.     if (readString == "pin10off") { Pin(10, 0x0); }
  61.     if (readString == "pin11off") { Pin(11, 0x0); }
  62.     if (readString == "pin12off") { Pin(12, 0x0); }
  63.     if (readString == "pin13off") { Pin(13, 0x0); }
  64.     readString="";
  65.   }
  66. }
  67.  
  68. void Pin(int pinnumber, int HighOrLow)
  69. {
  70.     if (HighOrLow == 0x1)
  71.     {
  72.         digitalWrite(pinnumber, 0x1);
  73.     }
  74.     else
  75.     {
  76.         digitalWrite(pinnumber, 0x0);
  77.     }
  78. }
  79.  
  80. //sets readString as the incomming serial data
  81. //serialEvent is a built in method for arduino, and fires when data is received. Byte by Byte.
  82. void serialEvent()
  83. {
  84.   while (Serial.available())
  85.   {
  86.     delay(3);  
  87.     char c = Serial.read();
  88.     readString += c;
  89.   }
  90. }
RAW Paste Data