This Sunday, our team worked mainly on changing the details and improving the PCB board that we have drawn. We have added LEDs and motor to the PCB so that we would be able to control both the LED and the motor in one single program. This is our schematic diagram and PCB board that we have drawn.
We have almost finished with the motor control programming that we have found a few mistakes such as the conditions we gave to the motor need to be more complicated although the calculation would not bring the plateholder to the right spot.
This is our code that still needs improvement:
String protocolString = "";
boolean protocolWait = false;
String parsedString = "";
int startIndex = 0;
String totalString = "";
String ledString = "";
unsigned long Xstepcount = 0;
unsigned long Ystepcount = 0;
unsigned long Xstep = 0;
unsigned long Ystep = 0;
unsigned long Xmili = 0;
unsigned long Ymili = 0;
unsigned long Xstep1 = 0;
unsigned long Ystep1 = 0;
unsigned long Xstep_prev = 0;
unsigned long Ystep_prev = 0;
int period = 0;
void initOutput() {
//digitalWrite(ENNpin,HIGH); // Disable motors
//We are going to overwrite the Timer1 to use the stepper motors
// STEPPER MOTORS INITIALIZATION
// TIMER1 CTC MODE
TCCR1B &= ~(1<<WGM13);
TCCR1B |= (1<<WGM12);
TCCR1A &= ~(1<<WGM11);
TCCR1A &= ~(1<<WGM10);
// output mode = 00 (disconnected)
TCCR1A &= ~(3<<COM1A0);
TCCR1A &= ~(3<<COM1B0);
// Set the timer pre-scaler
// Generally we use a divider of 8, resulting in a 2MHz timer on 16MHz CPU
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
//OCR1A = 125; // 16Khz
//OCR1A = 100; // 20Khz
OCR1A = 80; // 25Khz
TCNT1 = 0;
TIMSK1 |= (1<<OCIE1A); // Enable Timer1 interrupt
//digitalWrite(ENNpin, LOW); // Enable stepper drivers
}// System Inialization Setup
const int dirpin = 5;
const int steppin = 4;
const int enpin = 7;
const int dirpin2 = 2;
const int steppin2 = 3;
int stepstate = LOW;
unsigned long pM = 0;
unsigned long pM2 = 0;
int vt = 1000;
void setup() {
pinMode(dirpin, OUTPUT);
pinMode(dirpin2, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(steppin2, OUTPUT);
pinMode(enpin, OUTPUT);
digitalWrite(dirpin, HIGH);
digitalWrite(dirpin2, LOW);
digitalWrite(steppin, LOW);
digitalWrite(steppin2, LOW);
digitalWrite(enpin, LOW);
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
initOutput();
}
void loop() {
// Serial.println("pass");
if (Serial.available() > 0) {
protocolString = Serial.readString();
// Serial.println(protocolString);
for (int i = 0; i <= protocolString.length(); i++){ // Analyze command string one character at a time
parsedString = protocolString.substring(i,i+1);
if (parsedString == "@"){ // '@' Symbolizes the end of a command
if (protocolString.substring(startIndex, startIndex+1) == "L"){
totalString = protocolString.substring(startIndex,i);
// Serial.println(totalString.length());
if (totalString.length() <= 2){
ledString = protocolString.substring(startIndex+1,i);
if (ledString == "1"){
digitalWrite(2, !digitalRead(2));
}
else if (ledString == "2"){
digitalWrite(3, !digitalRead(3));
}
}
}
else if(protocolString.substring(startIndex, startIndex+1) == "M"){
Xstep_prev = Xstep;
Ystep_prev = Ystep;
Xstepcount = 0;
Xmili = protocolString.substring(startIndex+1,i-7).toInt();
Xstep = Xmili*40;
Xstep1 = Xstep;
Ystepcount = 0;
Ymili = protocolString.substring(startIndex+8,i).toInt();
Ystep = Ymili*40;
Ystep1 = Ystep;
// Serial.println("done");
// Serial.print(Xstep);
// Serial.println("");
// Serial.print(Ystep);
if (Xstep < Xstep_prev) {
if (digitalRead(dirpin) == HIGH) {
digitalWrite(dirpin, LOW);
}
else{
digitalWrite(dirpin, HIGH);
}
Xstep1 = Xstep_prev - Xstep;
}
else{
Xstep1 = Xstep - Xstep_prev;
}
if (Ystep < Ystep_prev) {
if (digitalRead(dirpin2) == HIGH) {
digitalWrite(dirpin2, LOW);
}
else{
digitalWrite(dirpin2, HIGH);
}
Ystep1 = Ystep_prev - Ystep;
}
else{
Ystep1 = Ystep - Ystep_prev;
}
}
startIndex = i+1;
}
}
}
startIndex = 0;
}
ISR(TIMER1_COMPA_vect) // DRIVER_PIN[5] = {5,6,7,8,4}; //STEP1 (PORTD 5), STEP2 (PORTD 6), DIR1 (PORTD 7), DIR2 (PORTB 0), ENABLE
{
period++;
if (period > 20) {
period = 0;
if (Xstepcount < Xstep1)
{
digitalWrite(steppin, 1);
delayMicroseconds(10);
digitalWrite(steppin, 0);
Xstepcount++;
}
else
{
Xstep1 = 0;
}
if (Ystepcount < Ystep1) {
digitalWrite(steppin2, 1);
delayMicroseconds(10);
digitalWrite(steppin2, 0);
Ystepcount++;
}
else
{
Ystep1 = 0;
}
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.