You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
358 lines
12 KiB
Arduino
358 lines
12 KiB
Arduino
2 years ago
|
/*
|
||
|
WEAVER
|
||
|
// board info: LilyGO TTGO T8 ESP32-S2 - with SD Card Slot
|
||
|
// board select: ESP32S2 DEV Module
|
||
|
|
||
|
current USB port: 142300
|
||
|
|
||
|
|
||
|
|
||
|
TO DO
|
||
|
define pins for all the inputs!
|
||
|
fix socket part
|
||
|
redefine range for leftover parameters?
|
||
|
*/
|
||
|
|
||
|
|
||
|
#define socketSensorOut1 45 // PCB: 8 //socket1 out
|
||
|
#define socketSensorOut2 42 // PCB: 16 // socket2
|
||
|
#define socketSensorOut3 40 // PCB: 7 // socket3
|
||
|
#define socketSensorOut4 41 // PCB: 15 // socket4
|
||
|
|
||
|
#define socketSensorIn1 45 // PCB: 8 // socket1 in
|
||
|
#define socketSensorIn2 42 // PCB: 16 // socket2
|
||
|
#define socketSensorIn3 40 // PCB: 7 // socket3
|
||
|
#define socketSensorIn4 41 // PCB: 15 // socket4
|
||
|
|
||
|
#define sensor1 1 // PCB: 46 // potentiometer: pattern1_1
|
||
|
#define sensor2 2 // PCB: 37 // potentiometer: pattern1_2
|
||
|
#define sensor3 3 // PCB: 36 // potentiometer: pattern1_3
|
||
|
#define sensor5 4 // PCB: 45 // potentiometer: pattern2_1
|
||
|
#define sensor6 5 // PCB: 38 // potentiometer: pattern2_2
|
||
|
#define sensor7 6 // PCB: 35 // potentiometer: pattern2_3
|
||
|
#define sensor9 7 // PCB: 42 // potentiometer: pattern3_1
|
||
|
#define sensor10 8 // PCB: 39 // potentiometer: pattern3_2
|
||
|
#define sensor11 16 // PCB: 34 // potentiometer: pattern3_3
|
||
|
#define sensor13 15 // PCB: 41 // potentiometer: pattern4_1
|
||
|
#define sensor14 20 // PCB: 40 // potentiometer: pattern4_2
|
||
|
#define sensor15 19 // PCB: 33 // potentiometer: pattern4_3
|
||
|
|
||
|
#define sensor21 13 // PCB: 13 // potentiometer: patternFactor (*each position in pattern)
|
||
|
#define sensor22 12 // PCB: 12 // potentiometer: patchFactor (*each pattern in patch)
|
||
|
#define sensor23 11 // PCB: 11 // potentiometer: patchworkFactor (*patch)
|
||
|
#define sensorLED 0 // PCB: 0 // LED
|
||
|
|
||
|
|
||
|
String pattern1 = "";
|
||
|
String pattern2 = "";
|
||
|
String pattern3 = "";
|
||
|
String pattern4 = "";
|
||
|
|
||
|
String patch1 = "";
|
||
|
String patch2 = "";
|
||
|
|
||
|
int socketOutput[] = {socketSensorOut1, socketSensorOut2, socketSensorOut3, socketSensorOut4};
|
||
|
int socketInput[] = {socketSensorIn1, socketSensorIn2, socketSensorIn3, socketSensorIn4};
|
||
|
|
||
|
|
||
|
|
||
|
String input;
|
||
|
String newData;
|
||
|
String output;
|
||
|
String moduleID = "#WV";
|
||
|
|
||
|
|
||
|
// the setup routine runs once when you press reset:
|
||
|
void setup() {
|
||
|
// initialize serial and serial1 communication at 115200 and 9600 bits per second:
|
||
|
Serial.begin(115200); //goes to serial monitor, just for debugging purposes
|
||
|
Serial1.begin(9600); // goes to the next module via patch cable (use transmit pin TX on IO17 and also connect to GND)
|
||
|
|
||
|
Serial1.setTimeout(100); // there is a small bug, so there needs to be a checksum?
|
||
|
|
||
|
// set mode for digital input to not give floating values, but either 0 or 1:
|
||
|
pinMode(sensorLED, OUTPUT); // set LED pin to output
|
||
|
//LED initial state
|
||
|
digitalWrite(sensorLED, LOW);
|
||
|
|
||
|
digitalWrite(socketSensorOut1, LOW);
|
||
|
digitalWrite(socketSensorOut2, LOW);
|
||
|
digitalWrite(socketSensorOut3, LOW);
|
||
|
digitalWrite(socketSensorOut4, LOW);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
// the loop routine runs over and over again forever:
|
||
|
void loop() {
|
||
|
|
||
|
|
||
|
// sensor 1: pattern1_1
|
||
|
// read analog input
|
||
|
int sensorValue1 = analogRead(sensor1);
|
||
|
// redefine range for font:
|
||
|
int value1 = map(sensorValue1, 0, 8191, 400, 1);
|
||
|
// use value1 to define first position of pattern1: not get int but string with pattern1.1:
|
||
|
String pattern1_1 = "";
|
||
|
if(value1 >= 1 && value1 < 2) {pattern1_1 = "a";} // if value1 is bigger than or equal to 1 and value1 is smaller than or equal to 100: a
|
||
|
else if(value1 >= 2 && value1 <= 245) {pattern1_1 = "b";} // else if value1 is bigger than or equal to 101 and value1 is smaller than or equal to 200: b
|
||
|
else if(value1 >= 246 && value1 <= 397) {pattern1_1 = "c";}
|
||
|
else if(value1 >= 398 && value1 <= 400) {pattern1_1 = "d";}
|
||
|
|
||
|
// sensor 2: pattern1_2
|
||
|
int sensorValue2 = analogRead(sensor2);
|
||
|
int value2 = map(sensorValue2, 0, 8191, 400, 1);
|
||
|
String pattern1_2 = "";
|
||
|
if(value2 >= 1 && value2 < 2) {pattern1_2 = "a";}
|
||
|
else if(value2 >= 2 && value2 <= 245) {pattern1_2 = "b";}
|
||
|
else if(value2 >= 246 && value2 <= 397) {pattern1_2 = "c";}
|
||
|
else if(value2 >= 398 && value2 <= 400) {pattern1_2 = "d";}
|
||
|
|
||
|
// sensor 3: pattern1_3
|
||
|
int sensorValue3 = analogRead(sensor3);
|
||
|
int value3 = map(sensorValue3, 0, 8191, 400, 1);
|
||
|
String pattern1_3 = "";
|
||
|
if(value3 >= 1 && value3 < 2) {pattern1_3 = "a";}
|
||
|
else if(value3 >= 2 && value3 <= 245) {pattern1_3 = "b";}
|
||
|
else if(value3 >= 246 && value3 <= 397) {pattern1_3 = "c";}
|
||
|
else if(value3 >= 398 && value3 <= 400) {pattern1_3 = "d";}
|
||
|
|
||
|
// sensor 5: pattern2_1
|
||
|
int sensorValue5 = analogRead(sensor5);
|
||
|
int value5 = map(sensorValue5, 0, 8191, 400, 1);
|
||
|
String pattern2_1 = "";
|
||
|
if(value5 >= 1 && value5 < 2) {pattern2_1 = "a";}
|
||
|
else if(value5 >= 2 && value5 <= 245) {pattern2_1 = "b";}
|
||
|
else if(value5 >= 246 && value5 <= 397) {pattern2_1 = "c";}
|
||
|
else if(value5 >= 398 && value5 <= 400) {pattern2_1 = "d";}
|
||
|
|
||
|
// sensor 6: pattern2_2
|
||
|
int sensorValue6 = analogRead(sensor6);
|
||
|
int value6 = map(sensorValue6, 0, 8191, 400, 1);
|
||
|
String pattern2_2 = "";
|
||
|
if(value6 >= 1 && value6 < 2) {pattern2_2 = "a";}
|
||
|
else if(value6 >= 2 && value6 <= 245) {pattern2_2 = "b";}
|
||
|
else if(value6 >= 246 && value6 <= 397) {pattern2_2 = "c";}
|
||
|
else if(value6 >= 398 && value6 <= 400) {pattern2_2 = "d";}
|
||
|
|
||
|
// sensor 7: pattern2_3
|
||
|
int sensorValue7 = analogRead(sensor7);
|
||
|
int value7 = map(sensorValue7, 0, 8191, 400, 1);
|
||
|
String pattern2_3 = "";
|
||
|
if(value7 >= 1 && value7 < 2) {pattern2_3 = "a";}
|
||
|
else if(value7 >= 2 && value7 <= 245) {pattern2_3 = "b";}
|
||
|
else if(value7 >= 246 && value7 <= 397) {pattern2_3 = "c";}
|
||
|
else if(value7 >= 398 && value7 <= 400) {pattern2_3 = "d";}
|
||
|
|
||
|
// sensor 9: pattern3_1
|
||
|
int sensorValue9 = analogRead(sensor9);
|
||
|
int value9 = map(sensorValue9, 0, 8191, 400, 1);
|
||
|
String pattern3_1 = "";
|
||
|
if(value9 >= 1 && value9 < 2) {pattern3_1 = "a";}
|
||
|
else if(value9 >= 2 && value9 <= 245) {pattern3_1 = "b";}
|
||
|
else if(value9 >= 246 && value9 <= 397) {pattern3_1 = "c";}
|
||
|
else if(value9 >= 398 && value9 <= 400) {pattern3_1 = "d";}
|
||
|
|
||
|
// sensor 10: pattern3_2
|
||
|
int sensorValue10 = analogRead(sensor10);
|
||
|
int value10 = map(sensorValue10, 0, 8191, 400, 1);
|
||
|
String pattern3_2 = "";
|
||
|
if(value10 >= 1 && value10 < 2) {pattern3_2 = "a";}
|
||
|
else if(value10 >= 2 && value10 <= 245) {pattern3_2 = "b";}
|
||
|
else if(value10 >= 246 && value10 <= 397) {pattern3_2 = "c";}
|
||
|
else if(value10 >= 398 && value10 <= 400) {pattern3_2 = "d";}
|
||
|
|
||
|
// sensor 11: pattern3_3
|
||
|
int sensorValue11 = analogRead(sensor11);
|
||
|
int value11 = map(sensorValue11, 0, 8191, 400, 1);
|
||
|
String pattern3_3 = "";
|
||
|
if(value11 >= 1 && value11 < 2) {pattern3_3 = "a";}
|
||
|
else if(value11 >= 2 && value11 <= 245) {pattern3_3 = "b";}
|
||
|
else if(value11 >= 246 && value11 <= 397) {pattern3_3 = "c";}
|
||
|
else if(value11 >= 398 && value11 <= 400) {pattern3_3 = "d";}
|
||
|
|
||
|
// sensor 13: pattern4_1
|
||
|
int sensorValue13 = analogRead(sensor13);
|
||
|
int value13 = map(sensorValue13, 0, 8191, 400, 1);
|
||
|
String pattern4_1 = "";
|
||
|
if(value13 >= 1 && value13 < 2) {pattern4_1 = "a";}
|
||
|
else if(value13 >= 2 && value13 <= 245) {pattern4_1 = "b";}
|
||
|
else if(value13 >= 246 && value13 <= 397) {pattern4_1 = "c";}
|
||
|
else if(value13 >= 398 && value13 <= 400) {pattern4_1 = "d";}
|
||
|
|
||
|
// sensor 14: pattern4_2
|
||
|
int sensorValue14 = analogRead(sensor14);
|
||
|
int value14 = map(sensorValue14, 0, 8191, 400, 1);
|
||
|
String pattern4_2 = "";
|
||
|
if(value14 >= 1 && value14 < 2) {pattern4_2 = "a";}
|
||
|
else if(value14 >= 2 && value14 <= 245) {pattern4_2 = "b";}
|
||
|
else if(value14 >= 246 && value14 <= 397) {pattern4_2 = "c";}
|
||
|
else if(value14 >= 398 && value14 <= 400) {pattern4_2 = "d";}
|
||
|
|
||
|
// sensor 15: pattern4_3
|
||
|
int sensorValue15 = analogRead(sensor15);
|
||
|
int value15 = map(sensorValue15, 0, 8191, 400, 1);
|
||
|
String pattern4_3 = "";
|
||
|
if(value15 >= 1 && value15 < 2) {pattern4_3 = "a";}
|
||
|
else if(value15 >= 2 && value15 <= 245) {pattern4_3 = "b";}
|
||
|
else if(value15 >= 246 && value15 <= 397) {pattern4_3 = "c";}
|
||
|
else if(value15 >= 398 && value15 <= 400) {pattern4_3 = "d";}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
// now generate patterns 1 - 4
|
||
|
pattern1 = String(pattern1_1) + String(pattern1_2) + String(pattern1_3);
|
||
|
pattern2 = String(pattern2_1) + String(pattern2_2) + String(pattern2_3);
|
||
|
pattern3 = String(pattern3_1) + String(pattern3_2) + String(pattern3_3);
|
||
|
pattern4 = String(pattern4_1) + String(pattern4_2) + String(pattern4_3);
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
String container = "";
|
||
|
for (int i=0; i<4; i++) { //int starts at 0 and counts up to length of output)
|
||
|
|
||
|
pinMode(socketOutput[i], OUTPUT);
|
||
|
digitalWrite(socketOutput[i], LOW); //turn digital writes to HIGH
|
||
|
for (int o=0; o<4; o++) {
|
||
|
if (i!= o) {
|
||
|
pinMode(socketInput[o], INPUT);
|
||
|
pinMode(socketInput[o], INPUT_PULLUP);
|
||
|
bool state = digitalRead(socketInput[o]);
|
||
|
//Serial.println(state);
|
||
|
if (state==LOW){ //if one of the inputs is high, it is connected to this output
|
||
|
//Serial.println("pair:"+ String(i+1)+ String(o+1));
|
||
|
String pair = String(i+1)+ String(o+1);
|
||
|
//Serial.println(pair);
|
||
|
container = container + pair;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
digitalWrite(socketOutput[i], HIGH);
|
||
|
|
||
|
}
|
||
|
|
||
|
Serial.println(container);
|
||
|
if (container == "1221") {
|
||
|
patch1 = pattern1 + pattern2;
|
||
|
patch2 = pattern2 + pattern1;
|
||
|
}
|
||
|
else if (container == "1331") {
|
||
|
patch1 = pattern1 + pattern3;
|
||
|
patch2 = pattern3 + pattern1;
|
||
|
}
|
||
|
else if (container == "1441") {
|
||
|
patch1 = pattern1 + pattern4;
|
||
|
patch2 = pattern4 + pattern1;
|
||
|
}
|
||
|
else if (container == "2332") {
|
||
|
patch1 = pattern2 + pattern3;
|
||
|
patch2 = pattern3 + pattern2;
|
||
|
}
|
||
|
else if (container == "2442") {
|
||
|
patch1 = pattern2 + pattern4;
|
||
|
patch2 = pattern4 + pattern2;
|
||
|
}
|
||
|
else if (container == "3443") {
|
||
|
patch1 = pattern3 + pattern4;
|
||
|
patch2 = pattern4 + pattern3;
|
||
|
}
|
||
|
else if (container == "12213443") {
|
||
|
patch1 = pattern1 + pattern2;
|
||
|
patch2 = pattern3 + pattern4;
|
||
|
}
|
||
|
else if (container == "13243142") {
|
||
|
patch1 = pattern1 + pattern3;
|
||
|
patch2 = pattern2 + pattern4;
|
||
|
}
|
||
|
else if (container == "14233241") {
|
||
|
patch1 = pattern1 + pattern4;
|
||
|
patch2 = pattern2 + pattern3;
|
||
|
}
|
||
|
else if (container == "") {
|
||
|
patch1 = "";
|
||
|
patch2 = "";
|
||
|
Serial.println("no patch");
|
||
|
}
|
||
|
container = "";
|
||
|
|
||
|
Serial.println(String(patch1) + String(patch2));
|
||
|
|
||
|
|
||
|
// now read leftover parameters
|
||
|
|
||
|
//sensor21: patternFactor (*each position in pattern)
|
||
|
// read analog input
|
||
|
int sensorValue21 = analogRead(sensor21);
|
||
|
// redefine range:
|
||
|
int value21 = map(sensorValue21, 0, 8191, 8191, 0);
|
||
|
int patternFactor = 0;
|
||
|
if(value21 >= 0 && value21 < 1) {patternFactor = 1;}
|
||
|
else if(value21 >= 1 && value21 <= 3300) {patternFactor = 2;}
|
||
|
else if(value21 >= 3301 && value21 <= 8000) {patternFactor = 3;}
|
||
|
else if(value21 >= 8001 && value21 <= 8191) {patternFactor = 4;}
|
||
|
|
||
|
|
||
|
//sensor22: patchFactor (*each pattern in patch)
|
||
|
// read analog input
|
||
|
int sensorValue22 = analogRead(sensor22);
|
||
|
// redefine range:
|
||
|
int value22 = map(sensorValue22, 0, 8191, 8191, 0);
|
||
|
int patchFactor = 0;
|
||
|
if(value22 >= 0 && value22 < 1) {patchFactor = 1;}
|
||
|
else if(value22 >= 1 && value22 <= 3300) {patchFactor = 2;}
|
||
|
else if(value22 >= 3301 && value22 <= 8000) {patchFactor = 3;}
|
||
|
else if(value22 >= 8001 && value22 <= 8191) {patchFactor = 4;}
|
||
|
|
||
|
|
||
|
|
||
|
//sensor23: patchworkFactor (*patch)
|
||
|
// read analog input
|
||
|
int sensorValue23 = analogRead(sensor23);
|
||
|
// redefine range:
|
||
|
int patchworkFactor = map(sensorValue23, 0, 8191, 15, 1);
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
String all_parameters = "&&&" + String(moduleID) + "," + String(pattern1_1) + "," + String(pattern1_2) + "," + String(pattern1_3) + "," + String(pattern2_1) + "," + String(pattern2_2) + "," + String(pattern2_3) + "," + String(pattern3_1) + "," + String(pattern3_2) + "," + String(pattern3_3) + "," + String(pattern4_1) + "," + String(pattern4_2) + "," + String(pattern4_3) + "," + String(patch1) + "," + String(patch2) + "," + String(patternFactor) + "," + String(patchFactor) + "," + String(patchworkFactor) + "&&&";
|
||
|
Serial.println(all_parameters);
|
||
|
|
||
|
|
||
|
//put all the above values in a string to define the new data from this module:
|
||
|
newData = "&&&" + String(moduleID) + "," + String(patch1) + "," + String(patch2) + "," + String(patternFactor) + "," + String(patchFactor) + "," + String(patchworkFactor) + "&&&";
|
||
|
|
||
|
|
||
|
|
||
|
//Receive data from previous module (RX):
|
||
|
input = Serial1.readStringUntil('\n'); // read received data until line break and store in input variable
|
||
|
|
||
|
//LED
|
||
|
if (input != "") {
|
||
|
digitalWrite(sensorLED, HIGH); //turn LED on
|
||
|
}
|
||
|
else {
|
||
|
digitalWrite(sensorLED, LOW); //turn LED off
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
output = input + newData;
|
||
|
|
||
|
//Send data to next module (TX):
|
||
|
Serial1.println(output); //the data received + new data
|
||
|
|
||
|
//Print data on Serial Monitor:
|
||
|
Serial.println("This is the input I received: " + input);
|
||
|
Serial.println("Weaver, new data: " + newData);
|
||
|
Serial.println("This is the output I send: " + output);
|
||
|
|
||
|
|
||
|
}
|