Arduino读取读取DHT11和触摸传感器数据传给processing程序显示声音和动画
作者:野牛程序员:2023-12-08 15:36:39Arduino阅读 2605
读取DHT11和触摸传感器代码
#include <DHT.h>
#define DHTPIN 2 // DHT11连接到Arduino的数字引脚
#define TOUCHPIN 3 // 触摸传感器连接到Arduino的数字引脚
DHT dht(DHTPIN, DHT11);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// 读取湿度
float humidity = dht.readHumidity();
// 处理湿度信息
if (!isnan(humidity)) {
Serial.print("Humidity:");
Serial.print(humidity);
Serial.println();
} else {
Serial.println("无法读取湿度数据");
}
delay(500);
// 读取触摸传感器状态
int touchValue = digitalRead(TOUCHPIN);
touchValue =1;
// 处理触摸信息
if (touchValue != HIGH) {
Serial.print("Touch:");
Serial.println(touchValue);
Serial.println();
// 在这里可以添加触摸触发的动作代码
}
delay(500);
}processing程序
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
import processing.serial.*;
Serial myPort; // 创建一个Serial对象
int lastTouchValue = 0; // 用于存储上一次触摸传感器的值
int lastHumidity = 0; // 用于存储上一次湿度传感器的值
boolean isTouching = false; // 标记是否检测到触摸
int starttime;
int dlytime =4000;
Minim minim;
AudioPlayer player;
void hands(int x){
stroke(0);
strokeWeight(10);
if(x==1){
fill(249,217,70);//黄色
}else{
fill(32,116,160);//蓝色
}
triangle(80,335,30,380,80,420);
triangle(330,335,380,380,330,420);
}
void body(int x){
if(x==1){
fill(249,217,70);//黄色
} else{
fill(200,17,70);//
}
stroke(0);
strokeWeight(5);
ellipse(205,200,250,250);
rect(80,200,250,220);
}
void eyes_glasses(int x){
stroke(0);
strokeWeight(5);
if(x==1){
fill(0);
}else{
fill(56);
}
rect(85,200,240,20);
if(x==1){
fill(255);
}else{
fill(155);
}
ellipse(160,200,90,90);
ellipse(250,200,90,90);
strokeWeight(30);
point(160,200);
point(250,200);
}
void mouth(){
stroke(0);
strokeWeight(10);
line(180,300,230,300);
}
void cloth_(int x){
noStroke();
if(x==1){
fill(32,116,160);//蓝色
}else{
fill(132,116,160);
}
rect(120,350,170,95);
noFill();
stroke(0);
strokeWeight(10);
rect(175,380,60,75);
strokeWeight(5);
line(120,350,290,350);
line(120,350,120,420);
line(290,350,290,420);
stroke(32,116,160);//蓝色
strokeWeight(25);
line(80,340,115,350);
line(330,340,290,350);
stroke(0);
point(115,350);
point(290,350);
}
void cloth(int x){
//fill(32,116,160);//蓝色
if(x==1){
fill(32,116,160);//蓝色
}else{
fill(132,116,160);
}
stroke(0);
strokeWeight(5);
ellipse(205,420,250,250);
}
void legs(){
stroke(0);
strokeWeight(40);
line(185,540,185,590);
line(226,540,226,590);
line(150,590,190,590);
line(226,590,266,590);
}
void drawini(){
cloth(1);
hands(1);
body(1);
//cloth_(1);
//eyes_glasses(1);
//legs();
//mouth();
}
void drawb(){
cloth(0);
hands(0);
body(0);
cloth_(0);
eyes_glasses(0);
legs();
mouth();
}
void setup() {
size(420,640);
minim = new Minim(this);
player = minim.loadFile("D:\\\\aaa.MP3");
myPort = new Serial(this, "COM3", 9600); // 替换"COM3"为你的Arduino连接的串口号
}
void draw() {
// 处理触摸信号
if (myPort.available() > 0) {
String data = myPort.readStringUntil('\\n');
if (data != null) {
data = trim(data);
if (data.startsWith("Touch:")) {
int touchValue = int(data.substring(6));
if (touchValue==0) {
isTouching = true;
starttime =millis();
singAndJump();
} else{
isTouching = false;
}
lastTouchValue = touchValue;
// 显示触摸传感器的值
displayTouchValue(touchValue);
} else if (data.startsWith("Humidity:")) {
int humidityValue = int(data.substring(9));
lastHumidity = humidityValue;
if(humidityValue<=130){
sing();
// 显示湿度传感器的值
displayHumidityValue(humidityValue);
}
}
}
}
if (millis()-starttime>dlytime){
background(255);
//delay(5000);
drawini();
}
}
void displayTouchValue(int value) {
fill(0);
textSize(16);
text("Touch Sensor Value: " + value, 10, 20);
}
void displayHumidityValue(int value) {
fill(0);
textSize(16);
text("Humidity Sensor Value: " + value, 10, 40);
}
void singAndJump() {
drawb();
}
void sing() {
if(!player.isPlaying()){
player.play();
}
}
void beep() {
float duration = 0.5;
float freq = 440.0;
float amplitude = 0.5;
for (float t = 0.0; t < duration * 2; t += 1.0 / 44100.0) {
float wave = sin(2.0 * PI * freq * t);
float envelope = 1.0 - max(0.0, min(1.0, 4.0 * t / duration - 1.0));
float sample = wave * envelope * amplitude;
delay(2);
}
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:C++求字符串中字母的个数
- 下一篇:processing程序播放MP3
