Close

Week 9 +2/3 Days

A project log for Microplate Reader

A High School project

wwangwwang 12/27/2020 at 09:180 Comments

On Day 2 of Week 9, we suffered great loss, our belt receiving gear left us forever, so we decided to remake it with a higher infill rate. And today we finally assembled the X-axis and Y-axis components of the motor, with the belt included and gears needed to receive the belt! Actually a few days ago we already had 3D printed most of the components, however, many of the first draft components broke due to insufficient infill and  not careful handling. Therefore, we had to revise the models and reassemble them onto the assembled aluminium beams. 

After we finished assembling these components, we then worked on the code to control both motors' movements and directions. Although it took some time, we were finally able to figure out the code suited: 

// System Inialization Setup
String protocolString = "";
boolean protocolWait = false;
String parsedString = "";
int startIndex = 0;
String totalString = "";
String ledString = "";
unsigned long Timecount1;
unsigned long Durationcount1;
unsigned long Time1;
unsigned long Duration1;
unsigned long Timecount2;
unsigned long Durationcount2;
unsigned long Time2;
unsigned long Duration2;
unsigned Direction1;
unsigned Direction2;
int DIRCount1 = 0;
int DIRCount2 = 1;
const int DIRPin1 = 2;
const int STEPPin1 = 3;
const int ENNPin1 = 7;
const int DIRPin2 = 6;
const int STEPPin2 = 5; 
int STEPState1 = LOW;
int DIRState1 = HIGH;
int STEPState2 = LOW;
int DIRState2 = HIGH;

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 = 250;   // 25Khz
  TCNT1 = 0;

  TIMSK1 |= (1<<OCIE1A);  // Enable Timer1 interrupt
  //digitalWrite(ENNpin, LOW);   // Enable stepper drivers
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  pinMode(DIRPin1, OUTPUT);
  pinMode(STEPPin1, OUTPUT);
  pinMode(DIRPin2, OUTPUT);
  pinMode(STEPPin2, OUTPUT);
  pinMode(ENNPin1, OUTPUT);
  digitalWrite(DIRPin1, HIGH);
  digitalWrite(STEPPin1, LOW);
  digitalWrite(DIRPin2, LOW);
  digitalWrite(STEPPin2, HIGH);
  digitalWrite(ENNPin1, LOW);
  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+3) == "LED"){
          totalString = protocolString.substring(startIndex,i);
          Serial.println(totalString.length());
          if (totalString.length() <= 4){
            ledString = protocolString.substring(startIndex+3,i);
            if (ledString == "1"){
              digitalWrite(2, !digitalRead(2));
              }
            else if (ledString == "2"){
              digitalWrite(3, !digitalRead(3));
              }
          }
        }
        else if(protocolString.substring(startIndex, startIndex+1) == "M"){
          Timecount1 =0;
          Time1 = protocolString.substring(startIndex+1,i-9).toInt();
          Direction1 = protocolString.substring(startIndex+6,i-8).toInt();
          Durationcount1 =0;
          Duration1 = protocolString.substring(startIndex+7,i-7).toInt();
          Timecount2 =0;
          Time2 = protocolString.substring(startIndex+8,i-2).toInt();
          Direction2 = protocolString.substring(startIndex+13,i-1).toInt();
          Durationcount2 =0;
          Duration2 = protocolString.substring(startIndex+14,i).toInt();
          Serial.println("done");
          Serial.print(Time1);
          Serial.println("");
          Serial.print(Direction1);
          Serial.println("");
          Serial.print(Duration1);
          Serial.println("");
          Serial.print(Time2);
          Serial.println("");
          Serial.print(Direction2);
          Serial.println("");
          Serial.print(Duration2);
        }
        else if (protocolString.substring(startIndex, startIndex+1) == "T"){
          totalString = protocolString.substring(startIndex,i);
          Serial.println(totalString.length());
          if (totalString.length() <= 4){
            ledString = protocolString.substring(startIndex+3,i);
            if (ledString == "1"){
              digitalWrite(2, !digitalRead(2));
              }
            else if (ledString == "2"){
              digitalWrite(3, !digitalRead(3));
              }
          }
        }
      }
  }
}
}

  ISR(TIMER1_COMPA_vect){
  
//    digitalWrite(STEPPin1, 1);
//    delayMicroseconds(1);
//    digitalWrite(STEPPin1, 0);
  if (Direction1 <= DIRCount1){
    digitalWrite(DIRPin1, LOW);
  }
  if (Direction1 >= DIRCount2){
    digitalWrite(DIRPin1,HIGH);
  }
  if (Direction2 <= DIRCount1){
    digitalWrite(DIRPin2, LOW);
  }
  if (Direction2 >= DIRCount2){
    digitalWrite(DIRPin2,HIGH);
  }

  if (Timecount1 < Time1) {
    if (Durationcount1 < Duration1) {
  if (STEPState1 == LOW) {
      STEPState1 = HIGH;
    } else {
      STEPState1 = LOW;
    }
    digitalWrite(STEPPin1, STEPState1);
    Durationcount1++;
     }
      if (Durationcount1 >= Duration1) {
          Timecount1++;
          Durationcount1 =0;
        }
     }
  
  else if(Timecount1 >= Time1)  {
      digitalWrite(STEPPin1, 0);
    }
      
  if (Timecount2 < Time2) {
    if (Durationcount2 < Duration2) {
  if (STEPState2 == LOW) {
      STEPState2 = HIGH;
    } else {
      STEPState2 = LOW;
    }
    digitalWrite(STEPPin2, STEPState2);
    Durationcount2++;
     }
      if (Durationcount2 >= Duration2) {
          Timecount2++;
          Durationcount2 =0;
        }
     }
  
  else if(Timecount2 >= Time2)  {
      digitalWrite(STEPPin2, 0);
    }
}

Discussions