Advertisement
BimoSora

penerima

May 22nd, 2024
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2.  
  3. int in1 = 2;
  4. int in2 = 3;
  5. int in3 = 4;
  6. int in4 = 5;
  7.  
  8. int lightll = 8;
  9. int lightrr = 10;
  10. int seinll = 9;
  11. int seinrr = 11;
  12.  
  13. unsigned long previousMillis = 0; // will store last time LED was updated
  14. const long interval = 500;        // interval at which to blink (milliseconds)
  15. int ledState = HIGH;              // ledState used to set the LED
  16. boolean state = false;
  17.  
  18. unsigned long previousMillis2 = 0; // will store last time LED was updated
  19. const long interval2 = 500;        // interval at which to blink (milliseconds)
  20. int ledState2 = HIGH;
  21. boolean state2 = false;
  22.  
  23. const char* up;
  24. const char* down;
  25. const char* left;
  26. const char* right;
  27. const char* stop;
  28. const char* lightoff;
  29. const char* light;
  30. const char* seinloff;
  31. const char* seinl;
  32. const char* seinroff;
  33. const char* seinr;
  34.  
  35. void setup() {
  36.   // Initialize Serial port
  37.   Serial.begin(9600);
  38.   while (!Serial) continue;
  39.  
  40.   pinMode(in1, OUTPUT);
  41.   pinMode(in2, OUTPUT);
  42.   pinMode(in3, OUTPUT);
  43.   pinMode(in4, OUTPUT);
  44.  
  45.   pinMode(lightll, OUTPUT);
  46.   pinMode(lightrr, OUTPUT);
  47.   pinMode(seinll, OUTPUT);
  48.   pinMode(seinrr, OUTPUT);
  49.  
  50.   digitalWrite(in1, LOW);
  51.   digitalWrite(in2, LOW);
  52.   digitalWrite(in3, LOW);
  53.   digitalWrite(in4, LOW);
  54.  
  55.   digitalWrite(lightll, HIGH);
  56.   digitalWrite(lightrr, HIGH);
  57.   digitalWrite(seinll, HIGH);
  58.   digitalWrite(seinrr, HIGH);
  59. }
  60.  
  61. void loop() {
  62.   JsonDocument doc;
  63.  
  64.   DeserializationError error = deserializeJson(doc, Serial);
  65.   if (error) {
  66.     Serial.print(F("deserializeJson() failed: "));
  67.     Serial.println(error.c_str());
  68.     return;
  69.   }
  70.  
  71.   up = doc["up"]; // "up"
  72.   down = doc["down"]; // "down"
  73.   left = doc["left"]; // "left"
  74.   right = doc["right"]; // "right"
  75.   stop = doc["stop"]; // "stop"
  76.   lightoff = doc["lightoff"]; // "lightoff"
  77.   light = doc["light"]; // "light"
  78.   seinloff = doc["seinloff"]; // "seinloff"
  79.   seinl = doc["seinl"]; // "seinl"
  80.   seinroff = doc["seinroff"]; // "seinroff"
  81.   seinr = doc["seinr"]; // "seinr"
  82.  
  83.   if (up == "a") {
  84.     digitalWrite(in1, HIGH);
  85.     digitalWrite(in2, LOW);
  86.     digitalWrite(in3, HIGH);
  87.     digitalWrite(in4, LOW);
  88.   }
  89.  
  90.   if (down == "b") {
  91.     digitalWrite(in1, LOW);
  92.     digitalWrite(in2, HIGH);
  93.     digitalWrite(in3, LOW);
  94.     digitalWrite(in4, HIGH);
  95.   }
  96.  
  97.   if (left == "c") {
  98.     digitalWrite(in1, LOW);
  99.     digitalWrite(in2, HIGH);
  100.     digitalWrite(in3, LOW);
  101.     digitalWrite(in4, LOW);
  102.   }
  103.  
  104.   if (right == "d"){
  105.     digitalWrite(in1, LOW);
  106.     digitalWrite(in2, LOW);
  107.     digitalWrite(in3, LOW);
  108.     digitalWrite(in4, HIGH);
  109.   }
  110.  
  111.   if (stop == "e") {
  112.     digitalWrite(in1, LOW);
  113.     digitalWrite(in2, LOW);
  114.     digitalWrite(in3, LOW);
  115.     digitalWrite(in4, LOW);
  116.   }
  117.  
  118.   if (lightoff == "f") {
  119.     digitalWrite(lightll, HIGH);
  120.     digitalWrite(lightrr, HIGH);
  121.   }
  122.  
  123.   if (light == "g"){
  124.     digitalWrite(lightll, LOW);
  125.     digitalWrite(lightrr, LOW);
  126.   }
  127.  
  128.   if (seinloff == "h") {
  129.     digitalWrite(seinll, HIGH);
  130.     digitalWrite(seinrr, HIGH);
  131.     state = false;
  132.     state2 = false;
  133.   }
  134.  
  135.   if (seinl == "i") {
  136.     digitalWrite(seinll, LOW);
  137.     digitalWrite(seinrr, HIGH);
  138.     state = true;
  139.     state2 = false;
  140.   }
  141.  
  142.   if (seinroff == "j") {
  143.     digitalWrite(seinrr, HIGH);
  144.     digitalWrite(seinll, HIGH);
  145.     state2 = false;
  146.     state = false;
  147.   }
  148.  
  149.   if (seinr == "k") {
  150.     digitalWrite(seinrr, LOW);
  151.     digitalWrite(seinll, HIGH);
  152.     state2 = true;
  153.     state = false;
  154.   }
  155.  
  156.   if (state) {
  157.     Blink();
  158.   } else {
  159.     digitalWrite(seinll, HIGH); // ensure it's off in case it was still on
  160.   }
  161.  
  162.   if (state2) {
  163.     Blink2();
  164.   } else {
  165.     digitalWrite(seinrr, HIGH); // ensure it's off in case it was still on
  166.   }
  167. }
  168.  
  169. void Blink() {
  170.   unsigned long currentMillis = millis();
  171.   if (currentMillis - previousMillis >= interval) {
  172.     previousMillis = currentMillis;
  173.     ledState = !ledState;
  174.     digitalWrite(seinll, ledState);
  175.   }
  176. }
  177.  
  178. void Blink2() {
  179.   unsigned long currentMillis2 = millis();
  180.   if (currentMillis2 - previousMillis2 >= interval2) {
  181.     previousMillis2 = currentMillis2;
  182.     ledState2 = !ledState2;
  183.     digitalWrite(seinrr, ledState2);
  184.   }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement