【问题标题】:Arduino send bad signal to interrupt pinArduino 向中断引脚发送错误信号
【发布时间】:2023-02-02 22:38:32
【问题描述】:

我已将投币器和投币器连接到一个 arduino uno,投币器连接到引脚 2,投币器连接到引脚 3 - 传感器和引脚 7 - 继电器。投币器开关继电器时,正在执行投币中断

我的代码:

#define SENSOR  3
#define RELAY   7
#define ACCEPTOR 2

volatile boolean insert = false;

int pulse=0,count;               
char sen;
int temp=0;
unsigned long int timer;             

void setup()
{
  Serial.begin(9600);
  pinMode(SENSOR,INPUT_PULLUP);
  pinMode(RELAY,OUTPUT);
  sen=digitalRead(SENSOR);
  digitalWrite(RELAY, HIGH);
  attachInterrupt(digitalPinToInterrupt(ACCEPTOR), coinInterrupt, RISING);
}

void loop()
{
  if (insert) {
    insert = false;
    Serial.println("coin");
    delay(1000);
  }

  if(Serial.available())
  {
    timer=millis();
    // temp is amount to dispense send to arduino
    temp=Serial.parseInt();
    if(temp>0){
      digitalWrite(RELAY,LOW);}
  }

  sen=(sen<<1)|digitalRead(SENSOR);
  
  // if hopper sensor read drop coin
  if(sen==1)
  {
    timer=millis();
    pulse++;
    sen&=0x03;
    Serial.println("out 1");
    
    //if dispensed coins equal with coins to dispense stop engine
    if(pulse==temp)
    {
      digitalWrite(RELAY,HIGH);
      pulse=0;
      temp=0;
    }
  }

  // if amount dispensed is not equal with amount to dispense and engine running, stop
  if((digitalRead(RELAY)==LOW)&(millis()-timer>2000))
  {
    digitalWrite(RELAY,HIGH);
    pulse=0;
    temp=0;
  }
}

void coinInterrupt() {
  insert = true;
}

我试图更改引脚(arduino uno 仅支持引脚 2 和 3 上的中断)但问题仍然出现所以我猜代码中存在问题

【问题讨论】:

    标签: arduino


    【解决方案1】:

    您的草图不会在此状态下运行: 首先修复错误:

    将 insert 声明为 volatile

    删除 cpulse(未在任何地方使用)

    将“if()”更改为(我想)“if(插入)....”

    使用“sen”var 删除内容:只需使用 if(digitalRead(SENSOR)) 或 if(!digitalRead(SENSOR)) 除非你需要存储中继状态。

    使用 || 等逻辑运算符或 && 除非你真的需要按位运算

    结果草图示例:

    #define SENSOR  3
    #define RELAY   7
    
    volatile boolean insert = false;
    byte amountToDispense = 0;
    
    int pulse = 0;
    int temp = 0;
    unsigned long int timer;
    
    void setup()
    {
      Serial.begin(9600);
      pinMode(SENSOR, INPUT_PULLUP);
      pinMode(RELAY, OUTPUT);
      digitalWrite(RELAY, HIGH);
      attachInterrupt(digitalPinToInterrupt(2), coinInterrupt, RISING);
    }
    
    void loop()
    {
      if (insert ) {
        insert = false;
        Serial.println("coin");
        delay(1000);
      }
    
      if (Serial.available())
      {
        timer = millis();
        temp = Serial.parseInt();
        if (temp > 0) {
          //amountToDispense = Serial.read() - 48;
          digitalWrite(RELAY, LOW);
        }
      }
    
      if (digitalRead(SENSOR))
      {
        timer = millis();
        pulse++;
        Serial.println("out 1");
        if (pulse >= temp)
        {
          digitalWrite(RELAY, HIGH);
          pulse = 0;
          temp = 0;
        }
      }
    
      if (!digitalRead(RELAY) && (millis() - timer > 2000))
      {
        digitalWrite(RELAY, HIGH);
        pulse = 0;
        temp = 0;
      }
    }
    
    void coinInterrupt() {
      insert = true;
    }
    

    【讨论】:

    • 我在第一篇文章中修复了代码,你的解决方案没有用,发送金额分配后硬币漏斗没有停止工作(传感器不计算脉冲)我仍然有问题,分配后,漏斗发送信号中断,可能是因为电气连接不良?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    相关资源
    最近更新 更多