Takeup sketch

This commit is contained in:
Matt McWilliams 2023-01-14 12:38:49 -05:00
parent ed93b6da39
commit 40dd3a3dbc
1 changed files with 163 additions and 0 deletions

163
ino/takeup/takeup.ino Normal file
View File

@ -0,0 +1,163 @@
/**
* Prototype
* Arduino Uno
* L298N
**/
#define Fpos 9
#define Fneg 10
#define Bpos 5
#define Bneg 6
#define Fsignal 11
#define Bsignal 12
#define Fbutton 3
#define Bbutton 4
const int Fmotor = 0;
const int Bmotor = 1;
volatile int Fspeed = 255;
volatile int Bspeed = 255;
volatile boolean Frunning = false;
volatile boolean Brunning = false;
void setup() {
Serial.begin(57600);
Serial.println("Started takeup demo");
pinMode(Fpos, OUTPUT);
pinMode(Fneg, OUTPUT);
pinMode(Bpos, OUTPUT);
pinMode(Bneg, OUTPUT);
pinMode(Fbutton, INPUT_PULLUP);
pinMode(Bbutton, INPUT_PULLUP);
}
void loop() {
}
//input value from 0 to 255
void set_speed (int motor, int val){
if (motor == 0) {
Fspeed = val;
} else if (motor == 1) {
Bspeed = val;
}
}
void clockwise_fade (int motor, int startSpeed, int stopSpeed, int ms) {
int steps = ceil((float) ms / (float) abs(stopSpeed - startSpeed));
int stepMs = round((float) ms / (float) steps);
int motorSpeed = startSpeed;
if (motor == Fmotor) {
analogWrite(Fneg, 0);
} else if (motor == Bmotor) {
analogWrite(Bneg, 0);
}
for (int i = 0; i < steps; i++) {
if (motor == Fmotor) {
analogWrite(Fpos, motorSpeed);
Frunning = true;
} else if (motor == Bmotor) {
analogWrite(Bpos, motorSpeed);
Brunning = true;
}
delay(stepMs);
motorSpeed += stopSpeed > startSpeed ? 1 : -1;
}
if (motor == Fmotor) {
Fspeed = stopSpeed;
} else if (motor == Bmotor) {
Bspeed = stopSpeed;
}
}
void counter_clockwise_fade (int motor, int startSpeed, int stopSpeed, int ms) {
int steps = ceil((float) ms / (float) abs(stopSpeed - startSpeed));
int stepMs = round((float) ms / (float) steps);
int motorSpeed = startSpeed;
if (motor == Fmotor) {
analogWrite(Fpos, 0);
} else if (motor == Bmotor) {
analogWrite(Bpos, 0);
}
for (int i = 0; i < steps; i++) {
if (motor == Fmotor) {
analogWrite(Fneg, motorSpeed);
Frunning = true;
} else if (motor == Bmotor) {
analogWrite(Bneg, motorSpeed);
Brunning = true;
}
delay(stepMs);
motorSpeed += stopSpeed > startSpeed ? 1 : -1;
}
if (motor == Fmotor) {
Fspeed = stopSpeed;
} else if (motor == Bmotor) {
Bspeed = stopSpeed;
}
}
void clockwise (int motor){
if (motor == Fmotor) {
analogWrite(Fpos, Fspeed);
analogWrite(Fneg, 0);
Frunning = true;
} else if (motor == Bmotor) {
analogWrite(Bpos, Bspeed);
analogWrite(Bneg, 0);
Brunning = true;
}
}
void counter_clockwise (int motor){
if (motor == Fmotor) {
analogWrite(Fpos, 0);
analogWrite(Fneg, Fspeed);
Frunning = true;
} else if (motor == Bmotor) {
analogWrite(Bpos, 0);
analogWrite(Bneg, Bspeed);
Brunning = true;
}
}
void brake (){
if (Frunning) {
brakeMotor(Fmotor);
}
if (Brunning) {
brakeMotor(Bmotor);
}
}
void brakeMotor (int motor) {
if (motor == Fmotor) {
digitalWrite(Fpos, LOW);
digitalWrite(Fneg, LOW);
Frunning = false;
} else if (motor == Bmotor) {
digitalWrite(Bpos, LOW);
digitalWrite(Bneg, LOW);
Brunning = false;
}
}
/**
* Vex = higher voltage that can power the motor and/or the shield as well as the Arduino if wanted
* 5V = Must be a clean 5V that can power the Arduino and shield, and if wanted also the motor itself
* GND = ass always, the ground of everything
* B- = Connection of the first motor or first winding of the stepper motor
* B+ = Connection of the first motor or first winding of the stepper motor
* A- = Connection of the second motor or second winding of the stepper motor
* A+ = Connection of the second motor or second winding of the stepper motor
**/