Нужно подкорректировать код

Aidos Sadak
Offline
Зарегистрирован: 22.05.2021

Собрал вендинговый аппарат воды из реле, монетоприемника HX-616, датчика расхода воды YF-S201. Мой код не работает, нужно подкорректировать код или при необходимости, полностью переписать.

#include <LCD_1602_RUS.h>
LCD_1602_RUS lcd(0x27, 16, 2);

int coinpin=2;
int relay = 13;                 
int start_stop = 8;             
const int sensor_pulse =12;     



volatile int pulse = 0;
volatile unsigned long timelastPulse = 0;
boolean coinInsert = false;
int buttonState = 0;
int credits = 0;
boolean currentstart_stop = LOW;         
boolean laststart_stop =LOW;             
boolean lastsensor_pulse = LOW;
boolean currentsensor_pulse = LOW;
boolean relayState = LOW;                


float cal = 2.5;                        
float counter = 0.0; 
int TotalCount = 0;
int set_point = 0;  

void setup(){
  pinMode(relay, OUTPUT);     
  pinMode(coinpin,INPUT_PULLUP);
  lcd.init();                      
  lcd.backlight();
}


   
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}




void loop(){

  currentstart_stop = debounce(laststart_stop, start_stop);    
  currentsensor_pulse = debounce(lastsensor_pulse, sensor_pulse); 


 
  if (currentstart_stop == HIGH && laststart_stop == LOW){
    
    if (relayState == HIGH){         
      digitalWrite(relay, LOW);
      relayState = LOW;
    } 
    else{
      digitalWrite(relay, HIGH);
      relayState = HIGH;
    }
  }
  
  laststart_stop = currentstart_stop;





if(relayState == LOW){  

attachInterrupt(digitalPinToInterrupt(coinpin),coinInterrupt,FALLING);

 unsigned long lastTime = millis() - timelastPulse;
 if((pulse > 0) && (lastTime > 24)){
  coinInsert = false;
  credits += pulse;
  pulse = 0;
 }
 
if (credits == 1){  
    set_point = set_point +500;
  }  

else if  (credits == 2){  
    set_point = set_point +1000;
  }  

else if  (credits == 3){  
    set_point = set_point +1500;
  }  

else if  (credits == 4){  
    set_point = set_point +2000;
  }        

else if  (credits == 5){  
    set_point = set_point +2500;
  }  

else if  (credits == 6){ 
    set_point = set_point +3000;
  }  

else if  (credits == 7){  
    set_point = set_point +3500;
  }  

else if  (credits == 8){  
    set_point = set_point +4000;
  }    

else if  (credits == 9){  
    set_point = set_point +4500;
  }  

else if  (credits == 10){  
    set_point = set_point +5000;
  }    

else {
  set_point = 0;
counter = 0;         
TotalCount = 0;
  }        
   

   

  if(relayState == HIGH){    
  if (lastsensor_pulse== LOW && currentsensor_pulse == HIGH){
      counter = counter + cal;
  }
  }
 lastsensor_pulse = currentsensor_pulse;
 

if(counter >= 10){
  TotalCount = TotalCount + 10;
  counter = 0;                     
  }

 lcd.setCursor(3, 0);           
 lcd.print(set_point);
 lcd.setCursor(4, 1);           
 lcd.print(TotalCount);

 
 if(set_point <= TotalCount){ 
  relayState = LOW;
  digitalWrite(relay, LOW);
  TotalCount=0;
  counter=0;
 }

}
}

void coinInterrupt(){
  pulse++ ;
  timelastPulse = millis();
}

 

man9913
Offline
Зарегистрирован: 19.03.2016

3тр, если монетник рабочий и подключен правильно

man9913@mail.ru