Hi,
I'm using Mega with W5100 shield to communicate with PLC via Modbus TCP/IP.
I wanted to put "status led" on the Arduino case, when communication is established, to light up.
On the side of the PLC, I'm constantly sending register with the HIGH state, and reading it with Arduino.
I'm facing the next problem - before communication is established, ledOn is turned off, and ledOff is turned On - which is fine. When communication is established, ledOff is turned OFF and ledOn starts blinking - which is also fine. But when I disconnect the cable (lost connection), ledOn still blinking and ledOff is turned off - which is not what I wanted (I wanted to ledOff turn on, and ledOn stop blinking).
Any help will be great...
Here is the code:
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;
const int ledOn = 8; // led indikacija kada se uspostavi komunikacija sa weintek-om
const int ledOff = 9; // led indikacija dok se nije uspostavila komunikacija as wentek-om
int ledState = LOW;
int readBit;
unsigned long previousMillis = 0;
const long interval = 150;
void setup(void)
{
pinMode (ledOn, OUTPUT);
pinMode (ledOff, OUTPUT);
uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
uint8_t ip[] = { 192, 168, 0, 36 };
uint8_t gateway[] = { 192, 168, 1, 1 };
uint8_t subnet[] = { 255, 255, 255, 0 };
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
}
void loop(void)
{
Mb.Run();
unsigned long currentMillis = millis();
if (Mb.C[6] == HIGH) {
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite (readBit, Mb.C[6]);
digitalWrite (ledOff, LOW);
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledOn, ledState);
}
}
else if (Mb.C[6] == LOW) {
digitalWrite (ledOff, HIGH);
digitalWrite (ledOn, LOW);
}
}