Hi All
I have made a Useless Machine with a printer (DC motor with encoder) and a Servo.
I need some help with making the code
The way i want the useless machine to work is the order you turn the switches on it will turn them off.
So i need to track the position of 10 switches and turn them off in the right order.
I know of a ring buffer but i have no idea how to use it.
I dont no much so take it easy on me.
My useless machine video My Machine
This is my code for 5 switches (left to right).
int Switch1 = 22;
int Switch2 = 23;
int Switch3 = 24;
int Switch4 = 25;
int Switch5 = 26;
int LimitSwitch = 32;
int DcMotorOn = 8; //Motor On - Off
int DcMotorDir1 = 9; //Motor Direction (Dir1 LOW + Dir2 HIGH = Move Right)
int DcMotorDir2 = 10; //Motor Direction (Dir1 HIGH + Dir2 LOW = Move Left)
#define encoderI 2
#define encoderQ 3
int Distance = 0; //Distance on linear encoder
int ServoAngle = 170; //ServoAngle
int DistanceAcc; //Distance for acceleration / deceleration
int ValTwo = HIGH;
boolean LastSwitch1 = LOW;
boolean FakeSwitch = LOW;
#include <Servo.h>
Servo Servo1;
int pos = 0;
void setup() {
Serial.begin(9600);
Servo1.attach(11);
Servo1.write(180);
pinMode(encoderI, INPUT);
pinMode(encoderQ, INPUT); attachInterrupt(0, handleEncoder, CHANGE);
pinMode(LimitSwitch, INPUT);
pinMode(Switch1, INPUT);
pinMode(DcMotorOn, OUTPUT);
pinMode(DcMotorDir1, OUTPUT);
pinMode(DcMotorDir2, OUTPUT);
digitalWrite(DcMotorOn, HIGH);
analogWrite(DcMotorDir1, 160);
digitalWrite(DcMotorDir2, LOW);
}
void loop() {
if (digitalRead(LimitSwitch) == HIGH) { // Moves the Arm to the far rigth and sets the Distance to -150
digitalWrite(DcMotorDir1, LOW);
Distance = -150;
}
if (digitalRead(Switch1) == HIGH) { //Switch 1
FlipSwitch (2870);
}
if (digitalRead(Switch2) == HIGH) { //Switch 2
FlipSwitch (2570);
}
if (digitalRead(Switch3) == HIGH) { //Switch 3
FlipSwitch (2250);
}
if (digitalRead(Switch4) == HIGH) { //Switch 4
FlipSwitch (1900);
}
if (digitalRead(Switch5) == HIGH) { //Switch 5
FlipSwitch (1600);
}
Serial.println(Distance);
}
void FlipSwitch (int ValOne) { // ValOne = distance to switch
DistanceAcc = Distance;
if (DistanceAcc < ValOne-600 && ValTwo == HIGH) { //Step 1 Set ValOne to teh distance with deceleration of 600 units (2870-2270=600)
digitalWrite(DcMotorDir1, LOW);
analogWrite(DcMotorDir2, 255);
}
if (DistanceAcc > ValOne-600 && ValTwo == HIGH) { //Step 2 Start deceleration from ValOne - 600 to ValOne Units
digitalWrite(DcMotorDir1, LOW);
int DcMotorAcc = map(DistanceAcc, ValOne-600, ValOne, 255, 120);
analogWrite(DcMotorDir2, DcMotorAcc);
}
if (Distance > ValOne && ValTwo == HIGH) {
digitalWrite(DcMotorDir1, LOW); //Step 3 If > ValOne turn motor off
digitalWrite(DcMotorDir2, LOW);
Servo1.write(85);
delay(150); //Step 4 Move arm up to hit switch
Servo1.write(170); //Step 5 Switch hs been hit. Move arm back down
delay(130);
ValTwo = LOW;
digitalWrite(DcMotorDir1, HIGH);
digitalWrite(DcMotorDir2, LOW);
}
if (Distance < 1500 && ValTwo == LOW) {
//Step 8 Stop arm from moving right. (ready for step 1 to start over)
digitalWrite(DcMotorDir1, LOW);
digitalWrite(DcMotorDir2, LOW);
ValTwo = HIGH;
}
}
void handleEncoder() {
if(digitalRead(encoderI) == digitalRead(encoderQ)) //Reads the encoder = Distance
{ Distance ++;
}
else
{ Distance --;
}
}