11#include < Arduino.h>
2-
2+ #include < NativeEthernet.h>
3+ #include < NativeEthernetUdp.h>
34#include < Adafruit_NeoPixel.h>
45
5- #define ESTOP_BUTTON_PIN 41
6+ #include " config.hpp"
7+ #include " heart_beat.hpp"
8+
9+ #define ESTOP_BUTTON_IN_PIN 41
10+ #define ESTOP_BUTTON_OUT_PIN 37
611
712#define NUMPIXELS 60
813#define BRIGHTNESS 100
@@ -17,6 +22,9 @@ Adafruit_NeoPixel pixels[pixels_n] = {
1722 Adafruit_NeoPixel (NUMPIXELS, 17 , NEO_GRB + NEO_KHZ800),
1823};
1924
25+ // Heart Beat
26+ UDPHeartBeat UdpHeart (LOCALPORT_HEART, TIMEOUT);
27+
2028unsigned long pre_time = 0 ;
2129unsigned long pre_saver_time = 0 ;
2230bool pre_estop_state = false ;
@@ -42,7 +50,8 @@ void setup()
4250{
4351 Serial.begin (9600 );
4452
45- pinMode (ESTOP_BUTTON_PIN, INPUT);
53+ pinMode (ESTOP_BUTTON_IN_PIN, INPUT);
54+ pinMode (ESTOP_BUTTON_OUT_PIN, OUTPUT);
4655
4756 // Neopixel initalize
4857 for (int i = 0 ; i < pixels_n; i++)
@@ -52,13 +61,32 @@ void setup()
5261 pixels[i].clear ();
5362 pixels[i].show ();
5463 }
64+
65+ Ethernet.begin (MAC, IP);
66+ if (Ethernet.hardwareStatus () == EthernetNoHardware) {
67+ Serial.println (" Ethernet shield was not found. Sorry, can't run without hardware. :(" );
68+ while (true ) {
69+ delay (1 ); // do nothing, no point running without Ethernet hardware
70+ }
71+ }
72+ if (Ethernet.linkStatus () == LinkOFF) {
73+ Serial.println (" Ethernet cable is not connected." );
74+ }
75+ // start UDP
76+ UdpHeart.begin ();
5577}
5678
5779
5880void loop ()
5981{
60- bool estop_state = !digitalRead (ESTOP_BUTTON_PIN);
82+ // Check heart beat and signal on/off
83+ if (UdpHeart.verify_survival ()) digitalWrite (ESTOP_BUTTON_OUT_PIN, HIGH);
84+ else digitalWrite (ESTOP_BUTTON_OUT_PIN, LOW);
85+
86+ // Check e-stop button state
87+ bool estop_state = !digitalRead (ESTOP_BUTTON_IN_PIN);
6188
89+ // Determination state
6290 if (estop_state == true && millis () - pre_saver_time < SAVER)
6391 state = 2 ;
6492 else if (millis () - pre_time > INTERVAL)
0 commit comments