Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

projets:turntable:accueil

Ceci est une ancienne révision du document !


Turntable

  • Porteur du projet : resonance
  • Date : 12/02/2019
  • Licence : libre !
Utilisation d'un encoder photo-electrique de (400 pas/tour) pour se balader dans une table audio d'un patch puredata.

code arduino

/***************************************************
  Two phase quadrature encoder(Incremental)
* ****************************************************
   To determine motor with encode (CW OR CCW)
 
  @author Dong
  @version  V1.0
  @date  2016-5-26
  All above must be included in any redistribution
* ****************************************************/
#define  A_PHASE 2
#define  B_PHASE 3
double flag_A = 0;  //Assign a value to the token bit
double flag_B = 0;  //Assign a value to the token bit
double pos = 0;
double newpos = 0;
 
/** *  */
 
// set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int previousButtonState = 0;
 
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP); 
  //
  pinMode(A_PHASE, INPUT);
  pinMode(B_PHASE, INPUT);
  Serial.begin(9600);   //Serial Port Baudrate: 9600
  //Interrupt trigger mode: RISING
  attachInterrupt(digitalPinToInterrupt( A_PHASE), interrupt, RISING); 
  previousButtonState = digitalRead(buttonPin);
}
void loop() {
 
  if (pos != newpos){
  Serial.print("enc ");
  Serial.println(pos);
  delay(10);// Direction judgement
  newpos = pos;
  }
 
  //
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
 
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH && previousButtonState == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    Serial.println("btn 1");
    delay(20);
  } 
  if (buttonState == LOW && previousButtonState == HIGH) {     
    // turn LED off:    
    digitalWrite(ledPin, LOW);  
    Serial.println("btn 0");
    delay(20);
  } 
  previousButtonState = buttonState;
 
}
void interrupt()// Interrupt function
{ char i;
  i = digitalRead( B_PHASE);
  if (i == 1)
  {
    flag_A += 1;
  }
  else
  {
    flag_B += 1;
  }
  pos = flag_A - flag_B;
}
/home/resonancg/www/wiki/data/attic/projets/turntable/accueil.1555337908.txt.gz · Dernière modification: 2019/04/15 16:18 de resonance