Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoJson.h>
- int in1 = 2;
- int in2 = 3;
- int in3 = 4;
- int in4 = 5;
- int lightll = 8;
- int lightrr = 10;
- int seinll = 9;
- int seinrr = 11;
- unsigned long previousMillis = 0; // will store last time LED was updated
- const long interval = 500; // interval at which to blink (milliseconds)
- int ledState = HIGH; // ledState used to set the LED
- boolean state = false;
- unsigned long previousMillis2 = 0; // will store last time LED was updated
- const long interval2 = 500; // interval at which to blink (milliseconds)
- int ledState2 = HIGH;
- boolean state2 = false;
- const char* up;
- const char* down;
- const char* left;
- const char* right;
- const char* stop;
- const char* lightoff;
- const char* light;
- const char* seinloff;
- const char* seinl;
- const char* seinroff;
- const char* seinr;
- void setup() {
- // Initialize Serial port
- Serial.begin(9600);
- while (!Serial) continue;
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- pinMode(in3, OUTPUT);
- pinMode(in4, OUTPUT);
- pinMode(lightll, OUTPUT);
- pinMode(lightrr, OUTPUT);
- pinMode(seinll, OUTPUT);
- pinMode(seinrr, OUTPUT);
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- digitalWrite(lightll, HIGH);
- digitalWrite(lightrr, HIGH);
- digitalWrite(seinll, HIGH);
- digitalWrite(seinrr, HIGH);
- }
- void loop() {
- JsonDocument doc;
- DeserializationError error = deserializeJson(doc, Serial);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.c_str());
- return;
- }
- up = doc["up"]; // "up"
- down = doc["down"]; // "down"
- left = doc["left"]; // "left"
- right = doc["right"]; // "right"
- stop = doc["stop"]; // "stop"
- lightoff = doc["lightoff"]; // "lightoff"
- light = doc["light"]; // "light"
- seinloff = doc["seinloff"]; // "seinloff"
- seinl = doc["seinl"]; // "seinl"
- seinroff = doc["seinroff"]; // "seinroff"
- seinr = doc["seinr"]; // "seinr"
- if (up == "a") {
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- }
- if (down == "b") {
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- if (left == "c") {
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- }
- if (right == "d"){
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- if (stop == "e") {
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- }
- if (lightoff == "f") {
- digitalWrite(lightll, HIGH);
- digitalWrite(lightrr, HIGH);
- }
- if (light == "g"){
- digitalWrite(lightll, LOW);
- digitalWrite(lightrr, LOW);
- }
- if (seinloff == "h") {
- digitalWrite(seinll, HIGH);
- digitalWrite(seinrr, HIGH);
- state = false;
- state2 = false;
- }
- if (seinl == "i") {
- digitalWrite(seinll, LOW);
- digitalWrite(seinrr, HIGH);
- state = true;
- state2 = false;
- }
- if (seinroff == "j") {
- digitalWrite(seinrr, HIGH);
- digitalWrite(seinll, HIGH);
- state2 = false;
- state = false;
- }
- if (seinr == "k") {
- digitalWrite(seinrr, LOW);
- digitalWrite(seinll, HIGH);
- state2 = true;
- state = false;
- }
- if (state) {
- Blink();
- } else {
- digitalWrite(seinll, HIGH); // ensure it's off in case it was still on
- }
- if (state2) {
- Blink2();
- } else {
- digitalWrite(seinrr, HIGH); // ensure it's off in case it was still on
- }
- }
- void Blink() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- ledState = !ledState;
- digitalWrite(seinll, ledState);
- }
- }
- void Blink2() {
- unsigned long currentMillis2 = millis();
- if (currentMillis2 - previousMillis2 >= interval2) {
- previousMillis2 = currentMillis2;
- ledState2 = !ledState2;
- digitalWrite(seinrr, ledState2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement