I have the an SMT feeder I made. Attached is the slave code. There is also a master code. I am trying to add two buttons (FORWARD/REVERSE) to make the sprocket go forward and back. The buttons I am using are momentary switches.
I would like it so that when you press the button and it connects the Attiny84 pin to GND, it moves the sprocket by 1 encoder click in the direction of the button. However, if you press and hold the button for X milliseconds, the sprocket would keep turning ignoring the encoder until you let go of the button and then it will stop the motor at the next available encoder point.
Could someone help guide me on how to do this? I need a step by step tutorial. Or if there's already sample code out there I can add to this, please let me know. Thanks!
#define __AVR_ATtiny84__
#include <Arduino.h>
#if defined(__AVR_ATtiny841__)
#define F_CPU 16000000 // clock speed: 16MHz (external crystal)
#include "WireS.h" // I2C library for ATtiny841 (and other modern ATtinys)
#else
#define F_CPU 20000000 // clock speed: 20MHz (external crystal)
#include "TinyWireS.h" // I2C library for ATtiny84A (and other older ATtinys)
#endif
#include <DRV8833.h>
DRV8833 driver = DRV8833();
#define Addr 1
int EncoderCounts = 5;
#define inputA1 PA0
#define inputA2 PA1
#define Sensor1 PA5
#define Sensor2 PA7
#define motorSpeed 255
int start = 0;
int counter = 0;
int MotorDistance;
int direction = 1; //1 -> forward nd 0-> reverse
int cDir = 0;
String data = "";
void setup() {
TinyWireS.begin(Addr);
TinyWireS.onReceive(receiveEvent); //register a function to be called when slave receive a transmission from master//
TinyWireS.onRequest(requestEvent); //register a function when master request data from this slave device
driver.attachMotorA(inputA1, inputA2);
}
void loop() {
readInput();
controlMotors();
}
void readInput() {
int value1 , value2;
value1 = digitalRead(Sensor1);
value2 = digitalRead(Sensor2);
if (start == 1 && value1 == 0) {
counter += 1;
}
if (value1 == 0 && value2 == 1) {
cDir = 0;
}
else
cDir = 1;
}
void controlMotors() {
if (counter < MotorDistance * EncoderCounts && start == 1) {
if (direction == 1) driver.motorAForward(motorSpeed);
else driver.motorAReverse(motorSpeed);
}
else if (counter >= MotorDistance * EncoderCounts && start == 1) {
driver.motorAStop();
start = 0;
}
}
//when slave receive string from master, trigger this event.
void receiveEvent(int howMany)
{
data = "";
while ( TinyWireS.available()) // execute repeatedly until last byte left in the data packet from master//
{
char c = TinyWireS.read(); // receive data from master and assign it to char c
data = data + String(c);
}
MotorDistance = data.substring(0, data.indexOf(',')).toInt();
direction = data.substring(data.indexOf(',') + 1 , data.length() + 1).toInt();
startMotor();
}
void startMotor() {
for (int i = 0 ; i < 255 ; i++) {
if (direction == 1) driver.motorAForward(i);
else driver.motorAReverse(i);
delay(5);
}
}
//trigger this event when master request data from slave,
void requestEvent()
{
}