-
Notifications
You must be signed in to change notification settings - Fork 9
Description
I about gave up on this.
Keep trying
// reciever/master rot
#include <Wire.h> ;
void setup() {
pinMode(1, OUTPUT); // visualize rotory
pinMode(3, OUTPUT); // visualize button
// Wire.begin(); // join i2c bus (address optional for master)
}
void loop() {
// digitalWrite(3, HIGH);
// analogWrite(1, 255);
while (true) {
Wire.requestFrom(4, 2); // request 2 byte from rotory device ( value, button ) address 4
if (Wire.available() ) { // Ensure we have received 2 bytes
uint8_t value = Wire.read(); // Read the first byte
uint8_t value2 = Wire.read(); // Read the second byte
analogWrite(1, value);
analogWrite(3, value2);
/*
if (value > 1) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
if (value2 > 1) {
analogWrite(1, value2);
} else {
digitalWrite(1, LOW);
}
*/
}
delay(100);
}
}
// example rot + button
#include "avr/interrupt.h";
#define I2C_ROTORY_ADDRESS 0x04 // Address of the slave
#include <Wire.h>
volatile int value = 122;
volatile int lastEncoded = 0;
void setup() {
Wire.begin(I2C_ROTORY_ADDRESS); // join i2c network
//TinyWireS.onReceive(receiveEvent); // not using this
Wire.onRequest(requestEvent);
// set pins 3 and 4 to input
// and enable pullup resisters
pinMode(3, INPUT);
pinMode(4, INPUT);
//pinMode(2, INPUT);
pinMode(1, INPUT);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
GIMSK = 0b00100000; // Enable pin change interrupts
PCMSK = 0b00011000; // Enable pin change interrupt for PB3 and PB4
sei(); // Turn on interrupts
}
// Gets called when the ATtiny receives an i2c request
void requestEvent() {
/*
byte button;
if (digitalRead(1)==HIGH){
button = 255;
} else {
button = 0;
}
byte j = value;
byte message[2] = {button, j};
Wire.write(message, 2);
*/
//byte message[2];
//message[0] = 0x25;
//message[1] = 0x75;
Wire.write(0xa0);
Wire.write(0x45);
//Wire.write(message, 2);
//uint16_t combined = (button << 8) | value;
//Wire.write((byte*)&combined, 2); // Send the 2-byte combined value
}
void loop() {
delay(100);
}
// This is the ISR that is called on each interrupt
// Taken from http://bildr.org/2012/08/rotary-encoder-arduino/
ISR(PCINT0_vect) {
int MSB = digitalRead(3); //MSB = most significant bit
int LSB = digitalRead(4); //LSB = least significant bit
int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
value++;
if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
value--;
lastEncoded = encoded; //store this value for next time
if (value <= 0)
value = 0;
if (value >= 255)
value = 255;
}