Created
May 14, 2016 02:15
-
-
Save SysOverdrive/ecec199739ac655b5a368dc8700b4638 to your computer and use it in GitHub Desktop.
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
#include <Wire.h> | |
#define SLAVE_ADDRESS 0x04 | |
int number = 0; | |
int state = 0; | |
//ENCODER PART------------------ | |
unsigned long timp1, timp2; | |
float frecventa; | |
//PINS | |
int interuptPin = 3; | |
//SDA A4 | |
//SCLK A5 | |
void setup() { | |
//ENCODER PART------------------ | |
timp1 = millis(); | |
attachInterrupt(digitalPinToInterrupt(interuptPin), ISRFunction, RISING); | |
//----------------------------- | |
Serial.begin(9600); // start serial for output | |
Wire.begin(SLAVE_ADDRESS);// initialize i2c as slave | |
// define callbacks for i2c communication | |
Wire.onReceive(receiveData); | |
Wire.onRequest(sendData); | |
Serial.println("Ready!"); | |
} | |
void loop() { | |
delay(100); | |
} | |
// callback for received data | |
void receiveData(int byteCount) { | |
while (Wire.available()) { | |
number = Wire.read(); | |
Serial.print("yo nigga I got new data : "); | |
Serial.println(number); | |
} | |
} | |
// callback for sending data | |
void sendData() { | |
Wire.write(number); | |
} | |
//ENCODER PART------------------ | |
void ISRFunction() | |
{ | |
frecventa = 1000000 / (float)(micros() - timp2); //Din cauza decentrarii avem variatie de 10 Hz | |
timp2 = micros(); | |
Serial.println(frecventa); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment