程序不适用于arduino leonardo

program not working on arduino leonardo

我有一个程序可以在具有串行输出的 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
  }
}

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

我会改变

int coinsChange = 0;

volatile int coinsChange = 0;