Close

Arduino (Teensyduino) Code

A project log for Wooden sensor box w/ 2 rotary disks

A homebuilt wooden sensor box i made, mainly for controlling PureData.

jan-goddeJan Godde 11/09/2015 at 19:010 Comments

This is the code that i am using in the second demo video, demonstrating the rotary disks. This code may be still a mess, and there may be some stupid solutions, but so far it works.

I am not reading in all the touch sensors as for this example i only need a few.

For reading the touch sensors, i simply use the "touchRead()" operator that is working with Teensyduino (Teensy 3.0 and higher). This is working quite good, but I have to tell you, that the pin readings change drastically when i touch, for example, the housing of my laptop computer. I will try to make some changes and improve my design. I posted another project log entry on this topic.

The two switches change a mode that is represented by the first 4 LEDs. The higher the mode (LEDMode: 0, 1, 2, 3, 4), the slower the values change when disks are spun. So it decreases the sensitivity of the wheels, making the wheel movements more precise.

When both switches are pressed, a second mode (LED5Mode) is changed (0, 1), displayed by LED number 5.

If you wonder about all the Serial.print() operators at the end of the sketch, this is how the data is being sent to Processing (see Processing code).

// ***** wheelController Software *****
//  ******** Jan Godde, 2015 ********
// ARDUINO SETTINGS FOR processingReceiveWheels SKETCH
//     USING PHASE VOCODER IN PD IN soundWheel.pd SKETCH

#include <Encoder.h>
#include <Bounce.h>
#define ENCODER_OPTIMIZE_INTERRUPTS
#define REVOL 308 // resolution optimized: 4 x (77 black bars)

int LEDMode = 0;
int LED5Mode = 0;
int switch1last, switch2last;
int LEDState[5][5] = { {LOW, LOW, LOW, LOW, LOW}, {HIGH, LOW, LOW, LOW, LOW}, 
                       {LOW, HIGH, LOW, LOW, LOW},
                       {LOW, LOW, HIGH, LOW, LOW}, {LOW, LOW, LOW, HIGH, LOW} };
                       
Encoder knobLeft(9, 10);
Encoder knobRight(11, 12); 
Bounce switch1 = Bounce(2, 5); // (pin number, 5 ms debounce time)
Bounce switch2 = Bounce(3, 5); // switch 1: D2, switch2: D3
// int sensorPin[20] = { A7, A6, A14, A20, A10, A11, A15, A16, //fader - fsr - knobs
//                     0, 1, 15, 16, 17, 18, 19, 22, 23, 25, 32, 33 }; //touchPins
int sensorPin[13] = { A7, A6, A14, A20, A10, A11, A15, A16, //fader - fsr - knobs
             0, 33, 32, 1, 25}; // 3 touchPins & 2 wheelTouch
int ribbonPin[2] = {A0, A19};
int ribbonValue[2] = {0, 0};
int ribbonTouched[2] = {0, 0};
int LEDPin[5] = {4, 5, 6, 7, 8};
int outputValue[15]; //with wheel encoder at last two positions

int positionLeft  = 0;
int positionRight = 0;

//int newLeft, newRight;

void setup() {  
    Serial.begin(57600);
  pinMode(A14, INPUT_PULLUP);//fsr1
  pinMode(A20, INPUT_PULLUP);//fsr2
  pinMode(A0, INPUT_PULLUP);//ribbon1
  pinMode(A19, INPUT_PULLUP);//ribbon2
  pinMode(2, INPUT_PULLUP);  //switch1
  pinMode(3, INPUT_PULLUP);  //switch2
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  LEDStateChange();
  
  if (switch1.update()){
    if(switch1.fallingEdge()) {
      switch1last = 1;
      LEDMode = mod((LEDMode-1),5);
      if (switch2last == 1){
        wheel2StateChange();
      }
    }
    if(switch1.risingEdge()) {
      switch1last = 0;
    }
  }
  
  if (switch2.update()){
    if(switch2.fallingEdge()) {
      switch2last = 1;
      LEDMode = mod((LEDMode+1),5);
      if (switch1last == 1){
        wheel2StateChange();
      }
    }
    if(switch2.risingEdge()) {
      switch2last = 0;
    }
  }

  //Serial.print(switch1last);
  //Serial.print(" ");
  //Serial.println(switch2last);

  // COLLECT SENSOR DATA:
  for(int i = 0; i < 8; i++){
    outputValue[i] = analogRead(sensorPin[i]);
  }
  for(int i = 8; i < 13; i++){
    outputValue[i] = touchRead(sensorPin[i]);
  }
  ribbonRead();
  
  int newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  //newLeft = newLeft + 1;
  //newRight = newRight + 2;
  if (newLeft != positionLeft || newRight != positionRight) {
    newLeft = mod(newLeft, (LEDMode+1)*REVOL);
    newRight = mod(newRight, (LEDMode+1)*REVOL);
    //knobLeft.write(0);
    //knobRight.write(0);
    positionLeft = newLeft;
    positionRight = newRight;
  }

  //**************************//  

  for(int i = 0; i < 13; i++){
    Serial.print(outputValue[i]); // fader, fsr, knobs, some touchpins
    Serial.print(" ");
  }
  Serial.print(newLeft); // wheel position left
    Serial.print(" ");
  Serial.print(newRight); // wheel position right
    Serial.print(" ");
  Serial.print(LEDMode); // LED Mode (0 - 4)
    Serial.print(" ");
  Serial.print(LED5Mode); // fifth LED Mode (0, 1)
    Serial.print(" ");
  Serial.print(ribbonValue[0]); // ribbon1
    Serial.print(" ");
  Serial.print(ribbonValue[1]); // ribbon2
  
  Serial.println(" ");
    delay(5);
    
  Serial.flush();
  //**************************//
  
}

void ribbonRead() {
  for (int i = 0; i < 2; i++) {
    int readSensor = analogRead(ribbonPin[i]);
    if (readSensor >= 950) {
      ribbonTouched[i] = 0;
    }
    else if (readSensor < 950) {
      ribbonTouched[i] = 1;
      ribbonValue[i] = readSensor;
    }
  }
}

void LEDStateChange(){
    digitalWrite(4, LEDState[LEDMode][0]);
    digitalWrite(5, LEDState[LEDMode][1]);
    digitalWrite(6, LEDState[LEDMode][2]);
    digitalWrite(7, LEDState[LEDMode][3]);
  }

void wheel2StateChange(){
  if (LED5Mode == 0){
    digitalWrite(8, HIGH);
    LED5Mode = 1;
  }
  else if (LED5Mode == 1){
    digitalWrite(8, LOW);
    LED5Mode = 0;
  }
}

int mod(int n, int x){ // MODULO function
  return ((n%x)+x)%x;
}

Discussions