main.cpp
1 #include <Arduino.h> 2 3 4 #if defined(CONFIG_IDF_TARGET_ESP32S3) 5 #elif defined(CONFIG_IDF_TARGET_ESP32C3) 6 #error Pins have not yet been defined for ESP32C3 7 #else 8 #error Unkown board 9 #endif 10 11 static String string = ""; 12 13 // The potentiometer we use for setting the cycle frequency 14 int FreqSensor = A0; 15 // The potentiometer we use for getting the Vacuum / Atmosphere time distribution 16 int DistSensor = A1; 17 // The LED that represents Vacuum 18 int ledVacPin = 2; 19 // The LED that represents Atmosphere 20 int ledAtmPin = 4; 21 // The initial value of the Frequency 22 float FreqValue = 0; 23 // The initial value of the Distribution 24 float DistValue = 0; 25 int repeats = 0; 26 27 void setup() 28 { 29 Serial.begin(9600); 30 // while(!Serial.available()); 31 Serial.println("setup"); 32 // Assign both of our test LEDs as outputs 33 pinMode(ledVacPin, OUTPUT); 34 Serial.println("After VacPin"); 35 pinMode(ledAtmPin, OUTPUT); 36 Serial.println("After AtmPin"); 37 } 38 39 void loop() 40 { 41 //Serial.println("start of loop"); 42 //Serial.println("test"); 43 // Get the Frequency value from our first potentiometer 44 FreqValue = analogRead(FreqSensor); 45 //Serial.println("read freq as: "); 46 //Serial.println(FreqValue); 47 //String string = String("\n\n") + String("read freq as: ") + String(FreqValue) + "\n"; 48 // Get the Distribution value from our second potentiometer 49 DistValue = analogRead(DistSensor); 50 //Serial.println("read dist as: "); 51 //Serial.println(DistValue); 52 string = string + String("read dist as: ") + String(DistValue) + "\n"; 53 Serial.println(string); 54 55 delay(100); 56 /* 57 // Cast the value we read from the potentiometer from a range of 0-1023 to a range of 20-150 58 // This represents the number of cycles of Vacuum->Atmosphere we want per minute. 59 float cyclesPerMinute = ((FreqValue / 1023) * 160) + 20; 60 // The total combined time the LEDs will be on (in ms) for one cycle 61 float totalDelayInCycle = (60 / cyclesPerMinute) * 1000; 62 */ 63 64 // The total combined time the LEDs will be on (in ms) for one cycle 65 float cycleDuration = (60 / (((FreqValue / 4095) * 130) + 20)) * 1000; // 160 for lower minimum duration, 130 for 0.4 minimum overall duration 66 //Serial.println("cycleDuration calculated"); 67 /* 68 // To get the % of time we spend with Vacuum, we work out the potentiometer value as a percentage of its maximum value 69 float cycleSplit = DistValue / 1023; 70 // If we want a minimum "on-time" value, we need to fudge the numbers a bit 71 // We can do this by adding a buffer to the value we read in, and double that to the 'total' we use to work out our percentage, meaning we can never get 0% or 100% 72 // Then split this between the Vacuum and Atmosphere duration values 73 */ 74 float minBuffer = 50; 75 float cycleSplit = ((DistValue + minBuffer) / (4095 + (minBuffer * 2))); 76 float VacuumDuration = cycleDuration * cycleSplit; 77 float AtmosphereDuration = cycleDuration - VacuumDuration; 78 bool enableForceMin = false; 79 //Serial.println("values set"); 80 81 // Use this to force a minimum duration if the valve misbehaves 82 if (enableForceMin) 83 { 84 //Serial.println("forceMin enabled"); 85 86 if (VacuumDuration < 200) 87 { 88 VacuumDuration = 200; 89 } 90 if (AtmosphereDuration < 200) 91 { 92 AtmosphereDuration = 200; 93 } 94 //Serial.println("values modified for forceMin"); 95 96 } 97 98 // Turn on Vacuum for the specified duration 99 digitalWrite(ledVacPin, HIGH); 100 //Serial.println("ledVacPin high"); 101 102 delay((float)(VacuumDuration)); 103 digitalWrite(ledVacPin, LOW); 104 //Serial.println("ledVacPin low"); 105 106 // Turn on Atmosphere for the specified duration 107 digitalWrite(ledAtmPin, HIGH); 108 //Serial.println("ledAtmPin high"); 109 //Serial.println(AtmosphereDuration); 110 111 delay((float)(AtmosphereDuration)); 112 //Serial.println("AtmosDelay"); 113 114 digitalWrite(ledAtmPin, LOW); 115 //Serial.println("ledAtmPin low"); 116 }