Trouble using capacitive sensor in Processing!

edited March 2017 in Arduino

Hello,

I'm novice at using Processing.

In my code I want to change the background to white when the capacitive sensor is held down, and then back to black when the sensor is released.

I have attempted at writing this code in both capacitive sensor code in Arduino, however when I run my code in Processing nothing is shown or changes.

Any help would be greatly appreciated.

MY ARDUINO CODE IS BELLOW

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);             

void setup(){

  cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     
  Serial.begin(9600);
}

void loop(){

  long start = millis();
  long total1 =  cs_4_2.capacitiveSensor(30);

  Serial.println(total1); 
  Serial.print(",");                

  delay(10);                              
}

MY PROCESSING CODE IS BELLOW

import processing.serial.*;

int touch1Value = 0;

int threshold = 30;

Serial myPort;

void setup(){

  size (500,500);

  myPort = new Serial(this, Serial.list()[1], 9600);

  myPort.bufferUntil('\n');
}

void draw(){

  background (0);

  println("touch1Value:"+touch1Value);

  if (touch1Value == 1){

    background (255,255,255);
  }
}

void serialEvent(Serial myPort){

  String inString = myPort.readStringUntil('\n');

  if (inString != null){

    inString = trim (inString);

    float[] touches = float (split(inString, ","));

    if (touches.length >=1){

      touch1Value = touches[0] >= threshold ? 1: 0;
    }
  }
}

Answers

  • Try removing line 17 in your ino code and see if that fixes it.

    Kf

  • Success, can't believe I missed that!

  • I am also trying to make a capacitive sensor display a random image each time my sensor is held down.

    I have added all the images I want to be included in this randomisation, however when my sensor is held down it skips constantly through all of my images when I infect just want one to be displayed at a time.

    The key thing I want to keep is the fact that the sensor has to be held down and not pressed.

    Here is my Processing code altered:

    import processing.serial.*;
    
    int touch1Value = 0;
    
    int threshold = 30;
    
    PImage p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25;
    PImage [] picArray = new PImage [26];
    
    Serial myPort;
    
    void setup(){
    
      background (255,255,255);
    
      size (500,500);
    
      myPort = new Serial(this, Serial.list()[1], 9600);
    
      myPort.bufferUntil('\n');
    
      p0 = loadImage("0.png");
      p1 = loadImage("1.png");
      p2 = loadImage("2.png");
      p3 = loadImage("3.png");
      p4 = loadImage("4.png");
      p5 = loadImage("5.png");
      p6 = loadImage("6.png");
      p7 = loadImage("7.png");
      p8 = loadImage("8.png");
      p9 = loadImage("9.png");
      p10 = loadImage("10.png");
      p11 = loadImage("11.png");
      p12 = loadImage("12.png");
      p13 = loadImage("13.png");
      p14 = loadImage("14.png");
      p15 = loadImage("15.png");
      p16 = loadImage("16.png");
      p17 = loadImage("17.png");
      p18 = loadImage("18.png");
      p19 = loadImage("19.png");
      p20 = loadImage("20.png");
      p21 = loadImage("21.png");
      p22 = loadImage("22.png");
      p23 = loadImage("23.png");
      p24 = loadImage("24.png");
      p25 = loadImage("25.png");
    
      picArray[0] = p0;
      picArray[1] = p1;
      picArray[2] = p2;
      picArray[3] = p3;
      picArray[4] = p4;
      picArray[5] = p5;
      picArray[6] = p6;
      picArray[7] = p7;
      picArray[8] = p8;
      picArray[9] = p9;
      picArray[10] = p10;
      picArray[11] = p11;
      picArray[12] = p12;
      picArray[13] = p13;
      picArray[14] = p14;
      picArray[15] = p15;
      picArray[16] = p16;
      picArray[17] = p17;
      picArray[18] = p18;
      picArray[19] = p19;
      picArray[20] = p20;
      picArray[21] = p21;
      picArray[22] = p22;
      picArray[23] = p23;
      picArray[24] = p24;
      picArray[25] = p25;
    
      imageMode(CENTER);
    }
    
    void draw(){
    
      background (255,255,255);
    
      println("touch1Value:"+touch1Value);
    
      if (touch1Value == 1){
    
      image(picArray[int(random(picArray.length))], 250, 250);
      }
    }
    
    void serialEvent(Serial myPort){
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString != null){
    
        inString = trim (inString);
    
        float[] touches = float (split(inString, ","));
    
        if (touches.length >=1){
    
        touch1Value = touches[0] >= threshold ? 1: 0;
        }
      }
    }
    
This discussion has been closed.