Close

Week12 Day 3

A project log for Microplate Reader

A High School project

wwangwwang 01/23/2021 at 08:450 Comments

Today we mainly worked on fitting all the final versions of our models that we created, for instance, tightening our belts that control our X and Y axis again. Adding on, we continued working on our code that control the X and Y axis movements of the plate holder. For example, stopping when it hits the switch on the end of the track, and moving to the (0,0) position when activated. Along the way, we met quite a few difficulties such as short-circuiting, connecting the wrong wires with the Arduino board, and not finding the bugs in our codes. However, we were able to solve most of our issues in our code and looking forward to coding the LED lights next week!

Our Code:

// 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 = 8;
const int DIRPin2 = 4;
const int STEPPin2 = 7; 
const int ES1 = 12;
const int ES2 = 13;
int STEPState1 = LOW;
int DIRState1 = HIGH;
int STEPState2 = LOW;
int DIRState2 = HIGH;
unsigned int XPosition;
unsigned int YPosition;

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;   // 8Khz
  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);
  pinMode(ES1, INPUT);
  pinMode(ES2, INPUT);
  digitalWrite(DIRPin1, HIGH);
  digitalWrite(STEPPin1, LOW);
  digitalWrite(DIRPin2, LOW);
  digitalWrite(STEPPin2, HIGH);
  digitalWrite(ENNPin1, LOW);
  XPosition = 1;
  YPosition = 1;
  initOutput();
    while (HIGH != digitalRead(ES1) && HIGH != digitalRead(ES2)) {
    digitalWrite (DIRPin1, LOW);
    if (STEPState1 == LOW) {
      STEPState1 = HIGH;
      delayMicroseconds(100);
    } else {
      STEPState1 = LOW;
      delayMicroseconds(100);
    }
    digitalWrite (DIRPin2, LOW);
    if (STEPState1 == LOW) {
      STEPState2 = HIGH;
      delayMicroseconds(100);
    } else {
      STEPState2 = LOW;
      delayMicroseconds(100);
    }
    XPosition = 0;
    YPosition = 0;
  }
}

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){
  if (HIGH == digitalRead(ES1)) {
    Timecount1 = Time1;
    Timecount1 >= Time1;
  }
  if (HIGH == digitalRead(ES2)) {
    Timecount2 = Time2;
    Timecount2 >= Time2;
  }
//    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