ClawMachineCosplay/ClawMachineOverhaul.ino

84 lines
1.9 KiB
Arduino
Raw Normal View History

2024-06-01 02:22:57 +02:00
#include "StepperController.h"
#include "StepperXController.h"
#include "Time.h"
#define JoystickPin 63
2024-06-01 02:22:57 +02:00
2024-06-01 05:18:44 +02:00
#define grabPin 19
2024-06-01 02:22:57 +02:00
Time time;
void setup(){
// Start serial communication at 115200 baud
Serial.begin(115200);
// Set the pin mode for the defined pins
2024-06-01 05:18:44 +02:00
pinMode(grabPin, INPUT_PULLUP);
2024-06-01 02:22:57 +02:00
initSteppers();
initXStepper();
}
bool isGrabbing = false;
float startGrabbingTime = 0;
const float moveToDropZoneTime = 1750; // How long to move right (relative to me)
const int joyCriticalPoint = 10;
2024-06-01 02:22:57 +02:00
void loop(){
// Updates deltatime
time.update();
stepperXLoop();
// Joystick. TODO: Make this only run when needed. Too lazy now.
int joystickValueRaw = analogRead(JoystickPin);
int joystickValue = map(joystickValueRaw, 0, 1023, 0, 100);
bool left = joystickValue > 100-joyCriticalPoint;
bool right = joystickValue < joyCriticalPoint;
2024-06-01 02:22:57 +02:00
// Serial.print("Left: ");
// Serial.print(left);
// Serial.print(" Right: ");
// Serial.println(right);
2024-06-01 02:22:57 +02:00
if (isGrabbing && millis() > startGrabbingTime + moveToDropZoneTime) {
isGrabbing = false;
processJoystickInput(false, false); // Quickly stop motor
delay(1000);
moveStepper(StepperAxis::Head, 10000, 100); // open hand
}
2024-06-01 02:22:57 +02:00
processJoystickInput(left, right);
2024-06-01 02:22:57 +02:00
if (digitalRead(grabPin) == LOW){
beginGrabSequence();
//initGrabber();
}
2024-06-01 05:18:44 +02:00
// //Serial.print(millis());
2024-06-01 02:22:57 +02:00
// Serial.print("Left state: ");
// Serial.print(left);
// Serial.print(" | ");
// Serial.print("Right state: ");
// Serial.println(right);
2024-06-01 02:22:57 +02:00
delay(10); // delay in milliseconds
}
void initGrabber(){
moveStepper(StepperAxis::Head, 100000, 1000);
2024-06-01 02:22:57 +02:00
}
void beginGrabSequence(){
moveStepper(StepperAxis::Head, 10000, 100); // open hand
2024-06-01 05:18:44 +02:00
moveStepper(StepperAxis::Y, 10000, -1100);
moveStepper(StepperAxis::Head, 1000, -100, 100); // close hand
2024-06-01 05:18:44 +02:00
moveStepper(StepperAxis::Y, 10000, 1100);
isGrabbing = true;
startGrabbingTime = millis();
2024-06-01 02:22:57 +02:00
}