gpt4 book ai didi

c - 在Processing/Arduino中使用延迟功能时出现高延迟

转载 作者:行者123 更新时间:2023-11-30 16:58:29 25 4
gpt4 key购买 nike

当我使用延迟功能时

  if(rpm > (rpm_max - 50))
{
arduino.analogWrite(r,255);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);
delay(15);
arduino.analogWrite(r,0);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);
delay(15);
arduino.analogWrite(r,255);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);
delay(15);

}

它落后了我的代码很多。我无法读取下一个转速值来更新我的 LED。如何在不停止我的整个代码的情况下让红色 LED 闪烁?我无法使用线程,因为 arduino 不允许。

import controlP5.*;
import hypermedia.net.*;
import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int arduinoPos = 0;
int g = 11;
int r = 10;
int b = 9;
ControlP5 cp5;
UDP udpRX;

String ip="127.0.0.1";
int portRX=20777;

int pos;

void setup(){
size(280,280);
background(255);

// Arduino connection and Servo output
arduino = new Arduino(this, Arduino.list()[1], 115200); // Your offset may vary
//println(Arduino.list());
arduino.pinMode(r,Arduino.OUTPUT);
arduino.pinMode(g,Arduino.OUTPUT);
arduino.pinMode(b,Arduino.OUTPUT);


// Create new object for receiving
udpRX=new UDP(this,portRX,ip);
udpRX.log(false);
udpRX.listen(true);
}


void draw(){

}

void receive(byte[] data){

// Function to output all the game data received
fullOutput(data);


// Time elapsed since game start
pos = 0;
float tTime = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24));

// Lap time
pos = 4;
float lapTime = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24));

// Speed, *3.6 for Km/h
pos = 28;
float speed = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24))*3.6;

// Gear, neutral = 0
pos = 132;
float gear = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24));

// Current lap, starts at 0
pos = 144;
float cLap = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24));

// RPM, requires *10 for realistic values
pos = 148;
float rpm = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24))*10;

pos = 252;
float rpm_max = Float.intBitsToFloat((data[pos] & 0xff) | ((data[pos+1] & 0xff) << 8) | ((data[pos+2] & 0xff) << 16) | ((data[pos+3] & 0xff) << 24))*10;

// Debug the received values
gameDataOutput(tTime, lapTime, speed, gear, cLap, rpm,rpm_max);

// Send the speed to the Servo
// arduinoPos = (int)map(speed, 0, 350, 1, 180); // Note that I've set the max speed to 350, you might have to change this for other games
//arduino.servoWrite(9, 180-arduinoPos);

// arduino.analogWrite(r,255);
//arduino.analogWrite(g,255);
// arduino.analogWrite(b,255);

arduino.digitalWrite(13,Arduino.HIGH);
//ligar arduino firmdata analog simple
if(rpm > (rpm_max - 50))
{
arduino.analogWrite(r,255);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);
delay(15);
arduino.analogWrite(r,0);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);
delay(15);
arduino.analogWrite(r,255);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);
delay(15);

}
else if(rpm > (rpm_max - 2000) && (rpm < (rpm_max - 50)))
{
arduino.analogWrite(r,0);
arduino.analogWrite(g,0);
arduino.analogWrite(b,255);

}
else if(rpm > (rpm_max - 3000) && (rpm < (rpm_max - 2000)))
{
arduino.analogWrite(r,0);
arduino.analogWrite(g,255);
arduino.analogWrite(b,0);

}
else if(rpm < (rpm_max - 3000))
{
arduino.analogWrite(r,0);
arduino.analogWrite(g,0);
arduino.analogWrite(b,0);

}
}

void gameDataOutput(float tTime, float lapTime, float speed, float gear, float cLap, float rpm,float rpm_max){
println("Total time: " + tTime);
println("Lap time: " + lapTime);
println("Speed: " + speed);
println("Gear: " + gear);
println("Current lap: " + cLap);
println("RPM: " + rpm);
println("RPM_MAX: " + rpm_max);
}

// Function that outputs all the received game data
void fullOutput(byte[] data){

// Loop all the received bytes
for(int i=0; i <= data.length-1; i++){

// Values consist of 4 bytes
if(i % 4 == 0){

// Combine 4 bytes to the value
float val = Float.intBitsToFloat((data[i] & 0xff) | ((data[i+1] & 0xff) << 8) | ((data[i+2] & 0xff) << 16) | ((data[i+3] & 0xff) << 24));

// Output the 'raw' value
println("Value received at position " + i + " = " + val);

}
}
}

最佳答案

要在 Arduino 上有效地执行多任务,您需要重新编写应用程序以利用中断。中断是事件,通常是定时器或 Arduino 上的引脚变化,处理器通过启动特殊中断服务例程 (ISR) 来响应。为了让您的应用程序按您的预期工作,您应该让主应用程序代码在loop() 中运行,并有一个附加到定时器中断的ISR 以使LED 闪烁。由于中断仅在计时器事件触发时运行,因此您的主应用程序可以毫无延迟地自由运行。

中断可能是一个很难学习的概念,但它们非常强大。 This可能有用。

或者,不要延迟闪烁之间的整个时间,而是允许应用程序运行并跟踪自上次闪烁以来的时间。在循环()的每个循环之后,检查自上次闪烁以来的时间是否超过了截止日期。如果有,请眨眼。否则,继续运行loop()。 This会有用的。

关于c - 在Processing/Arduino中使用延迟功能时出现高延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38798358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com