当我在
中使用延迟功能时 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);
}
它严重滞后于我的代码。我无法读取下一个rpm值来更新我的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);
}
}
}
答案 0 :(得分:0)
要在Arduino上有效地执行多任务,您将需要重新编写应用程序以利用中断。中断是Arduino上的事件,通常是定时器或引脚更改,处理器通过启动特殊中断服务程序(ISR)来响应。为了使您的应用程序按预期工作,您应该让主应用程序代码在loop()中运行并且具有连接到定时器中断的ISR以使LED闪烁。因为中断仅在定时器事件触发时运行,所以主应用程序可以立即自由运行。
中断可能是一个难以学习的概念,但它们非常强大。 This可能有用。
或者,不要在闪烁之间延迟整个时间,而是允许应用程序运行并跟踪自上次闪烁以来的时间。循环()的每个循环后,检查自上次闪烁以来的时间是否超过截止日期。如果有,眨眼。否则,继续运行loop()。 This会有用。