2017-06-13 251 views
-1

我在arduino.stackexchange上发布了这个问题,但我认为这个问题只能用语言(C)来完成(也许)。开启/关闭使用红外遥控器的Arduino的汽车

我有一辆车,我试图以某种方式在红外(IR)遥控器使用Arduino播放歌曲时关闭汽车。我决定用一个蜂鸣器来演奏一首歌曲(SuperMario),当我按下电源开启按钮时工作正常并且歌曲播放。

问题是当我按下电源关闭时,我必须等到歌曲结束才能关闭电源。

我在想,也许我需要线程或其他东西,但我不确定,或者有更好的方法来解决这个问题。

这里是一个演示程序:

#include "IRremote.h" 

#define NOTE_B0 31 
#define NOTE_C1 33 
#define NOTE_CS1 35 
#define NOTE_D1 37 
#define NOTE_DS1 39 
#define NOTE_E1 41 
#define NOTE_F1 44 
#define NOTE_FS1 46 
#define NOTE_G1 49 
#define NOTE_GS1 52 
#define NOTE_A1 55 
#define NOTE_AS1 58 
#define NOTE_B1 62 
#define NOTE_C2 65 
#define NOTE_CS2 69 
#define NOTE_D2 73 
#define NOTE_DS2 78 
#define NOTE_E2 82 
#define NOTE_F2 87 
#define NOTE_FS2 93 
#define NOTE_G2 98 
#define NOTE_GS2 104 
#define NOTE_A2 110 
#define NOTE_AS2 117 
#define NOTE_B2 123 
#define NOTE_C3 131 
#define NOTE_CS3 139 
#define NOTE_D3 147 
#define NOTE_DS3 156 
#define NOTE_E3 165 
#define NOTE_F3 175 
#define NOTE_FS3 185 
#define NOTE_G3 196 
#define NOTE_GS3 208 
#define NOTE_A3 220 
#define NOTE_AS3 233 
#define NOTE_B3 247 
#define NOTE_C4 262 
#define NOTE_CS4 277 
#define NOTE_D4 294 
#define NOTE_DS4 311 
#define NOTE_E4 330 
#define NOTE_F4 349 
#define NOTE_FS4 370 
#define NOTE_G4 392 
#define NOTE_GS4 415 
#define NOTE_A4 440 
#define NOTE_AS4 466 
#define NOTE_B4 494 
#define NOTE_C5 523 
#define NOTE_CS5 554 
#define NOTE_D5 587 
#define NOTE_DS5 622 
#define NOTE_E5 659 
#define NOTE_F5 698 
#define NOTE_FS5 740 
#define NOTE_G5 784 
#define NOTE_GS5 831 
#define NOTE_A5 880 
#define NOTE_AS5 932 
#define NOTE_B5 988 
#define NOTE_C6 1047 
#define NOTE_CS6 1109 
#define NOTE_D6 1175 
#define NOTE_DS6 1245 
#define NOTE_E6 1319 
#define NOTE_F6 1397 
#define NOTE_FS6 1480 
#define NOTE_G6 1568 
#define NOTE_GS6 1661 
#define NOTE_A6 1760 
#define NOTE_AS6 1865 
#define NOTE_B6 1976 
#define NOTE_C7 2093 
#define NOTE_CS7 2217 
#define NOTE_D7 2349 
#define NOTE_DS7 2489 
#define NOTE_E7 2637 
#define NOTE_F7 2794 
#define NOTE_FS7 2960 
#define NOTE_G7 3136 
#define NOTE_GS7 3322 
#define NOTE_A7 3520 
#define NOTE_AS7 3729 
#define NOTE_B7 3951 
#define NOTE_C8 4186 
#define NOTE_CS8 4435 
#define NOTE_D8 4699 
#define NOTE_DS8 4978 

#define powerLedRed 2 
#define powerLedGreen 3 
#define receiver  5 
#define buzzer   7 

void translateIR(void); 
void powerON(void);; 
void powerOFF(void); 
void swap(int *x, int *y); 
void playSuperMario(void); 
void buzz(int targetPin, long frequency, long length); 

int power   = 2; 
int switchOFF  = 0; 
int switchON  = 1; 

int melody[] = { 
    NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, 
    NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, 
    NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, 
    NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, 
    NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, 
    NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0, 
    NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, 
    NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, 
    NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, 
    NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0 
}; 

int tempo[] = { 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
}; 

int underworld_melody[] = { 
    NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_AS3, NOTE_AS4, 0, 0, 
    NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, NOTE_AS3, NOTE_AS4, 0, 0, 
    NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, NOTE_DS3, NOTE_DS4, 0, 0, 
    NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, NOTE_DS3, NOTE_DS4, 0, 0, 
    NOTE_DS4, NOTE_CS4, NOTE_D4, NOTE_CS4, NOTE_DS4, NOTE_DS4, NOTE_GS3, 
    NOTE_G3, NOTE_CS4, NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4, 
    NOTE_GS4, NOTE_DS4, NOTE_B3, NOTE_AS3, NOTE_A3, NOTE_GS3, 0, 0, 0 
}; 

int underworld_tempo[] = { 
    12, 12, 12, 12, 12, 12, 6, 3, 
    12, 12, 12, 12, 12, 12, 6, 3, 
    12, 12, 12, 12, 12, 12, 6, 3, 
    12, 12, 12, 12, 12, 12, 6, 
    6, 18, 18, 18, 6, 6, 6, 6, 6, 6, 
    18, 18, 18, 18, 18, 18, 10, 10, 10, 
    10, 10, 10, 3, 3, 3 
}; 

IRrecv irrecv(receiver); 
decode_results results; 

