Ti allego tutto il progetto:
Praticamente è un programma con funzioni complete come se fosse un PLC.
Dove tutto viene aggiornato e ragioni con logica "LADDER".
Ho usato questa "specie di struttura" per fare il flipper che ho casa: dove tutto è sempre in aggiornamento e le temporizzazioni devono essere dinamiche e non bloccanti...
File principale:
#include "Wire.h"
#define EEPROM_I2C_ADDRESS 0x50
//---------------------------------------------------------------------------------------
#define Num_In 6
#define Num_Out 8
#define Debug_Led 13
byte Pin_In[] = {2, 3, 4, 5, 6, 7};
byte Pin_Out[] = {9, 10, 11, 12, A0, A1, A2, A3};
bool In[Num_In], Out[Num_Out];
//---------------------------------------------------------------------------------------
#define Num_Mem 32
bool Mem[Num_Mem];
//---------------------------------------------------------------------------------------
#define Num_Timer 16
bool Timer_Out[Num_Timer];
int Timer_Data[Num_Timer];
//---------------------------------------------------------------------------------------
#define Num_Counter 16
bool Counter_Out[Num_Counter], Counter_Mem_Up[Num_Counter], Counter_Mem_Down[Num_Counter];
int Counter_Data[Num_Counter], Counter_Value[Num_Counter];
//---------------------------------------------------------------------------------------
#define Num_Edge 16
bool Edge_Out[Num_Edge], Edge_Mem[Num_Edge];
//---------------------------------------------------------------------------------------
#define Num_Data 16
int Data[Num_Data];
//---------------------------------------------------------------------------------------
#define Num_Compare 8
int Compare_A[Num_Compare], Compare_B[Num_Compare];
bool Compare_Out[Num_Compare];
//---------------------------------------------------------------------------------------
#define Num_Math 8
int Math_Data[Num_Math];
//---------------------------------------------------------------------------------------
#define Counter_Led_Value 500
int Counter_Led;
void setup() {
delay(10);
for (byte i = 0; i < Num_In; i++) {
pinMode(Pin_In[i], INPUT);
}
for (byte i = 0; i < Num_Out; i++) {
digitalWrite(Pin_Out[i], 0);
pinMode(Pin_Out[i], OUTPUT);
}
pinMode(Debug_Led, OUTPUT);
Wire.begin();
Serial.begin(115200);
delay(500);
Program_Setup();
}
void loop() {
delay(1);
Read_Input();
Program_Loop();
Write_Output();
if (Counter_Led < Counter_Led_Value) {
Counter_Led++;
} else {
Counter_Led = 0;
Program_Debug();
digitalWrite(Debug_Led, !digitalRead(Debug_Led));
}
}
File Hardware:
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Read_Input() {
for (byte i = 0; i < Num_In; i++) {
In[i] = digitalRead(Pin_In[i]);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Write_Output() {
for (byte i = 0; i < Num_Out; i++) {
digitalWrite(Pin_Out[i], Out[i]);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
byte EEPROM_Read(int ADR) {
byte rData = 0xFF;
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(ADR >> 8)); // MSB
Wire.write((int)(ADR & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
rData = Wire.read();
return rData;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void EEPROM_Write(byte DATA, unsigned int ADR) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(ADR >> 8)); // MSB
Wire.write((int)(ADR & 0xFF)); // LSB
Wire.write(DATA);
Wire.endTransmission();
delay(10);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void EEPROM_Write_Page(byte DATA[], byte OFFSET, byte LENGHT, unsigned int ADR) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(ADR >> 8)); // MSB
Wire.write((int)(ADR & 0xFF)); // LSB
for (byte i = 0; i < LENGHT; i++) {
Wire.write(DATA[i + OFFSET]);
}
Wire.endTransmission();
delay(10);
}
File delle funzioni:
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Timer(bool SET, int TIME, byte ADR) {
if (SET == 1) {
if (Timer_Data[ADR] < TIME) {
Timer_Data[ADR]++;
} else {
Timer_Out[ADR] = 1;
}
} else {
Timer_Out[ADR] = 0;
Timer_Data[ADR] = 0;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Counter_Up(bool SET, byte ADR) {
if (SET != Counter_Mem_Up[ADR]) {
if (SET == 1) {
Counter_Data[ADR]++;
Counter_Process(ADR);
}
Counter_Mem_Up[ADR] = SET;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Counter_Down(bool SET, byte ADR) {
if (SET != Counter_Mem_Down[ADR]) {
if (SET == 1) {
Counter_Data[ADR]--;
Counter_Process(ADR);
}
Counter_Mem_Down[ADR] = SET;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Counter_Process(byte ADR) {
if (Counter_Data[ADR] >= Counter_Value[ADR]) {
Counter_Out[ADR] = 1;
} else {
Counter_Out[ADR] = 0;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Counter_Reset(bool RESET, byte ADR) {
if (RESET == 1) {
Counter_Out[ADR] = 0;
Counter_Data[ADR] = 0;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Edge(bool IN_EDGE, byte FUC, byte ADR) {
bool Rising_Edge = 0, Falling_Edge = 0;
if (FUC == 1) {
Rising_Edge = 1;
Falling_Edge = 0;
} else if (FUC == 2) {
Rising_Edge = 0;
Falling_Edge = 1;
} else if (FUC == 3) {
Rising_Edge = 1;
Falling_Edge = 1;
}
Edge_Out[ADR] = (IN_EDGE and !Edge_Mem[ADR] and Rising_Edge) or (!IN_EDGE and Edge_Mem[ADR] and Falling_Edge);
Edge_Mem[ADR] = IN_EDGE;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Compare(byte TYPE, byte ADR) {
if (TYPE == 1) {
if (Compare_A[ADR] == Compare_B[ADR]) {
Compare_Out[ADR] = 1;
} else {
Compare_Out[ADR] = 0;
}
} else if (TYPE == 2) {
if (Compare_A[ADR] > Compare_B[ADR]) {
Compare_Out[ADR] = 1;
} else {
Compare_Out[ADR] = 0;
}
} else if (TYPE == 3) {
if (Compare_A[ADR] < Compare_B[ADR]) {
Compare_Out[ADR] = 1;
} else {
Compare_Out[ADR] = 0;
}
} else if (TYPE == 4) {
if (Compare_A[ADR] >= Compare_B[ADR]) {
Compare_Out[ADR] = 1;
} else {
Compare_Out[ADR] = 0;
}
} else if (TYPE == 5) {
if (Compare_A[ADR] <= Compare_B[ADR]) {
Compare_Out[ADR] = 1;
} else {
Compare_Out[ADR] = 0;
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Math(int DATA_A, int DATA_B, byte TYPE, byte ADR) {
if (TYPE == 1) {
Math_Data[ADR] = DATA_A + DATA_B;
} else if (TYPE == 2) {
Math_Data[ADR] = DATA_A - DATA_B;
} else if (TYPE == 3) {
Math_Data[ADR] = DATA_A * DATA_B;
} else if (TYPE == 4) {
Math_Data[ADR] = DATA_A / DATA_B;
}
}
File Main:
void Program_Setup() {
}
void Program_Loop() {
for (int i = 0; i < 10000; i++) {
Out[0] = (In[0] or Out[0]) and !In[1];
}
}
void Program_Debug(){
}