#c #timer
Вопрос:
я пытаюсь создать светофор, где состояние света(красный) дает приказ (зеленый) двигаться дальше и переходить с зеленого на красный. В этом проекте я использую 2 arduinoboards, такие как связь между хозяином и подчиненным. Проблема в том, что светофор должен подождать, прежде чем загорится зеленый свет, когда другой горит красным.
#include lt;Arduino.hgt; #include lt;TimerOne.hgt; #include "traffic_light.h" #include "traffic_slave_serial.h" const int greenLightPin = 6; const int redLightPin = 7; int Prev_State; unsigned long previousMillis = 0; long OnTime = 250; // milliseconds of on-time long OffTime = 750; // milliseconds of off-time long interval = 1000; // interval at which to blink (milliseconds) #define MESSAGE_START ('#') #define MESSAGE_STOP (';') #define TIME_Traffic_light_state (3000) bool (*sendRequestAndCheckReply)(char*mes); typedef enum { MASTER_SLAVE_CONNECTING, MASTER_DRIVING_ALLOWED, SLAVE_DRIVING_ALLOWED, NOT_DRIVING_ALLOWED, } Traffic_light_state; int currentState = MASTER_SLAVE_CONNECTING; int Master_Slave_Connecting_Begin() { char buf [20] = "BEAT"; bool voltooid = sendRequestAndCheckReply(buf); Serial.print(buf); Serial.print("........"); Serial.println(voltooid ? "ack": "nack"); if (voltooid) { currentState = MASTER_DRIVING_ALLOWED; return currentState; } else { return currentState; } } int Master_Slave_RED(int state) { { char doneRed [20] = "RED"; bool voltooid = sendRequestAndCheckReply(doneRed); Serial.print(doneRed); Serial.print("........"); Serial.println(voltooid ? "ack": "nack"); traffic_light_set_color(RED); } if (state == MASTER_DRIVING_ALLOWED) { currentState = SLAVE_DRIVING_ALLOWED; } if (state == SLAVE_DRIVING_ALLOWED) { currentState = MASTER_DRIVING_ALLOWED; } return currentState; } int Slave_Green_Light_Begin() { { char doneGreen[20] = "GREEN"; bool voltooid = sendRequestAndCheckReply(doneGreen); Serial.print(doneGreen); Serial.print("........"); Serial.println(voltooid ? "ack": "nack"); if (voltooid) { traffic_light_set_color(RED); startmillis=millis(); } else { currentState = MASTER_SLAVE_CONNECTING; return currentState; } currentState = NOT_DRIVING_ALLOWED; Prev_State = SLAVE_DRIVING_ALLOWED; return currentState ; } } int Master_Green_Light_Begin() { { char donered[20] = "RED"; bool voltooid = sendRequestAndCheckReply(donered); Serial.print(donered); Serial.print("........"); Serial.println(voltooid ? "ack": "nack"); if (voltooid) { traffic_light_set_color(GREEN); } else { currentState = MASTER_SLAVE_CONNECTING; return currentState; } currentState = NOT_DRIVING_ALLOWED; Prev_State = MASTER_DRIVING_ALLOWED; return currentState ; } } void startmilis() unsigned long currentMillis = millis(); { if(currentMillis - previousMillis gt; interval) { // save the last time you blinked the LED previousMillis = currentMillis; } } /*----------------------------------------------------------------*/ /* SETUP */ /*----------------------------------------------------------------*/ void setup() { Serial.begin(9600); Serial.println("master gestard"); traffic_slave_serial_begin(MESSAGE_START,MESSAGE_STOP); sendRequestAndCheckReply = traffic_slave_serial_sendRequestAndCheckReply; traffic_light_begin(greenLightPin, redLightPin); Timer1.initialize(500000000); } /*----------------------------------------------------------------*/ /* LOOP */ /*----------------------------------------------------------------*/ void loop() { switch (currentState) { case MASTER_SLAVE_CONNECTING: // case 1 Master_Slave_Connecting_Begin(); break; case MASTER_DRIVING_ALLOWED: // case 2 Master_Green_Light_Begin(); break; case SLAVE_DRIVING_ALLOWED: // case 3 Slave_Green_Light_Begin(); break; case NOT_DRIVING_ALLOWED: // case 4 Master_Slave_RED(Prev_State); break; } }