我可以在使用Arduino超声波传感器处理移动/播放时制作图像序列吗?

悉尼

这是我的第一篇文章,因此,如果有任何不清楚之处,我会提前道歉,并且我将尝试快速学习。在编程方面,我还是一个新手。使用Arduino Uno,超声波传感器HC-SR04,Processing3.5.3

在处理和Arduino草图中,我提供了能够播放图像序列的功能,并且当超声传感器拾取到物体的距离时,处理控制台中将打印“ 1”。

我想知道是否可以使用此“ 1”来创建if语句。如果控制台打印的数字大于0,则将播放图像序列-否则,将只绘制一张图像(gif将暂停)。我已经尝试了几个版本,但是我不想假装自己知道自己在做什么。

任何让我关注的线索都将很棒!或在线教程!

我感觉好像有些简单的东西我只是想念而已...我想没有什么比这更简单了。感谢您的时间 :))

阿根廷代码:

#define trigPin 9
#define echoPin 10

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);           //CHANGE THIS??
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  distance = pulseIn(echoPin, HIGH);

  if (distance <= 2000) { //raise this number to raise the distance to wave hand
    Serial.print("1");
  } else {
    Serial.print("0");
  }
  delay(500);  
}

处理方式

import processing.serial.*;

int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] image = new PImage[22];
Serial myPort;
String instring = "";

void setup() {
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');
  size(1600, 900);
  frameRate(30);
  background(0);
  
  //Load images below
  for(int i = 0; i<image.length; i++) {
    image[i] = loadImage("PatMovingFace" + i + ".png");
  }
}

void draw() {
  //ALL BELOW connects to arduino sensor
  while (myPort.available () > 0) {
    instring = myPort.readString();
    println(instring);
    int sensorAct = Integer.parseInt(instring, 10);
  }
  playGif();   
}

// ALL BELOW will make the pic array animate! Image moves! 

void playGif() {
  currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
  int offset = 0;

  for (int i = 0; i < image.length; i++) {
    //image(image[i] % numFrames), 0, 0);
    image(image[(currentFrame + offset) % numFrames], 0, 0);
    offset += 0;
    image(image[(currentFrame + offset) % numFrames], 0, 0);
    offset+= 0;
  }
}
乔治·普罗芬扎

