我已经成功起草了用于电容感应的Ardunio代码。串行监视器显示正确的值。
我还成功地起草了处理代码,该代码在处理控制台上显示了来自Arduino串行监视器的完全相同的值。
但是,在读取值时,在处理绘图功能时,所需的绘图没有执行功能。而且,两个代码都可以独立正常工作。
请帮助。
此外,我的处理代码可以与其他ardunio示例代码一起正常工作。那是图纸的变化。
感谢您的帮助
Arduino代码:
#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
}
处理代码
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);
}
}