2016-09-20 4 views
0

저는 arduino를 처음 접했고 멀티 스레드 프로그래밍을 원합니다. 몇 가지 코드를 썼는데 다음 코드가 tempsPose 변수를 업데이트하려고했지만 작동하지 않습니다 (Led가 항상 같은 속도로 깜박임).arduino 스레드 업데이트 휘발성 변수

방법이 변수가

는 당신의 도움을 주셔서 대단히 감사합니다 루프 기능에 mofified 때 나는 함수 blinkled13에서 'tempsPose'변수를 업데이트하기 위해 다음 코드를 변경할 수 있습니다

#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

예는 모두 "원샷"(무한 루프 없음)이고 if (thread.shouldRun()) thread.run(); 코드는 loop() 안에 있습니다. 그래서 나는 당신의 경우에 loop()에 전혀 들어 가지 않을 것이라고 생각합니다.

더 쌍방향 코드 ('+'는 100ms로를 추가 '-'substracts를 100ms로) : 상기 코드

#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

, 발광 다이오드 (13)는 blinkink을 (중단되지 않지만 (1) 함수)하지만 지연 시간은 시간에 따라 변하지 않습니다. – user3052784

+0

그것은 'blinkLed13'을 결코 남겨 두지 않았기 때문입니다. 지연을 변경하기 위해 결코 루프에 들어 가지 않습니다. 이 변경된 값을 직렬로 인쇄하면됩니다. 'setup' 함수와'myThread.run()'에 갇혀 있기 때문에 아무 것도 없을 것입니다. – KIIV

+0

당신의 의견을 보내 주셔서 감사합니다. 그러나 당신이 쓴 것을 정말로 이해하지 못합니다. 좀 코드 좀 보여 주시겠습니까? – user3052784