您可以先简化/清理playGif()

  • offset 目前似乎什么也没做(它从0“递增”到0)
  • image()被调用两次,具有相同的坐标,从而在其顶部覆盖相同的内容。一旦image()打电话应该做
  • 您可能不需要for循环,因为您一次绘制一帧
  • currentFrame是递增到下一帧并在的开头循环返回到起点playGif(),但是,在渲染图像时,数组索引将offset再次递增(0)。目前尚不清楚其意图是什么。你可以没有
  • currentFrame可以控制播放/暂停:如果递增,则播放,否则暂停。它的好,有更新数据之间(分离currentFrame),并用更新的数据渲染(image(image[currentFrame],0,0)
  • 您还可以将image数组重命名为images以便更清楚地表示其含义,并且更难以混淆数组image()函数

例如,playGif()可能变成:

void playGif(){
      currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
      image(image[currentFrame], 0, 0);
}

关于使用Arduino的控制,如上所述,只需检查您是否获得“ 1”来更新currentFrame(否则它将保持(暂停)在相同的值):

void playGif(){
      if(instring.equals("1")){
         currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
      }
      image(image[currentFrame], 0, 0);
}

我正在equals()上面使用,因为它是一个String,即使您要发送一个字符。(或者比较第一个字符会起作用if(instring.charAt(0) == '1')

我也有一些注意事项:

  • 在您使用Arduino的代码print(),不println(),这意味着没有\n将被发送,因此,没有必要myPort.bufferUntil('\n');,也不instring = myPort.readString();
  • 您可以使用读取单个字符myPort.read();(可以将其与==(而不是String equals()进行比较(例如if(myPort.read() == '1'){...
  • while正在阻止,我建议不要使用它。因为您要发送一个字节,所以这不会对您造成很大的影响,但是在发送更多字节的更复杂的程序上,这将阻止Processing渲染单个帧,直到接收到所有数据为止

这是一个例子:

import processing.serial.*;

int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
Serial myPort; 

void setup() {
  myPort = new Serial(this, Serial.list()[1], 9600);  
  
  size(1600, 900);                                    
  frameRate(30);                                      
  background(0);

  //Load images below
  for (int i = 0; i < numFrames; i++)
  { 
    images[i] = loadImage("PatMovingFace" + i + ".png");
  }
}

void draw() {
  // if there's at least one byte to read and it's '1'
  if(myPort.available() > 0 && myPort.read() == '1'){
    // increment frame, looping to the start
    currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
  }
  // render current frame
  image(images[currentFrame], 0, 0);
}

一个更谨慎的版本,检查可能出问题的地方(串行连接,数据加载),如下所示:

import processing.serial.*;
    
int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
Serial myPort; 

void setup() {
  String[] ports = Serial.list();
  int portIndex = 1;
  if(ports.length <= portIndex){
    println("serial ports index " + portIndex + " not found! check cable connection to Arduino");
    println("total ports: " + ports.length);
    printArray(ports);
    exit();
  }
  try{
    myPort = new Serial(this, ports[portIndex], 9600);  
  }catch(Exception e){
    println("error connecting to serial port: double check the cable is connected and no other program (e.g. Serial Monitor) uses this port");
    e.printStackTrace();
  }  
  size(1600, 900);                                    
  frameRate(30);                                      
  background(0);

  //Load images below
  try{
    for (int i = 0; i < numFrames; i++)
    { 
      images[i] = loadImage("PatMovingFace" + i + ".png");
    }
  }catch(Exception e){
    println("image loading error");
    e.printStackTrace();
  }
}

void draw() {
  // if Arduino connection was successfull
  if(myPort != null){
    // if there's at least one byte to read and it's '1'
    if(myPort.available() > 0 && myPort.read() == '1'){
      // increment frame, looping to the start
      currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
    }
  }else{
    text("serial port not initialised", 10, 15);
  }
  
  if(images[currentFrame] != null){
    // render current frame
    image(images[currentFrame], 0, 0);
  }else{
    text("serial port not loaded", 10, 15);
  }
}

或使用功能分组时具有相同的功能:

import processing.serial.*;

int numFrames = 22; //number of frames in animation
int currentFrame = 0;
PImage[] images = new PImage[numFrames];

final int SERIAL_PORT_INDEX = 1;
final int SERIAL_BAUD_RATE  = 9600;
Serial myPort; 

void setup() {
  size(1600, 900);                                    
  frameRate(30);                                      
  background(0);
  
  setupArduino();
  //Load images below
  loadImages();
}

void setupArduino(){
  String[] ports = Serial.list();
  int numSerialPorts = ports.length;
  // optional debug prints: useful to double check serial connection 
  println("total ports: " + numSerialPorts);
  printArray(ports);
  // exit if requested port is not found  
  if(numSerialPorts <= SERIAL_PORT_INDEX){
    println("serial ports index " + SERIAL_PORT_INDEX + " not found! check cable connection to Arduino");
    //exit();
  }
  // try to open port, exit otherwise
  try{
    myPort = new Serial(this, ports[SERIAL_PORT_INDEX], SERIAL_BAUD_RATE);  
  }catch(Exception e){
    println("error connecting to serial port: double check the cable is connected and no other program (e.g. Serial Monitor) uses this port");
    e.printStackTrace();
    //exit();
  }
}

void loadImages(){
  try{
    for (int i = 0; i < numFrames; i++)
    { 
      images[i] = loadImage("PatMovingFace" + i + ".png");
    }
  }catch(Exception e){
    println("image loading error");
    e.printStackTrace();
    //exit();
  }
}

void serialUpdateImage(){
  // if Arduino connection was successfull
  if(myPort != null){
    // if there's at least one byte to read and it's '1'
    if(myPort.available() > 0 && myPort.read() == '1'){
      // increment frame, looping to the start
      currentFrame = (currentFrame+1) % numFrames; //use % to cycle through frames!
    }
  }else{
    text("serial port not initialised", 10, 15);
  }
}

void displayCurrentImage(){
  if(images[currentFrame] != null){
    // render current frame
    image(images[currentFrame], 0, 0);
  }else{
    text("image " + currentFrame + " not loaded", 10, 25);
  }
}

void draw() {
  
  serialUpdateImage();
  
  displayCurrentImage();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我可以在使用Arduino超声波传感器处理移动/播放时制作图像序列吗?

来自分类Dev

超声波传感器HC-SR04 + Arduino计算?

来自分类Dev

Arduino UNO +以太网屏蔽+超声波传感器=失败

来自分类Dev

arduino uno和超声波传感器的结果不正确

来自分类Dev

如何与HM-19 BLE模块通讯并使用超声波传感器进行扫描

来自分类Dev

在树莓派中使用超声波传感器测量距离

来自分类Dev

使用 python 用 BBB 读取多个超声波传感器

来自分类Dev

为什么使用arduino uno,sr04超声波传感器和9个led不能使用此代码?

来自分类Dev

我有一张图片 18F4550 如何从超声波传感器读取距离

来自分类Dev

在特定区域内分布许多超声波传感器

来自分类Dev

我可以使用 FFMPEG 来录制超声波吗?

来自分类Dev

Arduino + HC SR04超声波+电位计

来自分类Dev

用于 Arduino 的传感器可以与 Raspberry Pi 一起使用吗?

来自分类Dev

是否可以通过超声波音频信号将移动设备与嵌入式设备进行通信

来自分类Dev

如何在JavaScript中播放超声波WAV / PCM文件而不丢失数据

来自分类Dev

我可以在android中同时在两个应用程序中使用陀螺仪传感器吗?

来自分类Dev

简单的Verilog即可控制MD1715超声波驱动器

来自分类Dev

距离小于4厘米时伽利略和超声波误差

来自分类Dev

我可以在浏览器中使用github页面操作图像吗?

来自分类Dev

当手绕传感器移动时,接近传感器打开

来自分类Dev

我如何制作图像,以便可以单击它并播放声音

来自分类Dev

我如何制作图像,以便可以单击它并播放声音

来自分类Dev

我可以使用光传感器来测量两次闪光之间的时间间隔吗?有什么我可以使用的课程吗?

来自分类Dev

在Arduino中读取传感器时按下按钮

来自分类Dev

使用 tkinter GUI 触发传感器时,让 pygame 播放声音

来自分类Dev

我可以保存可穿戴设备(MOTO 360)的传感器数据吗?

来自分类Dev

我可以在同一服务中收听两个传感器吗?

来自分类Dev

使用Twilio的Arduino Uno PIR运动传感器

来自分类Dev

可以使用iBeacon广播传感器门的打开/关闭吗?

Related 相关文章

  1. 1

    我可以在使用Arduino超声波传感器处理移动/播放时制作图像序列吗?

  2. 2

    超声波传感器HC-SR04 + Arduino计算?

  3. 3

    Arduino UNO +以太网屏蔽+超声波传感器=失败

  4. 4

    arduino uno和超声波传感器的结果不正确

  5. 5

    如何与HM-19 BLE模块通讯并使用超声波传感器进行扫描

  6. 6

    在树莓派中使用超声波传感器测量距离

  7. 7

    使用 python 用 BBB 读取多个超声波传感器

  8. 8

    为什么使用arduino uno,sr04超声波传感器和9个led不能使用此代码?

  9. 9

    我有一张图片 18F4550 如何从超声波传感器读取距离

  10. 10

    在特定区域内分布许多超声波传感器

  11. 11

    我可以使用 FFMPEG 来录制超声波吗?

  12. 12

    Arduino + HC SR04超声波+电位计

  13. 13

    用于 Arduino 的传感器可以与 Raspberry Pi 一起使用吗?

  14. 14

    是否可以通过超声波音频信号将移动设备与嵌入式设备进行通信

  15. 15

    如何在JavaScript中播放超声波WAV / PCM文件而不丢失数据

  16. 16

    我可以在android中同时在两个应用程序中使用陀螺仪传感器吗?

  17. 17

    简单的Verilog即可控制MD1715超声波驱动器

  18. 18

    距离小于4厘米时伽利略和超声波误差

  19. 19

    我可以在浏览器中使用github页面操作图像吗?

  20. 20

    当手绕传感器移动时,接近传感器打开

  21. 21

    我如何制作图像,以便可以单击它并播放声音

  22. 22

    我如何制作图像,以便可以单击它并播放声音

  23. 23

    我可以使用光传感器来测量两次闪光之间的时间间隔吗?有什么我可以使用的课程吗?

  24. 24

    在Arduino中读取传感器时按下按钮

  25. 25

    使用 tkinter GUI 触发传感器时,让 pygame 播放声音

  26. 26

    我可以保存可穿戴设备(MOTO 360)的传感器数据吗?

  27. 27

    我可以在同一服务中收听两个传感器吗?

  28. 28

    使用Twilio的Arduino Uno PIR运动传感器

  29. 29

    可以使用iBeacon广播传感器门的打开/关闭吗?

热门标签

归档