Go Down

Topic: Arduino and Processing are taking but drawing function not working (Read 1 time) previous topic - next topic

arduinocrazy369

Hello,

I have successfully drafted the Ardunio code for capacitive sensing. The serial Monitor shows right values.

Also, I have successful drafted the processing code which shows the exact same values from Arduino's serial monitor on processing's console.

However,  on processing the drawing function is not doing by desired drawing when reading values. Moreover, both codes are working fine independently.

Please help.

Moreover, my processing code works fine with other ardunio example code. That is drawings change.

Thank you for helping

**Arduino Code:**

```
#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
 */


CapacitiveSensor   cs_4_2 = CapacitiveSensor(A4,A2);        // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil


void setup()                   
{

   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);

}

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

    //Serial.println(millis() - start);        // check on performance in milliseconds
    //Serial.println("\t");                    // tab character for debug window spacing

    Serial.println(total1);                  // print sensor output 1
    Serial.println("\t");


    delay(1000);                             // arbitrary delay to limit data to serial port
}
```


**Processing Code**


```
import processing.serial.*;



import processing.serial.*; // import the Processing serial library
Serial myPort;  // The serial port
int sensor1;
int sensor2;
int sensor3;
float mappedSensor1;
float mappedSensor2;
float mappedSensor3;
PImage Gifimg;
PImage firstimg;
PImage secondimg;



PImage Frontimg;  // Declare variable "a" of type PImage

// Declare variable "a" of type PImage

void setup() {
  size(1080, 800);
  // List all the available serial ports in the console
  printArray(Serial.list());
   Gifimg = loadImage("secondsmall.png");  // Load the image into the program
   Frontimg = loadImage("Frontsmall.png");  // Load the image into the program 
   secondimg = loadImage("finalsmall.png");  // Load the image into the program 

   
   

  // Change the 0 to the appropriate number of the serial port
  // that your microcontroller is attached to.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  // read incoming bytes to a buffer
  // until you get a linefeed (ASCII 10):
  myPort.bufferUntil('\n');
}


void draw() {
  background(255);

  if (sensor1 == 0) {
image(secondimg, 0, 0);
image(Gifimg, 0, 0);



}
else {
image(Frontimg, 0, 0);

 

  }

}


void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  if (myString != null) {
    // println(myString);
    myString = trim(myString);

    // split the string at the commas
    // and convert the sections into integers:
    int sensors[] = int(split(myString, ','));
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
    }
    // add a linefeed at the end:
    println();
    sensor1 = sensors[0]; 
   
    mappedSensor1 = map(sensor1, 0, 1023, height, 0);
   
  }
}
```



Grumpy_Mike

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

I can't see how you can ever get a value of 0 from the Arduino code, so the else case will always be shown.

The draw command should check if changed data has arrived before drawing anything or else it will constantly be drawing.

Also try and not put blank lines in functions it makes them difficult to read, and only have one blank line between functions.

Go Up