/*LAST CHANGES 2017-10-26
* /*THIS IS THE MASTER!
This programm is setting the communication pins (Master > Slave)
for each of the 35 umbrellas*/
//PAU = PinAssignmentUmbrella
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
/*START defining global Master-Slave Outputs*/
int DO_PAU[6] = {14, 15, 16, 17, 18, 19}; //ARDUINO PINs: Digital outputs for PAU communication
int Takt = 1; //ARDUINO PIN 1: Takt manages whether SLAVE listens (if HIGH, Slave listens)
int Data = 20; //ARDUINO PIN 20: Data indicates whether umbrella should be closed (LOW) or opened (HIGH)
int Busy = 21; //ARDUINO PIN 21: Busy indicates whether Slave is Busy with reading PAUS (HIGH) or finished (LOW)
/*END defining globalMaster-Slave Outputs*/
//----------------------------------------------------------------------//
/*START UmbrellaSetPins INT*/
int PAU[] = {//Pin assignment for each umbrella
1, 0, 0, 0, 0, 0, //1
0, 1, 0, 0, 0, 0, //2
1, 1, 0, 0, 0, 0, //3
0, 0, 1, 0, 0, 0, //4
1, 0, 1, 0, 0, 0, //5
0, 1, 1, 0, 0, 0, //6
1, 1, 1, 0, 0, 0, //7
0, 0, 0, 1, 0, 0, //8
1, 0, 0, 1, 0, 0, //9
0, 1, 0, 1, 0, 0, //10
1, 1, 0, 1, 0, 0, //11
0, 0, 1, 1, 0, 0, //12
1, 0, 1, 1, 0, 0, //13
0, 1, 1, 1, 0, 0, //14
1, 1, 1, 1, 0, 0, //15
0, 0, 0, 0, 1, 0, //16
1, 0, 0, 0, 1, 0, //17
0, 1, 0, 0, 1, 0, //18
1, 1, 0, 0, 1, 0, //19
0, 0, 1, 0, 1, 0, //20
1, 0, 1, 0, 1, 0, //21
0, 1, 1, 0, 1, 0, //22
1, 1, 1, 0, 1, 0, //23
0, 0, 0, 1, 1, 0, //24
1, 0, 0, 1, 1, 0, //25
0, 1, 0, 1, 1, 0, //26
1, 1, 0, 1, 1, 0, //27
0, 0, 1, 1, 1, 0, //28
1, 0, 1, 1, 1, 0, //29
0, 1, 1, 1, 1, 0, //30
1, 1, 1, 1, 1, 0, //31
0, 0, 0, 0, 0, 1, //32
1, 0, 0, 0, 0, 1, //33
0, 1, 0, 0, 0, 1, //34
1, 1, 0, 0, 0, 1 //35
};
int NumberEffect[20] = {1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2}; //How many umbrellas does the sensor effect?
int EffectedUmbrella[40] = {0,10,1,11,2,12,3,13,4,14,5,15,6,16,7,17,8,18,9,19,0,20,30,21,0,22,31,23,0,24,32,25,0,26,33,27,0,28,34,29};
/*END UmbrellaSetPins INT*/
//----------------------------------------------------------------------//
/*To be adapted: FPS for SensorChecking*/
int DurationSensorChecking = 5000; //Duration in FPS for Trigger > Echo
//int DurationSensorChecking = 30000; //Duration in FPS for Trigger > Echo
int SecurityDelaySensor = 50; //Wait between two sensors for safety reasons to avoid interference.
//----------------------------------------------------------------------//
/*START ints for all Sensor-Changes*/
int SensorChange[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Indicates whether a sensor changes its Data and therefore needs umbrella action
int CurrentSensorData[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Managed by the actual sensors > Sth Detected = 1 / Nothing detected = 0
int PreviousSensorData[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Saved data from previous checking
/*END ints for all Sensor-Changes*/
//----------------------------------------------------------------------//
/*START defining Sensor ARDUINO Pins*/
int SensorTrigger[20] = {3, 5, 7, 9, 11, 13, 24, 28, 32, 36, 40, 44, 48, 52, 25, 29, 33, 37, 41, 45}; //ARDUINO PINs: Digital Outputs
int SensorEcho[20] = {2, 4, 6, 8, 10, 12, 22, 26, 30, 34, 38, 42, 46, 50, 23, 27, 31, 35, 39, 43}; //ARDUINO PINs: Digital Inputs
/*END defining Sensor ARDUINO Pins*/
int WhichSensor = 0;
int AllSensorsNull = 0;
boolean newStart = true;
/*DelayCounter so that Umbrella stays open some time*/
int DelayCounterUmbrella[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Stores delay for each umbrella
int DelayCounter = 5000; //Delay tbd.
int secondUmbrellaDelay = 300; //Delay until 2nd Umbrella reacts.
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
void setup() {
//Serial.begin(9600);
Serial.end();
Serial1.end();
Serial2.end();
Serial3.end();
//START Defining all communication pins//
for (int x = 0; x < 6; x++) { //Declaring all 6 DO-PAUs as Digital Outputs
pinMode(DO_PAU[x], OUTPUT);
digitalWrite(DO_PAU[x], LOW);//Setting all 6 DO-PAUs to LOW
}
pinMode(Takt, OUTPUT); digitalWrite(Takt, LOW);
pinMode(Data, OUTPUT); digitalWrite(Data, LOW);
pinMode(Busy, INPUT);
//END Defining all communication pins//
//START Defining all ARDUINO sensor pins//
for (int x = 0; x < 20; x++) { //Declaring all Trigger Pins as Digital Output, Echo as Digital Input
pinMode(SensorTrigger[x], OUTPUT); digitalWrite(SensorTrigger[x], LOW);
pinMode(SensorEcho[x], INPUT);
}
//END Defining all ARDUINO sensor pins//
}
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
//----------------------------------------------------------------------//
void loop() {
if (newStart == true){
newStart = false;
delay(1000);
}
//START Checking Sensor [WhichSensor] for x FPS
if(digitalRead(SensorEcho[WhichSensor]) == HIGH){CurrentSensorData[WhichSensor] = 1;
DelayCounterUmbrella[WhichSensor] = 0;}
else {
if(DelayCounterUmbrella[WhichSensor] == DelayCounter){
CurrentSensorData[WhichSensor] = 0;
}
else{DelayCounterUmbrella[WhichSensor]++;}
}
//END Checking Sensor [WhichSensor] for x FPS
//START Checking if something changed regarding the current sensor
if (CurrentSensorData[WhichSensor] == PreviousSensorData[WhichSensor]) {
SensorChange[WhichSensor] = 0;
}
else {
SensorChange[WhichSensor] = 1;
}
//END Checking if something changed regarding the current sensor
//START communicating Change to SLAVE
if (SensorChange[WhichSensor] == 1) { // if change = true, start communication
while (digitalRead(Busy) == HIGH) {
delay(1); // >>while SLAVE busy, don't continue.
}
//START Setting PAU for current Umbrella (NrUmbrella)//
digitalWrite(DO_PAU[0], (PAU[(((EffectedUmbrella[WhichSensor])* 6) + 0)]) );
digitalWrite(DO_PAU[1], (PAU[(((EffectedUmbrella[WhichSensor])* 6) + 1)]) );
digitalWrite(DO_PAU[2], (PAU[(((EffectedUmbrella[WhichSensor])* 6) + 2)]) );
digitalWrite(DO_PAU[3], (PAU[(((EffectedUmbrella[WhichSensor])* 6) + 3)]) );
digitalWrite(DO_PAU[4], (PAU[(((EffectedUmbrella[WhichSensor])* 6) + 4)]) );
digitalWrite(DO_PAU[5], (PAU[(((EffectedUmbrella[WhichSensor])* 6) + 5)]) );
//END Setting PAU per Umbrella//
if (CurrentSensorData[WhichSensor] == 1) {
digitalWrite((Data), 1); //Set Data for Slave.
} else {
digitalWrite((Data), 0);
}
PreviousSensorData[WhichSensor] = CurrentSensorData[WhichSensor]; //"Previous state" is now the current one
SensorChange[WhichSensor] = 0; //There's no current change on this sensor
digitalWrite((Takt), 1); //Now the date is ready to be read by SLAVE
//AS SOON AS SLAVE STARTS READING DATA (>> BUSY > HIGH), TAKT > 0. WAIT FOR IT.
while (digitalRead(Busy) == LOW) {
delay(1); // >>while SLAVE busy, don't continue.
}
digitalWrite((Takt), 0);
//START Sensor affects 2 umbrellas, second umbrella is set
if(NumberEffect[WhichSensor] == 2){
delay(secondUmbrellaDelay); //Short Delay until 2nd umbrella reacts.
while (digitalRead(Busy) == HIGH) {
delay(1); // >>while SLAVE busy, don't continue.
}
//START Setting PAU for current Umbrella (NrUmbrella)//
digitalWrite(DO_PAU[0], (PAU[(((EffectedUmbrella[WhichSensor+20])* 6) + 0)]) );
digitalWrite(DO_PAU[1], (PAU[(((EffectedUmbrella[WhichSensor+20])* 6) + 1)]) );
digitalWrite(DO_PAU[2], (PAU[(((EffectedUmbrella[WhichSensor+20])* 6) + 2)]) );
digitalWrite(DO_PAU[3], (PAU[(((EffectedUmbrella[WhichSensor+20])* 6) + 3)]) );
digitalWrite(DO_PAU[4], (PAU[(((EffectedUmbrella[WhichSensor+20])* 6) + 4)]) );
digitalWrite(DO_PAU[5], (PAU[(((EffectedUmbrella[WhichSensor+20])* 6) + 5)]) );
//END Setting PAU per Umbrella//
if (CurrentSensorData[WhichSensor] == 1) {
digitalWrite((Data), 1); //Set Data for Slave.
} else {
digitalWrite((Data), 0);
}
digitalWrite((Takt), 1); //Now the date is ready to be read by SLAVE
//AS SOON AS SLAVE STARTS READING DATA (>> BUSY > HIGH), TAKT > 0. WAIT FOR IT.
while (digitalRead(Busy) == LOW) {
delay(1); // >>while SLAVE busy, don't continue.
}
digitalWrite((Takt), 0);
}
//END Sensor affects 2 umbrella, second umbrella is set
}
//START switching to next sensor after the previous one got checked
if (WhichSensor < 19) {
WhichSensor++;
} else {
WhichSensor = 0;
}
//END switching to next sensor after the previous one got checked
}
Comments