void setup(void) 
{ 
    pinMode(buzzer, OUTPUT); 
    pinMode(powerLedGreen, OUTPUT); 
    pinMode(powerLedRed, OUTPUT); 
    Serial.begin(9600); 
    Serial.println("IR Receiver Button Decode"); 
    irrecv.enableIRIn(); 
} 

void loop(void) 
{ 
    if (power == 2) { 
    powerOFF(); 
    } 

    if (irrecv.decode(&results)) { 
    translateIR(); 
    irrecv.resume(); 
    } 
} 

void playSuperMario(void) { 
    Serial.println(" 'Mario Theme'"); 
    int size = sizeof(melody)/sizeof(int); 
    for (int thisNote = 0; thisNote < size; thisNote++) { 
    int noteDuration = 1000/tempo[thisNote]; 
    buzz(buzzer, melody[thisNote], noteDuration); 

    int pauseBetweenNotes = noteDuration * 1.30; 
    delay(pauseBetweenNotes); 

    buzz(buzzer, 0, noteDuration); 
    } 
} 

void buzz(int targetPin, long frequency, long length) { 
    digitalWrite(13, HIGH); 
    long delayValue = 1000000/frequency/2; // calculate the delay value between transitions 
    long numCycles = frequency * length/1000; // calculate the number of cycles for proper timing 

    for (long i = 0; i < numCycles; i++) { // for the calculated length of time... 
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram 
    delayMicroseconds(delayValue); // wait for the calculated delay value 
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram 
    delayMicroseconds(delayValue); // wait again or the calculated delay value 
    } 

    digitalWrite(13, LOW); 
} 

void translateIR(void) { 
    switch (results.value) { 
    case 0xFF02FD: 
     Serial.println(" -OK-"); 

     swap(&switchON, &switchOFF); 

     if (switchON == 0) 
     { 
     powerON(); 
     playSuperMario(); 
     } 
     else 
     { 
     powerOFF(); 
     } 
     break; 
    default: 
     Serial.println(" other button "); 
     power = 0; 
    } 
    //delay(500); 
} 

void powerOFF(void) { 
    digitalWrite(powerLedRed, HIGH); 
    digitalWrite(powerLedGreen, LOW); 
    power = 0; 
} 

void powerON(void) { 
    digitalWrite(powerLedRed, LOW); 
    digitalWrite(powerLedGreen, HIGH); 
} 

void swap(int *x, int *y) { 
    if (*x != *y) { 
    *x ^= *y; 
    *y ^= *x; 
    *x ^= *y; 
    } 
} 

translateIR功能里面我有这样的:

void translateIR(void) { 
    switch (results.value) { 
    case 0xFF02FD: 
     Serial.println(" -OK-"); 

     swap(&switchON, &switchOFF); 

     if (switchON == 0) 
     { 
     powerON(); 
     playSuperMario(); 
     } 
     else 
     { 
     powerOFF(); 
     } 
     break; 
    default: 
     Serial.println(" other button "); 
     power = 0; 
    } 
    //delay(500); 
} 

这里,被称为两种功能,powerON()playSuperMario();所以我需要以某种方式断电播放歌曲期间的车。

这是VIDEO Demo。 如何解决这个问题?

回答

2

忘记线程,这是我们在这里讨论的微控制器。我建议一个基于计时器的中断,可能每100毫秒发射一次。对于人类耳朵而言,时间短,但在每秒执行1600万条指令的微中断之间会有很多时间。所以,在中断中你检查按钮是否被按下。如果有,请设置在主循环中检查的变量,如果设置,关闭旋律。

最大延迟时间= 100 ms加上几个时钟周期。

我不会为你写代码。但我会告诉你谷歌应该怎么做。首先需要阅读特定Arduino板上的微控制器的数据表。从Atmel下载。然后研究如何在CTC模式下设置定时器,如何设置预分频器,如何在您选择的编译器-IDE中为其编写中断,如何在中断期间检测按钮按下,如何设置中断主体中的变量volatile,以及如何在主循环中检查该变量并中断旋律。很多学习嵌入式编程的知识都在互联网上。从数据表开始。 ;)

0

我不会重写您的程序,使其成为多线程,因为这是回答特定问题的范围。作为一个起点,我可以给你一个描述。

首先,一个简单的方法来解决这个没有线程将是使用类似signals停止播放的曲调。这是按Ctrl-C时发生的情况。尽管你的程序正在读取来自IR的输入,但据我所知,使用线程看起来同时需要读取IR输入和播放声音。

我不熟悉Arduino,但我假设你有类似pthreads的东西。你需要熟悉这一点。这不是一个微不足道的变化,因为使用线程是非常不同的范例,如果你以前没有使用它们,需要时间让你的头脑发热。让你头脑清醒的主要原因是代码在多个地方执行。

您的主线程,任何线程创建之前的开始进程将运行loop(),因此它可以响应任何IR输入。让它在自己的线程中响应任何用户输入。代码正确的问题在于它必须等待playSuperMario()中的曲调播放才能处理任何新输入。

您将在初始化过程中创建一个线程。这个主题将会发挥什么作用。它需要能够根据IR输入启动曲调。你还需要定义事情的行为,比如在已经播放时按下“ON”按钮会发生什么?它是否重新开始,停止还是被忽略,并继续播放?

您需要一个或多个线程使用的变量。这将需要互斥量保护,因此一次只有一个线程可以读取/写入。假设有一个全局变量isPlaying,最初设置为true。当关闭按钮被按下时,这将被改变为false。您的playSuperMario()buzz()等函数需要检查它的循环中这个值是否为假。如果为false,则会立即返回。

这是一个如何处理这个希望它有所帮助的一般指针。

+1

arduino没有pthreads - 它有一个非常简单的线程实现protothreads。最好的方法可能不是查看线程(这里真的不需要),而是在计时器ISR中播放音乐。 – tofro