diff --git a/platformio.ini b/platformio.ini index 7216e60..16b65bf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,5 +16,6 @@ lib_deps = paulstoffregen/Encoder@^1.4.2 andrealombardo/L298N@^2.0.3 waspinator/AccelStepper@^1.64 + stepper upload_port = COM4 monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp index 31a5c47..2d6f6c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,21 @@ #include #include "motorControl.h" +#include "shooting.h" motorControl controller; +shooting shooter; stepStruct reading; +//previous count for shooting to ensure fire one puck at a time +int prevCount = 0; + //function prototype: void serialCommunicate(); void setup() { Serial.begin(115200); controller.setup(); + shooter.setup(); while (!Serial) { ; // Wait for the serial port to be ready } @@ -46,9 +52,9 @@ void loop() { void serialCommunicate(){ if (Serial.available() > 0) { String inputString = Serial.readStringUntil('\n'); // Read until newline - + // Separate the comma-separated values - int values[4]; + int values[5]; int currentIndex = 0; int commaIndex = inputString.indexOf(','); while (commaIndex != -1) { @@ -59,12 +65,18 @@ void serialCommunicate(){ } // Handle the last value - if (currentIndex < 4) { + if (currentIndex < 5) { values[currentIndex] = inputString.toInt(); } //write the values to the arduino controller.updateMotors(values[0], values[1], values[2], values[3]); + + //loading and shooting values write to arduino + if(values[4] > prevCount){ + shooter.shoot(); + prevCount = values[4]; + } } //read from the motor reading = controller.readSteppers(); @@ -75,6 +87,8 @@ void serialCommunicate(){ Serial.print(reading.step3); Serial.print(','); Serial.print(reading.step4); + Serial.print(','); + Serial.print(prevCount); Serial.println(); //move steps diff --git a/src/shooting.cpp b/src/shooting.cpp new file mode 100644 index 0000000..739acc8 --- /dev/null +++ b/src/shooting.cpp @@ -0,0 +1,29 @@ +#include "shooting.h" + +shooting::shooting() :myStepper(200, IN1, IN2, IN3, IN4){ +// Constructor initializer list to initialize myStepper +} + +shooting::~shooting() { + +} + +void shooting::setup() { + myStepper.setSpeed(25); + pinMode(Solenoid, OUTPUT); +} + + +void shooting::shoot(){ + //Shooting Code, may need to adjust delays + digitalWrite(Solenoid, HIGH); + delay(100); + digitalWrite(Solenoid, LOW); + myStepper.step(-130); //Switch Solenoid ON + //delay(100); + //Switch Solenoid OFF + //Reload code + //need to verify rotation duration/trial&error? + //delay(300); + myStepper.step(130); +} \ No newline at end of file diff --git a/src/shooting.h b/src/shooting.h new file mode 100644 index 0000000..e4f6ba0 --- /dev/null +++ b/src/shooting.h @@ -0,0 +1,28 @@ +#ifndef shooting_h +#define shooting_h + +#include +#include + + +class shooting { +public: + shooting(); //constructor + virtual ~shooting(); //virtual destructor + void setup(); + void shoot(); + +protected: + //solenoid pin + int Solenoid = 50;//pin value + + //motors + int IN1 = 42;//1 + int IN2 = 44;//2 + int IN3 = 46;//3 + int IN4 = 48;//4 + + Stepper myStepper;// Declare Stepper instance as a member variable +}; + +#endif \ No newline at end of file