2016-09-20 7 views
0

私はarduinoが新しく、マルチスレッドプログラミングをしたいと思っています。 私はいくつかのコードを書きましたが、変数tempsPoseを更新するには次のコードが必要でしたが、動作しません(Ledは常に同じ速度で点滅します)。arduinoスレッドの揮発性変数の変更

この変数は

はあなたの助け

#include <Thread.h> 
Thread myThread = Thread(); 

int ledPin = 13; 
volatile int tempsPose ; 

void blinkLed13() 
{ 
    \\i would like the value of 'tempspose' to be updated 
    \\ when the value of the variable changes in the blinkLed13 function 
    while(1){ 
    digitalWrite(ledPin, HIGH); 
    delay(tempsPose); 
    digitalWrite(ledPin, LOW); 
    delay(tempsPose); 
    } 
} 


void setup() { 
    tempsPose = 100; 
    pinMode(13,OUTPUT); 
    Serial.begin(9600); 
    myThread.onRun(blinkLed13); 
    if(myThread.shouldRun()) 
    myThread.run(); 
} 

void loop() { 
    for(int j=0; j<100; j++){ 
    delay(200); 
    \\some code which change the value of 'tempsPose' 
    \\this code is pseudo code 
    tempsPose = tempsPose + 1000; 
    } 
    } 

答えて

0

のためにどうもありがとうございましループ機能にmofifiedされたとき、どのように私は、関数blinkled13の「tempsPose」変数を更新するために、次のコードを変更することができます例はすべて「ワンショット」(無限ループなし)でコードif (thread.shouldRun()) thread.run();loop()の内部にあります。私はそれがあなたのケースでは全くloop()に入ってこないと思います。

例えば

よりインタラクティブなコードは、( '+' は100ミリ秒を追加 ' - ' substractsの100ミリ秒):上記のコードで

#include <Thread.h> 

Thread myThread = Thread(); 
int ledPin = 13; 
volatile int tempsPose; 

void blinkLed13() { 
    // i would like the value of 'tempspose' to be updated 
    // when the value of the variable changes in the blinkLed13 function 
    static bool state = 0; 

    state = !state; 
    digitalWrite(ledPin, state); 

    Serial.println(state ? "On" : "Off"); 
} 

void setup() { 
    tempsPose = 100; 
    pinMode(13,OUTPUT); 
    Serial.begin(9600); 
    myThread.onRun(blinkLed13); 
    myThread.setInterval(tempsPose); 
} 

void loop() { 
    if(myThread.shouldRun()) myThread.run(); 

    if (Serial.available()) { 
    uint8_t ch = Serial.read(); // read character from serial 
    if (ch == '+' && tempsPose < 10000) { // no more than 10s 
     tempsPose += 100; 
     myThread.setInterval(tempsPose); 
    } else if (ch == '-' && tempsPose > 100) { // less than 100ms is not allowed here 
     tempsPose -= 100; 
     myThread.setInterval(tempsPose); 
    } 
    Serial.println(tempsPose); 
    } 
} 
+0

は、LED 13がblinkinkを(停止しませんが(1)関数で)しかし、遅延は時間とともに変化しません – user3052784

+0

これは決して 'blinkLed13'を残さなかったからです。その遅延を変更するために決して 'loop'に入ることはありません。これらの変更された値をシリアルに印刷するだけです。 'setup'関数と' myThread.run() 'にはまっているので、何もありません。 – KIIV

+0

あなたのコメントありがとうございますが、あなたが書いたことは本当に分かりません。私にいくつかのコードを教えてもらえますか? – user3052784

関連する問題