#define hzclk 7 #define hzbinary 6 #define sigLED 11 #include "LedControl.h" LedControl lc=LedControl(12,11,10,1); /* we always wait a bit between updates of the display */ unsigned long delaytime=250; uint8_t hzclkval = 0; uint8_t hzbinval[30]; uint8_t clkval = 0; uint32_t milliscompare = 0; uint8_t readyet = 0; uint8_t x = 0; uint16_t mainsfreq = 0; struct { unsigned int mainstwo : 14; //pesky binary wants a struct of } bitfieldA; //only exact amt of bits int temp = 0; void setup() { // put your setup code here, to run once: pinMode(hzclk, INPUT); pinMode(hzbinary, INPUT); pinMode(sigLED, OUTPUT); Serial.begin(115200); /* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */ lc.shutdown(0,false); /* Set the brightness to a medium values */ lc.setIntensity(0,8); /* and clear the display */ lc.clearDisplay(0); } void loop() { if(digitalRead(hzbinary) == LOW){ //this handles one clk cycle each //need to read 20 cycles clkval = 0; checkCLKandBIN();checkCLKandBIN();checkCLKandBIN(); checkCLKandBIN();checkCLKandBIN();checkCLKandBIN(); checkCLKandBIN();checkCLKandBIN();checkCLKandBIN(); checkCLKandBIN();checkCLKandBIN();checkCLKandBIN(); checkCLKandBIN();checkCLKandBIN();checkCLKandBIN(); checkCLKandBIN(); checkCLKandBIN(); checkCLKandBIN(); checkCLKandBIN(); checkCLKandBIN(); checkCLKandBIN(); checkCLKandBIN(); //Serial.println(clkval); //outputs 19 or 20 ConvertArraytoBin(); mainsfreq = 0; bitfieldA.mainstwo = 0; } delayMicroseconds(100); //must be lower than 1 millisecond, so mic /*if hzbinary is low * * while clk high, do nothing * clk++ * sample hzbinary * while clk low do nothing */ /* * //serial print is very slow, and breaks timing * the below is not usable * (DMA anyone? second processor anyone?) Serial.println("hzbinval 1 - 20:"); for(x=0;x<21;x++){ Serial.println(hzbinval[x]); } Serial.print("\n\n\n\n"); */ //sample on falling edge (middle of bit) } void checkCLKandBIN(void){ /* * while clk high, do nothing * clk++ * sample hzbinary * while clk low do nothing */ //milliscompare = millis(); while((hzclkval = digitalRead(hzclk)) == HIGH){ if(readyet == 0){ hzbinval[clkval] = digitalRead(hzbinary); clkval++; readyet = 1; } delayMicroseconds(10); } while((hzclkval = digitalRead(hzclk)) == LOW){ delayMicroseconds(1); readyet = 0; } //digitalWrite(sigLED, HIGH); //delayMicroseconds(1); //digitalWrite(sigLED, LOW); //would be nice to add a picture to this source code (waveform) //or oscope picture //why isn't this possible? } //https://playground.arduino.cc/Main/LedControl/#Seg7Control void printNumber(int v) { int ones; int tens; int hundreds; int thousands; boolean negative; if(v < -9999 || v > 9999) return; if(v<0) { negative=true; v=v*-1; } ones=v%10; v=v/10; tens=v%10; v=v/10; hundreds=v%10; v=v/10; thousands=v; /*if(negative) { //print character '-' in the leftmost column lc.setChar(0,4,'-',false); } else { //print a blank in the sign column lc.setChar(0,4,' ',false); }*/ //Now print the number digit by digit lc.setDigit(0,3,(byte)thousands,false); lc.setDigit(0,2,(byte)hundreds,false); lc.setDigit(0,1,(byte)tens,false); lc.setDigit(0,0,(byte)ones,false); } void ConvertArraytoBin (void){ for(x=4;x<21;x++){ if(hzbinval[x] == 1){ bitfieldA.mainstwo = bitfieldA.mainstwo | (0b0000000000000000000001 << (x - 3)); } } //Serial.println(bitfieldA.mainstwo,BIN); temp = bitfieldA.mainstwo >> 1; //almost, but no cigar. has timing issues. //ugly hack time temp = temp & 0b1011111111111; //results in a clock that is right most of the time. //but wrong every 20 or so counts //todo: fix timing of duino by removing digital read for //direct port reads if(temp == 5047){ temp = 5998; //something, something, premature optimization... } if(temp == 5048){ temp = 6000; //LUTs } Serial.println(temp,BIN); Serial.println(temp,DEC); Serial.print("\n"); lc.clearDisplay(0); printNumber(temp); delay(delaytime); /*mainsfreq = mainsfreq >> 3; mainsfreq = mainsfreq & 0b0111111111111; Serial.println(mainsfreq,DEC); //111011011101 //1011101101111 //111011011101 //11101101111*/ }