Professional Documents
Culture Documents
F2 : detaches encoder #2
#include <Servo.h>
/* define internal for the MEGA as 1.1V (as as for the 328) */
typedef struct { int pinA; int pinB; int pos; int del;} Encoder;
Servo servo[70];
void setup() {
/* Make sure all pins are put in high impedence state and
int i;
for (i=0;i<20;i++) {
pinMode(i,INPUT);
digitalWrite(i,0);
/* initialize serial */
Serial.begin(115200);
void loop() {
/* variables declaration and initialization */
if (Serial.available() >0) {
val = Serial.read();
value s=-1 */
switch (s) {
case -1:
*/
s=10*(val-48);
s=-1;
case 0:
else {
case 1:
if (val==48) {
pinMode(pin,INPUT);
else {
pinMode(pin,OUTPUT);
case 10:
case 20:
}
else {
case 21:
case 30:
case 40:
else {
case 41:
case 50:
case 60:
else {
case 61:
case 70:
/* the second value indicates the servo attachment pin
case 80:
else {
case 90:
if (val==57) {
Serial.println(1);
case 210:
/* the second value indicates the encoder number:
either 0, 1 or 2 */
else {
case 211:
else {
pinMode(Enc[enc].pinA, INPUT);
pinMode(Enc[enc].pinB, INPUT);
digitalWrite(Enc[enc].pinA, HIGH);
digitalWrite(Enc[enc].pinB, HIGH);
/* attach interrupts */
switch(enc) {
case 0:
break;
case 1:
case 2:
break;
case 220:
either 0, 1 or 2 */
/* detach interrupts */
detachInterrupt(getIntNum(Enc[enc].pinA));
detachInterrupt(getIntNum(Enc[enc].pinB));
case 230:
either 0, 1 or 2 */
Serial.println(Enc[enc].pos);
case 240:
either 0, 1 or 2 */
/* reset position */
Enc[enc].pos=0;
}
case 250:
either 0, 1 or 2 */
else {
case 251:
case 340:
switch (val) {
case 48:
analogReference(DEFAULT);
break;
case 49:
analogReference(INTERNAL);
break;
case 50:
analogReference(EXTERNAL);
break;
break;
case 400:
Serial.println(val);
default:
see http://arduino.cc/it/Reference/AttachInterrupt */
switch(pin) {
case 2:
return 0;
case 3:
return 1;
case 21:
return 2;
case 20:
return 3;
case 19:
return 4;
case 18:
return 5;
default:
return -1;
void irsPinAEn0(){
debounce(Enc[0].del);
} else {
}
} else { /* must be high to low on A */
} else {
void isrPinBEn0(){
debounce(Enc[0].del);
} else {
} else {
void irsPinAEn1(){
/* read pin B right away */
debounce(Enc[1].del);
} else {
} else {
void isrPinBEn1(){
debounce(Enc[1].del);
} else {
} else {
void irsPinAEn2(){
debounce(Enc[2].del);
} else {
} else {
void isrPinBEn2(){
} else {
} else {