Forked from jeffnoko/gist:8754a0e611c3d67a18fea872dd19a8ea
Last active
December 30, 2017 02:05
-
-
Save jmpinit/150460d1b098db94b3f2b5e3c04e3735 to your computer and use it in GitHub Desktop.
Draw lines controlling direction with rotary encoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<AccelStepper.h> | |
#include <MultiStepper.h> | |
#include <Servo.h> | |
#include <Rotary.h> | |
#include <Wire.h> | |
Servo servo1; | |
Servo servo2; | |
Servo servo3; | |
MultiStepper steppers; | |
Rotary rotary = Rotary(18,19); | |
const int analogInPin1 = A1; // Analog input pin that the potentiometer is attached to | |
const int analogInPin2 = A2; // Analog input pin that the potentiometer is attached to | |
int sensorValue1 = 0; // value read from the pot | |
int sensorValue2 = 0; // value read from the pot | |
int outputValue1 = 0; // value output to the PWM (analog out) | |
int outputValue2 = 0; // value output to the PWM (analog out) | |
AccelStepper stepper1(1, 2, 3); | |
AccelStepper stepper2(1, 10, 11); | |
//AccelStepper stepper1(1,2,3); //set up the accelStepper intance, the "1" tells it we are using a driver | |
//AccelStepper stepper2(1,10,11); | |
AccelStepper stepper3(1, 5, 6); | |
volatile int state = LOW; | |
int stepper1TotalSteps = 0; | |
int stepper2TotalSteps = 0; | |
int stepper1Steps; | |
int stepper2Steps; | |
int POTs; | |
int POTu; | |
int POTt; | |
int POTv; | |
int voltageS = A13; | |
int voltageT = A12; | |
float voltageU = A11; | |
float voltageV = A10; | |
float previousVoltageV; | |
int voltageQ = A3; | |
int i = 0; | |
int pos = 130; | |
int currentpos = 0; | |
int pos1 = random(1000, 13000); | |
int pos2 = random(1000, 13000); | |
int xx; | |
int yy; | |
long positions[2]; | |
int x, y; | |
float point1,point2; | |
int length = random(300, 1000); | |
//float angle = 0.0; | |
float angle = 0.0; | |
// float angle_stepsize = PI/4; | |
float angle_stepsize = 0.08; | |
/// float angle = PI/2; | |
// float angle_stepsize = PI/6; | |
int anglef1; | |
float anglef2; | |
int angle3; | |
float anglef3; | |
int newAngle; | |
float newAnglef; | |
int newLength = 70; | |
int x2; | |
int y2; | |
int ppp, qqq; | |
int xxx, yyy; | |
float incr1,incr2; | |
int counter = 0; | |
float g = counter *.26; | |
float P1x; | |
float P1y; | |
float P2x; | |
float P2y; | |
int centerX = 0; | |
int centerY = 0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(9, INPUT_PULLUP); | |
pinMode (A13, INPUT); | |
pinMode (A12, INPUT); | |
pinMode (A11, INPUT); | |
pinMode (A10, INPUT); | |
pinMode (A3, INPUT); | |
pinMode (A5, INPUT_PULLUP); | |
servo1.attach(8);//paint | |
servo1.write(90); | |
servo2.attach(9);//trigger | |
servo2.write(165); | |
attachInterrupt(4, rotate, CHANGE);//0 on uno | |
attachInterrupt(5, rotate, CHANGE);//1 on uno | |
stepper1.setMaxSpeed(800); | |
stepper1.setAcceleration(1000); | |
stepper2.setMaxSpeed(800); | |
stepper2.setAcceleration(1000); | |
stepper3.setMaxSpeed(2000); | |
stepper3.setAcceleration(2000); | |
steppers.addStepper(stepper1); | |
steppers.addStepper(stepper2); | |
} | |
void loop() | |
{ | |
static float previousAngle = 0; | |
unsigned long seed = seedOut(31); | |
randomSeed(seed); | |
delay(2000); | |
voltageQ = analogRead(A3); | |
voltageQ = map(voltageQ, 0, 1023, 500, 2000); //speed | |
int speed1 = voltageQ; | |
stepper1.setMaxSpeed(speed1); | |
stepper2.setMaxSpeed(speed1); | |
s1up(); | |
float newAngle = counter; | |
voltageQ = analogRead(A3); | |
voltageQ = map(voltageQ, 0, 1023, 500, 2000); //speed | |
int speed1 = voltageQ; | |
stepper1.setMaxSpeed(speed1); | |
stepper2.setMaxSpeed(speed1); | |
newAngle = counter *.13; | |
Serial.print("previousAngle");Serial.println(previousAngle); | |
Serial.print("newAngle");Serial.println(newAngle); | |
voltageQ = analogRead(A3); | |
voltageQ = map(voltageQ, 0, 1023, 500, 2000); | |
int radius = 500; | |
stepper3.setMaxSpeed(1000); | |
if( newAngle > previousAngle ){ // make a new center x y | |
Serial.println("hello1"); | |
// calcutlate a new center and P1x,P1y. divide it in steps keep P2x, P2y | |
centerX = P2x + radius * cos(newAngle); | |
centerY = P2y + radius * sin(newAngle); | |
}; | |
if( newAngle < previousAngle ){ | |
Serial.println("hello2"); | |
newAngle = newAngle + 3.14; | |
centerX = P1x + radius * cos(newAngle); | |
centerY = P1y + radius * sin(newAngle); | |
newAngle = newAngle - 3.14; | |
}; | |
s1up(); | |
P1x = centerX + radius * cos(newAngle); // P1x = x | |
P1y = centerY + radius * sin(newAngle);// P1y = y | |
positions[0] = P1x; | |
positions[1] = P1y ; | |
steppers.moveTo(positions); | |
steppers.runSpeedToPosition(); | |
s1down(); | |
newAngle = newAngle + 3.14; | |
P2x = centerX + radius * cos(newAngle); | |
P2y = centerY + radius * sin(newAngle); | |
positions[0] = P2x; | |
positions[1] = P2y ; | |
steppers.moveTo(positions); | |
steppers.runSpeedToPosition(); | |
////////////to get the direction 90 degree from line | |
newAngle = newAngle - 1.57; | |
int newRadius = 50; | |
centerX = centerX + newRadius * cos(newAngle); | |
centerY = centerY + newRadius * sin(newAngle); | |
newAngle = newAngle - 1.57; | |
///////////////////////////////////// | |
previousAngle = newAngle; | |
previousAngle = newAngle; | |
} | |
//////////////////////////////////////////////////////////////////////// | |
void rotate() { | |
unsigned char result = rotary.process(); | |
if (result == DIR_CW) { | |
counter++; | |
// Serial.println(counter); | |
} else if (result == DIR_CCW) { | |
counter--; | |
// Serial.println(counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment