Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CLOCK 2
- #define DATA 3
- volatile int master_count = 0; // universal count
- volatile uint8_t in_buffer = 0;
- bool secondByte = false;
- int readingOffset = 2;
- const byte numTable[] =
- {
- B00000011, //0
- B11111001, //1
- B11010010, //2
- B11101000, //3
- B10011001, //4
- B01001001, //5
- B01000001, //6
- B11111000, //7
- B10000000, //8
- B11111000 //9
- };
- void setup() {
- pinMode(DATA, INPUT_PULLUP);
- pinMode(CLOCK,INPUT);
- Serial.begin(250000);
- Serial.println("Ready for sniffing...");
- }
- void loop() {
- delayMicroseconds(300);
- master_count = 0;
- in_buffer = 0;
- attachInterrupt(digitalPinToInterrupt(CLOCK), interrupt, RISING);
- // interrupt 0 digital pin 2 positive edge trigger
- while(1)
- {
- if(master_count > 7)
- {
- detachInterrupt(digitalPinToInterrupt(CLOCK));
- // digitalWrite(DATA, LOW);
- readingOffset++;
- if(readingOffset >= 3){
- printBIN(in_buffer);
- readingOffset = 0;
- }
- in_buffer = 0;
- master_count=0;
- //delay(3);
- break;
- }
- }
- }
- void interrupt() {
- boolean read = digitalRead(DATA);
- noInterrupts();
- if(read==true)bitSet(in_buffer,(7-master_count));
- master_count++;
- interrupts();
- }
- String digitOne = "";
- String digitTwo = "";
- bool shouldPrint = false;
- int printed = 0;
- //function that also prints leading zeros
- String temp = "";
- void printBIN(volatile uint8_t* binary)
- {
- for(int byteIndex = 0; byteIndex < 8; byteIndex++){
- if(numTable[byteIndex] == binary){
- if(!secondByte){
- secondByte = true;
- if(byteIndex > 10){
- //return;
- }
- if(digitTwo != String(byteIndex, DEC)){
- digitTwo = String(byteIndex, DEC);
- shouldPrint = true;
- }
- }else{
- secondByte = false;
- if(digitOne != String(byteIndex, DEC) && digitTwo != String(byteIndex, DEC)){
- digitOne = String(byteIndex, DEC);
- shouldPrint = true;
- }
- }
- if(shouldPrint){
- if(printed == 1){
- if(temp.length() >= 2){
- temp = "";
- }else{
- temp += String(digitOne + digitTwo);
- }
- printed = 0;
- }else{
- if(temp.length() == 2){
- temp = "";
- }else{
- temp += String(digitOne + digitTwo);
- }
- printed++;
- }
- shouldPrint = false;
- if(temp.length() == 2){
- Serial.println(temp);
- temp = "";
- }else{
- }
- }
- }else{
- if(binary == B11111111){
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment