Advertisement
retrophil

Arduino + Unity + 240v Mains Fan

Nov 12th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. int fanPin = 3;
  2. int incomingByte[2];
  3. boolean FAN = false;
  4.  
  5. //Waits for 'L1' and 'L2' to be received via COM**
  6. //L1 = ON
  7. //L2 = OFF
  8.  
  9. void setup()
  10. {
  11.   pinMode(fanPin, OUTPUT);
  12.   digitalWrite(fanPin, LOW);
  13.  
  14.   Serial.begin(19200);
  15. }
  16.  
  17. void loop()
  18. {
  19.  
  20.   if (Serial.available() > 0)
  21.   {
  22.         while (Serial.peek() == 'L')
  23.       {
  24.         Serial.read();
  25.         incomingByte[0] = Serial.parseInt();
  26.        
  27.         if(incomingByte[0] == 1)
  28.         {
  29.           FAN = true;
  30.           //Serial.write("HI");
  31.         }
  32.         else
  33.         {
  34.           FAN = false;
  35.           //Serial.write("LOW");
  36.        
  37.         }
  38.       }
  39.     while (Serial.available() > 0)
  40.     {
  41.       Serial.read();
  42.     }
  43.  
  44.     FanCheck();
  45.   }
  46. }
  47.  
  48. void FanCheck()
  49. {
  50.   if (FAN)
  51.   {
  52.     digitalWrite(fanPin, HIGH);
  53.   }
  54.   else
  55.   {
  56.      digitalWrite(fanPin, LOW);
  57.   }
  58.   return;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement