Go Down

Topic: arduino+processing+touch sensor (Read 2634 times) previous topic - next topic

Erika86

Hi all.
I'm making a prototype with two buttons, two leds and one touch sensor. The circuit should do this: at first when the button are released nothing happens; if I press (nothing happens again) and release them, the leds starts to fade in and out. If this last condition is satisfied and I press the touch sensor, a song has to be played.
All works except for the touch sensor because the song is played even if I don't touch the sensor. The problem is inside the led code because if I turn off that tab, the sensor replies in the right way. It seems that when the leds are activated they send the input to the sensor and it is read by the processing that plays the sound. Here the code:

Arduino

MAIN TAB

const int BUTTON2 = 7;     // the number of the pushbutton pin
const int BUTTON1 = 2;     // the number of the pushbutton pin
const int LED2 =  9;      // the number of the LED pin
const int LED1 =  10;      // the number of the LED pin


unsigned long start;
boolean timer = false;
unsigned long end_fadeIn;
unsigned long end_fadeOut;
int fadePeriod = 2000;

const int knockSensor = 5; // the sensor is connected to analog pin 5
const int THRESHOLD = 100;  // threshold value to decide when the detected sound is a knock or not

int tempo = 0;

// these variables will change:
int sensorReading;      // variable to store the value read from the sensor pin

int state;
int fade1 = 0;
int fade2 = 0;
int incr1 = 1;
int incr2 = 1;

boolean ledOn = false; //boolean to lets the sensor play the sound just if LEDs are turned on

void setup()
{
  pinMode(LED2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(knockSensor, INPUT); // declare the sensor as as INPUT
  Serial.begin(9600);       // use the serial port
}

void loop()

  led();
  if (ledOn) {
  pressSensor();
  }
}


LEDs TAB


void led() {

  switch (state) {
  case 0:                           
    if(digitalRead(BUTTON2)==HIGH && digitalRead(BUTTON1)==HIGH){   //initial state: buttons pressed, nothing happens
      state=1;
       
    }
ledOn = false;
    break;

  case 1:       

    if(digitalRead(BUTTON2)==LOW && digitalRead(BUTTON1)==LOW){       //the two buttons are released at the same time


      //the timer indicates the fade time
      if (timer == false){
        start = millis();
        timer = true;
         
      }

      //fading time
      end_fadeIn = start + fadePeriod;
      end_fadeOut = end_fadeIn + fadePeriod;
      //Serial.println (start);
      if (millis()<=end_fadeIn){
        fade1 = fade2 = map(millis(), start, end_fadeIn, 0, 255);
        analogWrite(LED1, fade1);
       analogWrite(LED2, fade2);
      }

      if (millis()>=end_fadeIn && millis()<=end_fadeOut){
        fade1 = fade2 = map(millis(), end_fadeIn, end_fadeOut, 255, 0);
        analogWrite(LED1, fade1);
       analogWrite(LED2, fade2);
      }

    if (millis()>end_fadeOut){
    timer = false;
      }
    }
    ledOn = true;
    break;

  case 2:              //at first when the buttons are released nothing happens

    state = 0;         
    ledOn = false;
    break;
  }
}


SENSOR TAB


void pressSensor() {
 
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);   
 
  if ((millis() - tempo) > 100) {
 
  // if the sensor reading is greater than the threshold:
  if (sensorReading <= THRESHOLD)
  {       
    Serial.print(1, BYTE);               // send 1 to Processing
    tempo = millis();
  }
  else {
   
    Serial.print(0, BYTE);               // send 0 to Processing
  }
 
  }
}




PROCESSING CODE

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

   import processing.serial.*;
   
   Serial port;
   
   int val;
   
   Minim minim;
   AudioPlayer prova;


   
   
   void setup()
   {
      minim = new Minim(this);
      prova = minim.loadFile("01 - Fango.mp3", 2048); 
   
     println(Serial.list());
   
     port = new Serial(this, Serial.list()[0], 9600);
   
     size(10, 10);
     
   }
   
   
   void draw()
   { 
   
   
   
 
      if (1 < port.available()) {
       val = port.read();
       println(val);
   
     }

     if (val == 1) {
        val = 0;
       prova.play();
       }

   }
   
   void stop()
   {
   
     prova.close();
   
     minim.stop();
   
     super.stop();
   }




ANY SUGGESTION????

Go Up