Last active
July 9, 2021 13:53
-
-
Save svavassori/7fc9af576663dc35193071d04cc8e4cf to your computer and use it in GitHub Desktop.
raw_sensor_read_2gate.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* gate 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15 || 16 || 17 || 18 || 19 || 20 || 21 || 22 || 23 | |
* inside 1 || 3 || 5 || 7 || 9 || 11 || 13 || 15 || 17 || 19 || 21 || 23 || 25 || 27 || 29 || 31 || 33 || 35 || 37 || 39 || 41 || 43 || 45 || 47 | |
* outside 2 || 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 22 || 24 || 26 || 28 || 30 || 32 || 34 || 36 || 38 || 40 || 42 || 44 || 46 || 48 | |
* | |
* | |
* HOW long can we drive the LEDs ON without current limiting resistors? | |
* LED timed pulse (tp) allowance vs period (T) | |
* with increasing forward current the IR LED cutsheet says max tp/T = 0.01 | |
* so the max tp for 1 amp is 100us and 100us/0.01 = 10ms | |
* with an ON time of ~75us the max period or delay between ON times is 75us/0.01=7.5ms | |
* So the minimum OFF time between ON times is 7.5ms | |
* This is very conservative as the forward voltage of 3.3v/2 = 1.65v, and max current from the cutsheet is estimated ~170ma | |
* this is well below the absolute max of 1 amp with which the tp/T ratio was developed for | |
* | |
* | |
* Rough Power Draw with cheap USB current sensor | |
* feather ESP32 by itself with tp/T 15us/2ms => 50ma | |
* feather ESP32 by itself with tp/T 15us/10ms => 40-50ma | |
* ESP32 & LEDs | |
* tp/T 15us/2ms 100ma | |
* tp/T 15us/4ms 75ma | |
* tp/T 15us/6ms 60-70ma had to add 3000uf cap to smooth out drawl | |
* tp/T 15us/8ms 50-60ma had to add 3000uf cap to smooth out drawl | |
* tp/T 15us/10ms 50ma had to add 3000uf cap to smooth out drawl | |
* | |
* | |
* | |
* | |
* This sketch: | |
* It takes a honey bee around 250ms to traverse the sensor This code only looks for honeybees moving faster than 650ms. | |
* | |
* | |
* | |
* | |
*/ | |
#include <SPI.h> | |
const int testLed = 13; //onboard LED | |
const int LATCH = A5; | |
//Pins Feather ESP32 | |
const byte powerGates1 = 15; | |
const byte powerGates2 = 33; | |
//Pins ItsyBitsy | |
//const byte powerGates1 = 10; | |
//const byte powerGates2 = 11; | |
const int numberOfGates = 2; // 24 gates, 48 sensors | |
const int startGate = 0; //useful for testing | |
const int endGate = 2; //useful for testing | |
unsigned long currentTime = 0; | |
//boolean 0 or 1 sensor readings, 1 bee is present | |
boolean inSensorReading[numberOfGates]; | |
boolean outSensorReading[numberOfGates]; | |
void setup () | |
{ | |
SPI.begin(); | |
SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE2)); | |
Serial.begin (115200); | |
Serial.println ("Begin switch test."); | |
pinMode (LATCH, OUTPUT); | |
digitalWrite (LATCH, HIGH); | |
pinMode (powerGates1, OUTPUT); | |
digitalWrite(powerGates1, LOW); | |
pinMode (powerGates2, OUTPUT); | |
digitalWrite(powerGates2, LOW); | |
pinMode(testLed, OUTPUT); | |
} // end of setup | |
byte switchBank1; | |
byte switchBank2; | |
byte switchBank3; | |
byte switchBank4; | |
byte switchBank5; | |
byte switchBank6; | |
void loop () | |
{ | |
currentTime = millis(); | |
digitalWrite(powerGates1, HIGH); | |
digitalWrite(powerGates2, HIGH); | |
delayMicroseconds(75); //first 24 gates only need 15us while gates closer to the end need ~40us-75us | |
digitalWrite (LATCH, LOW); // pulse the parallel load latch | |
delayMicroseconds(3); | |
digitalWrite (LATCH, HIGH); | |
delayMicroseconds(3); | |
digitalWrite(powerGates1, LOW); | |
digitalWrite(powerGates2, LOW); | |
//Reading 24 bits at 1Mhz should take about 24 microseconds, | |
//reading 24 bits at 3Mhz should take about 8us | |
//reading 48 bits at 3Mhz should take abotu 16us | |
switchBank1 = SPI.transfer (0); //8 | |
switchBank2 = SPI.transfer (0); //16 | |
switchBank3 = SPI.transfer (0); //24 | |
switchBank4 = SPI.transfer (0); //32 | |
switchBank5 = SPI.transfer (0); //40 | |
switchBank6 = SPI.transfer (0); //48 | |
boolean anyHigh = false; | |
//convert bytes to gate values only for gate 0 and 1 | |
for (int i = startGate; i < endGate; i++) | |
{ | |
anyHigh |= outSensorReading[i] = ((switchBank1 >> 0) & 1); | |
anyHigh |= inSensorReading[i] = ((switchBank1 >> 1) & 1); | |
switchBank1 >>= 2; | |
} | |
// light the LED if any sensor is HIGH | |
digitalWrite(testLed, anyHigh ? HIGH : LOW); | |
// print out time,in and out values | |
Serial.print(currentTime); | |
for (int i = startGate; i < endGate; i++) | |
{ | |
int in = inSensorReading[i] == HIGH ? 1 : 0; | |
int out = outSensorReading[i] == HIGH ? 1 : 0; | |
Serial.print(","); | |
Serial.print(in); | |
Serial.print(out); | |
} | |
Serial.println(""); | |
delay(9); // wait some time | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment