0

私は AVR を初めて使用しますが、以前に PIC をプログラムしたことがあります。今回は Linux でもすべてを行っているため、avrdude を使用していると推測できます...

このプロジェクトの私の目標は、特定の状態に応じて 2 色 LED の色を変更することです。

デフォルトでは、LEDは赤です(もちろん接続を正しく行う場合)。ユーザーがボタンを押したままにしてから放すと、黄色になります(緑と赤の間のクイックチェンジです)。緑色になり、次回はオフになります。

要約すると、

LEDは赤色

ボタンを押している間は黄色になります

離すと緑色になります

ボタンをもう一度押したままにすると、黄色になります

離すとオフになります

ボタンをもう一度押したままにすると、黄色になります

離すと赤くなります(など...)

私の実際のコードでは、すべてがうまく機能することがありますが、LEDが黄色で動かなくなることがあります。その理由はわかりません

何か案は?

コードは ideone にあります: http://ideone.com/LI9gH

ありがとう

4

2 に答える 2

1

デバウンスがわずかにずれているため、ボタンがないランダムジェネレーターが約1/3の時間で変化するのを見ているだけだと思います。さまざまな州で過ごした時間を考えてみましょう。

check button
wait 10ms
check button again
if button values differ, update state
if yellow
  shine red for 1ms
  shine green for 4ms then leave green on
otherwise
  set current color

それがあなたのメインループです。ご覧のとおり、約15ミリ秒の間に2つの瞬間にボタンをチェックします。また、状態を更新した最後の値とは比較されず、10ミリ秒前の値とのみ比較されます。「黄色」の5msの期間中にボタンを離すと、allumerAmbreは、たまたま10msの期間に入る次のリリースまでリセットされません。また、黄色は1/15の赤になりましたが、意図したミックスではない可能性があります。

于 2012-01-28T14:59:21.283 に答える
1

I don't know where the problem is, but I can suggest an alternative approach if that helps. :-)

Because you're cycling through a sequence of LED states, you could merely list them in an array and step along it (wrapping around when you reach the end), updating the LED each time, whenever the button state changes.

EDIT:

Here's an alternative:

colours = [red, yellow, green, yellow, off, yellow]
current button = released
state = 0
repeat
    check button
    if button != current button
        current button = button
        state += 1
        if state >= len(colours)
            state = 0
    // showing the colour sets the LED and includes a delay
    show colours[state]
于 2012-01-28T01:46:36.670 に答える