/** This app checks the rotation position of
a rotary encoder and waits to send result on Wire
Pete Rogers 31/01/2009 rotaryEncoderMaster01
steps number is 400
**/
#include -Wire.h-
//constants pin declarations
int encoder0PinA = 2;
int encoder0PinB = 4;
int val;
int byte1;
int byte2;
volatile int encoder0Pos = 0;
void setup(){
//encoder setup
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH);
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH);
attachInterrupt(0, doEncoder, CHANGE); // using interrupt to call doEncoder function
Wire.onRequest(requestEvent); // register event
Wire.begin(4);
}
void loop(){
delay(100);
}
void doEncoder(){
if (digitalRead(encoder0PinA) == HIGH) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos = encoder0Pos - 1;
} else {
encoder0Pos = encoder0Pos + 1;
}
}else {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos = encoder0Pos + 1;
}
else {
encoder0Pos = encoder0Pos - 1;
}
}
val = encoder0Pos;
}
void requestEvent(){
byte i[] = {(byte)(val & 0xff),(byte)(val >> 8)};
Wire.send(i,2);
}
>>>>>>>>>>>>>
code for stepper
<<<<<<<<<<<
/** stepper app controls the position and speed of stepper motor.
position determined by the sensor num both + and -
Pete Rogers 30/01/2009
steps number is 400
**/
#include -Wire-
//constants pin declarations
int pinA = 9;
int pinB = 7;
int pinC = 10;
int pinD = 8;
int sensorPin = 0;
//stepper sequence at half step for higher resolution
int A[8] = {LOW,LOW,LOW,LOW,HIGH,HIGH,HIGH,LOW};
int B[8] = {LOW,LOW,HIGH,HIGH,HIGH,LOW,LOW,LOW};
int C[8] = {HIGH,HIGH,HIGH,LOW,LOW,LOW,LOW,LOW};
int D[8] = {HIGH,LOW,LOW,LOW,LOW,LOW,HIGH,HIGH};
//sketch variables
int cycle = 0;
int stepperPos = 0;
int newPos = 0;
int mySpeed = 2000;
void setup(){
pinMode (pinA, OUTPUT);
pinMode (pinB, OUTPUT);
pinMode (pinC, OUTPUT);
pinMode (pinD, OUTPUT);
Serial.begin(9600);
Wire.begin(4); // join i2c bus with address #4
//Wire.onReceive(receiveEvent); // register event
// start serial for output
}
void loop(){
//delay(50);
checkPosition();
}
void checkPosition(){
while (true){
//if in correct position turn stepper off (cools it down)
if (newPos == stepperPos){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
receiveEvent();
}
// moves clockwise to new pos
if (newPos > stepperPos){
cycle ++;
stepperPos ++;
moveStepper(mySpeed);
break;
}
// moves anticlockwise to new pos
if (newPos < stepperPos){
cycle --;
stepperPos --;
moveStepper(mySpeed);
break;
}
}
}
void moveStepper(int delayTime){
//sort cycle array ints
if (cycle == 8){
cycle = 0;
}
if (cycle == -1){
cycle = 7;
}
//send to stepper
digitalWrite(pinA, A[cycle]);
digitalWrite(pinB, B[cycle]);
digitalWrite(pinC, C[cycle]);
digitalWrite(pinD, D[cycle]);
delayMicroseconds(delayTime);
}
void receiveEvent(){
Wire.requestFrom(4,2);
while(Wire.available()){
int byte1 = Wire.receive();
int byte2 = Wire.receive();
if(byte1 == 0 | byte2 == 0 ){
} else{
newPos = (byte2 << 8) | byte1 ;
newPos = newPos / 10;
Serial.println(newPos);
//Serial.println(byte2);
}
}
}