diff --git a/address_cycling/src/h/address_cycling.h b/address_cycling/src/h/address_cycling.h index cf7e0c3292af5b3a406f2427c5dda5aac474aa02..fb27d2268c32a1706eca0e78862b45e3c42cd578 100644 --- a/address_cycling/src/h/address_cycling.h +++ b/address_cycling/src/h/address_cycling.h @@ -3,28 +3,29 @@ const int addrPins[] = {46,3,8,18}; const int segmentPins[] = {17,16,15,7,6,5,4}; +const int ledPins[] = {17,16,15,7,6,5,4,42,2,1}; int address; int plants; const byte hexMap[16][7]={ - // a,b,c,d,e,f,g - {1,1,1,1,1,1,0}, // 0 - {0,1,1,0,0,0,0}, // 1 - {1,1,0,1,1,0,1}, // 2 - {1,1,1,1,0,0,1}, // 3 - {0,1,1,0,0,1,1}, // 4 - {1,0,1,1,0,1,1}, // 5 - {1,0,1,1,1,1,1}, // 6 - {1,1,1,0,0,0,0}, // 7 - {1,1,1,1,1,1,1}, // 8 - {1,1,1,1,0,1,1}, // 9 - {1,1,1,0,1,1,1}, // A - {0,0,1,1,1,1,1}, // b - {1,0,0,1,1,1,0}, // C - {0,1,1,1,1,0,1}, // d - {1,0,0,1,1,1,1}, // E - {1,0,0,0,1,1,1}}; // F +// a,b,c,d,e,f,g + {1,1,1,1,1,1,0}, // 0 + {0,1,1,0,0,0,0}, // 1 + {1,1,0,1,1,0,1}, // 2 + {1,1,1,1,0,0,1}, // 3 + {0,1,1,0,0,1,1}, // 4 + {1,0,1,1,0,1,1}, // 5 + {1,0,1,1,1,1,1}, // 6 + {1,1,1,0,0,0,0}, // 7 + {1,1,1,1,1,1,1}, // 8 + {1,1,1,1,0,1,1}, // 9 + {1,1,1,0,1,1,1}, // A + {0,0,1,1,1,1,1}, // b + {1,0,0,1,1,1,0}, // C + {0,1,1,1,1,0,1}, // d + {1,0,0,1,1,1,1}, // E + {1,0,0,0,1,1,1}};// F void resetHex(){ for(byte i = 0 ; i < 7 ; i++){digitalWrite(segmentPins[i], LOW);} @@ -42,15 +43,18 @@ void setAddr(){ if(address > addrLimit){ address = 0; } + +} +void setAddrPins(int addr){ for (byte i=0; i<pinMax; i++) { - int state = bitRead(address, i); + int state = bitRead(addr, i); digitalWrite(addrPins[i], state); } } -void cycle(){//// +void addrCycle(){//// address = (address >= plants) ? 0 : address; setAddr(); - setHex(); + // setHex(); address++; } diff --git a/address_cycling/src/h/prototypes.h b/address_cycling/src/h/prototypes.h index 4183ab86bc8ba1feda67f4e4c6ea35ee7e835c3c..cbb995dc5432f0f402a67f08891537b9d99b45cd 100644 --- a/address_cycling/src/h/prototypes.h +++ b/address_cycling/src/h/prototypes.h @@ -1,5 +1,6 @@ -static void cycle(); - +static void addrCycle(); +static void setAddrPins(int addr); +static int makeDecision(); static void setHex(); // set hex led values static void resetHex(); static void setPins(); diff --git a/address_cycling/src/main.cpp b/address_cycling/src/main.cpp index 7ec2e6668550c4b38b6e9c1d8407463ac583f9d8..9d1d96f3b1703356fe002b19fcfc8e3ca2b903e5 100644 --- a/address_cycling/src/main.cpp +++ b/address_cycling/src/main.cpp @@ -2,66 +2,260 @@ #include <h/prototypes.h> #include <h/address_cycling.h> #include <driver/gpio.h> +#include <array> // for array, at() +#include <Wire.h> + +#define thresh 580 + +#define interval 10 // (microseconds) +#define dataSize 10 +#define MAXADDR 1 -#define interval 1500 // (milliseconds) #define write_pin 9 #define open_pin 10 +#define mosipin 14 +#define misopin 13 +#define clkpin 12 + +#define offset1 3 +#define offset2 (offset1 + 1000)//long delay to allow sensor to power on +#define offset3 (offset2 + 18 + 20) +#define offset4 (offset3 + 1000)//long delay to allow sensor to power on +#define offset5 (offset4 + 16 + 2) +#define offset6 (offset5 + 10) +#define offset7 (offset6 + 20) + -int open_state; +int cycle; +int clk; +int din; +int on; -unsigned long previousMillis = 0; // will store last time LED was updated +std::array<int,dataSize> channel1; +std::array<int,dataSize> channel2; + +int writesig; +int opensig; +int topval; +int drainval; + +unsigned long previousMicros = 0; // will store last time LED was updated // the setup function runs once when you press reset or power the board void setup() { - Serial.begin(115200); - while(!Serial); - setPins(); - plants = 16; + Serial.begin(9600); + // while(!Serial); + setPins(); + pinMode(4, OUTPUT); + analogWrite(4,220); + + plants = 15; address = 0; - open_state = LOW; + cycle = 0; + clk = 0; + din = 0; + writesig = 0; + opensig = 0; + topval = 0; + drainval = 0; + on = 1; + for(int i = 0; i < 1; i++){ + pinMode(ledPins[i],OUTPUT); + } + } // the loop function runs over and over again forever void loop() { - unsigned long currentMillis = millis(); - if (currentMillis - previousMillis >= interval) { - previousMillis = currentMillis; - // digitalWrite(open_pin, open_state); - // open_state = ~open_state; - cycle(); - } - if(address == 3){ - digitalWrite(open_pin, open_state); - open_state = ~open_state; - } - - - if (currentMillis - previousMillis >= interval/2.0) { - digitalWrite(write_pin, LOW); + unsigned long currentMicros = micros(); + if (currentMicros - previousMicros >= interval) { + previousMicros = currentMicros; + clk = ~clk; + digitalWrite(clkpin, clk); + topval = 0; + for (int i = 0; i < 10; i++){ + topval = topval + channel1[i] * pow(2, i);//find binary number + } + for (int i = 0; i < 10; i++){ + drainval = drainval + channel2[i] * pow(2, i);//find binary number + } + Serial.println(drainval); - } - else { - digitalWrite(write_pin, HIGH); - } - - - + if(clk == 0){ + cycle++; + } + //clk and cycle are the new values we just started at this halfperiod + //do stuff + if(clk==0 && cycle==0){ + //keep track of address but only selectively set the bits + //assume address is correct, will not change until end of total operation + //assume write was just set to 0 + din = 0; + digitalWrite(mosipin, din); + } + if(clk==0 && cycle==offset1){ + setAddrPins(address);//address is sent to slaves + } + if(clk==0 && cycle==(offset2)){ + din = 1; + digitalWrite(mosipin, din);//start bit + } + if(clk==0 && cycle==(offset2 + 1)){ + din = 1; + digitalWrite(mosipin, din);//single ended + } + if(clk==0 && cycle==(offset2 + 2)){ + din = 0; + digitalWrite(mosipin, din);//d2 (don't care) + } + if(clk==0 && cycle==(offset2 + 3)){ + din = 0; + digitalWrite(mosipin, din);//d1 + } + if(clk==0 && cycle==(offset2 + 4)){ + din = 0; + digitalWrite(mosipin, din);//d0 + } + if(clk!=0 && cycle==(offset2 + 7)){ + channel1[9] = digitalRead(misopin);//most sig bit of ouptut + } + if(clk!=0 && cycle==(offset2 + 8)){ + channel1[8] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 9)){ + channel1[7] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 10)){ + channel1[6] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 11)){ + channel1[5] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 12)){ + channel1[4] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 13)){ + channel1[3] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 14)){ + channel1[2] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 15)){ + channel1[1] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset2 + 16)){ + channel1[0] = digitalRead(misopin);//lsb of adc + } + if(clk==0 && cycle==(offset2 + 18)){ + setAddrPins(15);//flick address off ie pull chip select high + } + if(clk==0 && cycle==(offset3)){ + setAddrPins(address);//pull chip select back down + } + if(clk==0 && cycle==(offset4)){ + din = 1; + digitalWrite(mosipin, din);//start bit + } + if(clk==0 && cycle==(offset4 + 1)){ + din = 1; + digitalWrite(mosipin, din);//single ended + } + if(clk==0 && cycle==(offset4 + 2)){ + din = 0; + digitalWrite(mosipin, din);//d2 (don't care) + } + if(clk==0 && cycle==(offset4 + 3)){ + din = 0; + digitalWrite(mosipin, din);//d1 + } + if(clk==0 && cycle==(offset4 + 4)){ + din = 1; + digitalWrite(mosipin, din);//d0 select second channel + } + if(clk!=0 && cycle==(offset4 + 7)){ + channel2[9] = digitalRead(misopin);//most sig bit of ouptut + } + if(clk!=0 && cycle==(offset4 + 8)){ + channel2[8] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 9)){ + channel2[7] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 10)){ + channel2[6] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 11)){ + channel2[5] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 12)){ + channel2[4] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 13)){ + channel2[3] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 14)){ + channel2[2] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 15)){ + channel2[1] = digitalRead(misopin); + } + if(clk!=0 && cycle==(offset4 + 16)){ + channel2[0] = digitalRead(misopin);//lsb of adc second channel + } + if(clk==0 && cycle==(offset5)){ + opensig = makeDecision();//MAKE DECISION AND OUTPUT IT BEFORE RAISING WRITE SIGNAL + digitalWrite(open_pin, opensig);//opensig + // if(address == 1){ + // if (on == 0){ + // on = 1; + // } + // else{ + // on = 0; + // } + // } + // else{} + } + if(clk==0 && cycle==(offset6)){ + writesig = 1;//change to write operation + din = 0; + digitalWrite(mosipin, din); + digitalWrite(write_pin, writesig); + } + if(clk!=0 && cycle==(offset7)){//1 + writesig = 0; + digitalWrite(write_pin, writesig); + cycle = -1; + if (address == MAXADDR){ + address = 0; + } + else{ + address++; + } + setAddrPins(15);//flick address off + } + } +} +int makeDecision(){//change this later + return (topval < thresh)? 1 : 0; } void setPins(){ pinMode(write_pin,OUTPUT); pinMode(open_pin,OUTPUT); - digitalWrite(write_pin, LOW); - digitalWrite(open_pin, LOW); - + pinMode(clkpin,OUTPUT); + pinMode(misopin,INPUT); + pinMode(mosipin,OUTPUT); + digitalWrite(write_pin, LOW); + digitalWrite(open_pin, LOW); + digitalWrite(clkpin, LOW); + digitalWrite(mosipin, LOW); - for(int i = 0 ; i < 7 ; i ++){ + for(int i = 0 ; i < 10 ; i ++){ if(i < pinMax){ pinMode(addrPins[i], OUTPUT); digitalWrite(addrPins[i], LOW); } - pinMode(segmentPins[i],OUTPUT); } }