#include <Ethernet2.h>
#include <EthernetClient.h>
#include <SPI.h>
#include <SD.h>
#include <TimerOne.h>
#include <avr/wdt.h>
//************************************************************************************
//************************************************************************************
// GLOBAL VARIABLES
const int chipSelect = 4;// SD
File dataFile;//FILE ENTITY
File dataFile2;//FILE ENTITY
volatile unsigned long int total;//Counter accumelator
char b, R, N, X, Y, Z, x, p; // k //status flag to stop count while stop , connection trials count ,
//status send & reset send, reet debouncer , count debouncer , repeat send status
int port=5045;
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x76, 0x7C};
byte ip[] = {10, 10, 10, 45}; // unit for machine 21 counting ip =41
byte server[] = {10, 10, 10, 250}; // server ip
EthernetClient client;
//************************************************************************************
//************************************************************************************
void setup() {
char w, c;
wdt_disable();
p=0;
pinMode (2, INPUT); // status
pinMode (8, OUTPUT); // reset out
pinMode (9, INPUT); // reset in
pinMode (3, INPUT); // count
pinMode(10, OUTPUT);//CS for SD
pinMode (7, OUTPUT); //Simulator o/p
//----------------------------------------------------------------
c = 0;
while (c < 20)
{
if (SD.begin(chipSelect))
{
c = 120;
}
else
{
delay(5);
c++;
}
}
//-------------------------------------------------
Ethernet.begin(mac, ip);
c = 0;
while (c < 20)
{
c++;
client.connect(server, port);
if (client.connected())
{
c = 120;
}
else
{
client.stop();
delay(5);
}
}
//-------------------------------------------------
w = 0;
dataFile2 = SD.open("total.txt");
if (dataFile2)
{
w = 1;
getit();
}
else {
dataFile2 = SD.open("total1.txt");
}
if (dataFile2)
{
if (w == 0) {
getit();
}
}
//-------------------------------------------------
Timer1.initialize(2000000L); // set a timer 2 seconds
Timer1.attachInterrupt( Repeats ); // attach the service routine here
Status1();
}
//************************************************************************************
//************************************************************************************
void loop() {
char f, a;
attachInterrupt (digitalPinToInterrupt (3), Counting, RISING); // attach interrupt handler
//----------------------------------------------------------------
f = digitalRead(2);
if (f != X)
{
Status1();
X = f;
}
//----------------------------------------------------------------
//----------------------------------------------------------------
a = digitalRead(9);
if (a != Z)
{
Z = a;
if (digitalRead(9) == 1)
{
delay(5);
if (digitalRead(9) == 1)
{
reset1();
}
}
}
//----------------------------------------------------------------
if (R == 1)
{ // those are the functions which where in the Timer ISR R is a flag of time
sendether();
p++;
if (p>=28){
p=0;
}
writetotal();
R = 0;
}
}
//************************************************************************************
//************************************************************************************
void Counting()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 150)
{
if (digitalRead(2) == 1)
{
total++;
}
}
last_interrupt_time = interrupt_time;
}
//************************************************************************************
//************************************************************************************
void reset1()
{
b = 2;
digitalWrite(8 , HIGH);
delay(1000);
digitalWrite(8 , LOW);
delay(500);
sendether();
total = 0;
writetotal();
}
//************************************************************************************
//************************************************************************************
void Status1()
{
b = 1;
if (digitalRead(2) == 1)
{
sendether();
}
else
{
sendether();
}
}
//************************************************************************************
//************************************************************************************
void Repeats()
{
R = 1;
// k++;
}
//************************************************************************************
//************************************************************************************
void sendether()
{
char c;
c = 0;
while (c < 5)
{
c++;
client.connect(server, port);
if (client.connected())
{
c = 10;
}
else
{
client.stop();
}
}
// if coming from reset
if (b == 2)
{
client.println(total);
client.println(F("shift"));
client.println(total);
client.println(total);
client.println(total);
b = 0;
}
// if coming from repeat
if (b == 0)
{
client.println(total);
x++;
if (x == 5)
{
if (digitalRead(2) == 1) {
client.println(F("run"));
}
else if (digitalRead(2) == 0) {
client.println(F("stop"));
}
x = 0;
}
}
// if coming from status
else if (b == 1)
{
if (digitalRead(2) == 1)
{
client.println(F("run"));
client.println(F("run"));
b = 0;
}
else if (digitalRead(2) == 0)
{
client.println(F("stop"));
client.println(F("stop"));
b = 0;
}
}
}
//************************************************************************************
//************************************************************************************
void writetotal()
{
char dataString2[25];
sprintf(dataString2, "%u", total);
SD.remove("total.txt");
dataFile2 = SD.open("total.txt", FILE_WRITE);
if (dataFile2)
{
dataFile2.println(dataString2);
dataFile2.close();
}
SD.remove("total1.txt");
dataFile2 = SD.open("total1.txt", FILE_WRITE);
if (dataFile2)
{
dataFile2.println(dataString2);
dataFile2.close();
}
}
//************************************************************************************
//************************************************************************************
void getit()
{
char dataString1[25];
char q = 0;
while (dataFile2.available())
{
dataString1[q++] = dataFile2.read();
dataString1[q] = '\0';
}
// close the file:
dataFile2.close();
}
//************************************************************************************
//************************************************************************************