该计划不适用于arduino leonardo

时间:2015-01-07 02:21:24

标签: arduino

我有一个程序可以在带有串行输出的uno上运行,但是对leonardo不起作用,只有键盘按下并释放。我正在使用硬币机械,并且在添加硬币时输出脉冲。然而,当信号从

发送的引脚松动时,它确实触发了一段时间
    const int coinInt = 0; 
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;                  
//A Coin has been inserted flag

void setup()
{
  attachInterrupt(coinInt, coinInserted, RISING);   
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()    
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 0.05;  
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;                           
//Flag that there has been a coin inserted
}

void loop()
{
  if(coinsChange == 1)          
//Check if a coin has been Inserted
  {
    coinsChange = 0;              
//unflag that a coin has been inserted
    Keyboard.press('+');
    delay(100);
   Keyboard.releaseAll();    
//Print the Value of coins inserted
  }
}

1 个答案:

答案 0 :(得分:0)

这可能不是问题,但我看到coinsChanged在中断函数中设置并在loop()中测试,但未声明为volatile。

我改变

int coinsChange = 0;

volatile int coinsChange = 0;