various fixes for grooves

This commit is contained in:
George Butskivsky 2025-05-21 14:26:17 +03:00
parent 71f791dcf5
commit cc143c08e8
1 changed files with 40 additions and 19 deletions

View File

@ -4,7 +4,7 @@
*/
#define EE_RESET 26 // любое число 0-255. Измени, чтобы сбросить настройки и обновить время
#define FEED_SPEED 500 // задержка между шагами мотора (мкс)
#define FEED_SPEED 250 // задержка между шагами мотора (мкс) def: 1500
#define DIRPIN 2 // d2 направление
#define STEPPIN 3 // d3 движение
#define BTN_PIN 4 // кнопка выдачи
@ -22,17 +22,17 @@
#define STEPPER_SPEED 250
#define STEPPER_REV 800
#define STEPPER_DELAY 500
#define STEPPER_DELAY 200
#define GROOVE_COUNT 3
#define GEAR 4*3.857 // 3.857 - это соотношение зубцов внутреннее шестеренки и белого круга с орехами
// =========================================================
#include <EEPROM.h>
#include <Thread.h>
#include <ThreadController.h>
//#include <ThreadController.h>
Thread IRLedThread = Thread();
Thread MotorThread = Thread();
ThreadController threadController = ThreadController();
//ThreadController threadController = ThreadController();
#include "microDS3231.h"
MicroDS3231 rtc;
@ -79,6 +79,8 @@ unsigned long sleepTime; // переменная для задания врем
int monitor_speed = 9600;
int failures = 0; // счетчик ошибок на IR
// GND -- [ R2 ] -- A0 -- [ R1 ] -- VIN
#define VREF 3.94 // точное напряжение на пине 5V (в данном случае зависит от стабилизатора на плате Arduino)
#define DIV_R1 10000 // точное значение 6.80 кОм резистора
@ -129,6 +131,7 @@ void setup()
void loop()
{
//empty_signal = analogRead(IRsens); // считать пустой (опорный) сигнал
static uint32_t tmr = 0;
float voltage = (float)analogRead(0) * VREF * ((DIV_R1 + DIV_R2) / DIV_R2) / 1024;
if (millis() - tmr > 500)
@ -152,8 +155,15 @@ void loop()
digitalWrite(RELAY, LOW);
delay(sleepTime);
}
if ((now.hour >= onTime && now.hour < offTime && voltage > VOLT && analogRead(IRsens) > 100 && analogRead(IRsens) < 1000 ))
{ //&& voltage > 10.5 && IR sensor is good
if (analogRead(IRsens) > 400 && analogRead(IRsens) < 1000) {
failures = 0;
}
else {
failures++;
Serial.println("Ошибок IR: " + String(failures));
}
if (now.hour >= onTime && now.hour < offTime && voltage > VOLT && failures < 60)
{ //&& voltage > 10.2
digitalWrite(RELAY, HIGH); // работаем если время рабочее И напряжение выше минимума
}
else
@ -238,30 +248,38 @@ void loop()
void feed()
{
Serial.println("Feeding...");
int counted = 0;
int counted = 0; int rev = 0;
static uint32_t tmr = 0;
EEPROM.put(3, counted); // Начинаем отсчет орехов на выдачу
//threadController.clear();
//threadController.add(&IRLedThread);
//threadController.add(&MotorThread);
feedAmount = EEPROM.read(1);
//Serial.println("IR sens: " + String(analogRead(IRsens)));
//Serial.println("Groove amount: " + String(feedAmount));
Serial.println("IR sens: " + String(analogRead(IRsens)));
Serial.println("Groove amount: " + String(feedAmount));
for (int i = 0; i < feedAmount; i++){
tmr = millis();
while (1) {
if (millis() - tmr > 10000)
if (millis() - tmr > 6000)
{
motorRev();
tmr = millis();
motorRev(); rev++;
if (rev >= 5) {
rev = 0;
tmr = millis();
}
} else {
digitalWrite(SLEEP, HIGH);
MotorThread.run();
}
digitalWrite(SLEEP, HIGH);
MotorThread.run();
IRLedThread.run();
grooveCounted = EEPROM.read(3);
if (grooveCounted > counted) {
counted = grooveCounted;
//Serial.println("Now " + String(counted) + " grooves");
delayMicroseconds(STEPPER_DELAY);
Serial.println("Now " + String(counted) + " grooves");
//delayMicroseconds(STEPPER_DELAY);
break;
}
}
@ -270,6 +288,7 @@ void feed()
}
disableMotor();
//threadController.clear();
}
void FeederMotor()
@ -280,19 +299,21 @@ void FeederMotor()
void motorRev()
{
myStepper.step(-((STEPPER_REV * GEAR) / GROOVE_COUNT));
//myStepper.step(-((STEPPER_REV * GEAR) / GROOVE_COUNT));
myStepper.step(-300);
}
void IRLedCount()
{
grooveCounted = EEPROM.read(3);
for (int i=0; i<50; i++)
for (int i=0; i<300; i++)
{
//Serial.println(String(analogRead(IRsens)));
if (analogRead(IRsens) > 980) {
//Serial.println(grooveCounted++);
Serial.println(grooveCounted++);
EEPROM.put(3, grooveCounted++);
//myStepper.step(-((STEPPER_REV * GEAR) / GROOVE_COUNT));
break;
}
}