2019.03.07(pm): Water Pump Test with RAMPS 1.4

Calibration

// iter # : move dist (mm)
//  x & y direction
//  1800 = 180 mm
//  1 count = 0.1 mm
 
//  z direction
//  15000 = 150 mm
//  1 count = 0.01 mm

void  moveXYZToOrigin(){
    touchXsensor(); // in Calibration.INO 
    touchYsensor();
    touchZsensor();
  
}
void touchXsensor(){
  int count = 0;
  while (!digitalRead(X_MIN_PIN))
  {
    Serial.println(digitalRead(X_MIN_PIN));
    digitalWrite(X_STEP_PIN, HIGH); 
    delayMicroseconds(motorSpeed);
    digitalWrite(X_STEP_PIN, LOW); 
    delayMicroseconds(motorSpeed);
    count++;
  }
  Serial.print("X home: ");
  Serial.println(count);
}

void touchYsensor(){
  
  int count = 0;
  while (!digitalRead(Y_MIN_PIN))
  {
    //Serial.println(digitalRead(X_MIN_PIN));
    digitalWrite(Y_STEP_PIN, HIGH);
    delayMicroseconds(motorSpeed);
    digitalWrite(Y_STEP_PIN, LOW);
    delayMicroseconds(motorSpeed);
    count++;
  }
  Serial.print("Y home: ");
  Serial.println(count);
}

void touchZsensor(){
  
  int count = 0;
  while (!digitalRead(Z_MIN_PIN))
  {
    digitalWrite(Z_STEP_PIN, HIGH);
    delayMicroseconds(motorSpeed);
    digitalWrite(Z_STEP_PIN, LOW);
    delayMicroseconds(motorSpeed);
    count++;
  }
  Serial.print("Z home: ");
  Serial.println(count);
}



void cal_X(){
 for (int i = 0; i < 100; i++) {
    digitalWrite(X_STEP_PIN, HIGH);
    delay(5);
    digitalWrite(X_STEP_PIN, LOW);
    delay(5);
  }
  delay(100);
}

void cal_Y(){
  for (int i = 0; i < 100; i++) {
    digitalWrite(Y_STEP_PIN, HIGH);
    delay(5);
    digitalWrite(Y_STEP_PIN, LOW);
    delay(5);
  }
  delay(100);

  
}

void cal_Z_old(){
  for (int i = 0; i < 100; i++) {
      digitalWrite(Z_STEP_PIN, HIGH);
      delay(5);
      digitalWrite(Z_STEP_PIN, LOW);
      delay(5);
  }
  delay(100);
}

void cal_Z(){
  int count = 0;
  while(count <= 20000) {
    digitalWrite(Z_STEP_PIN, HIGH);
    delay(1);
    digitalWrite(Z_STEP_PIN, LOW);
    delay(1);
    count++;
    Serial.println(count);
  }
  delay(100000);
}

Initialization

void ini_setting(){
  
    // pinmode setting
    pinMode(X_STEP_PIN, OUTPUT); 
    pinMode(X_DIR_PIN, OUTPUT); 
    pinMode(X_ENABLE_PIN, OUTPUT);
    
    pinMode(Y_STEP_PIN, OUTPUT); 
    pinMode(Y_DIR_PIN, OUTPUT); 
    pinMode(Y_ENABLE_PIN, OUTPUT);
    
    pinMode(Z_STEP_PIN, OUTPUT); 
    pinMode(Z_DIR_PIN, OUTPUT); 
    pinMode(Z_ENABLE_PIN, OUTPUT);

    pinMode(WATER_NOZZLE_PIN, OUTPUT);
    pinMode(WATER_PUMP_PIN, OUTPUT);

  
    // direction setting
    digitalWrite(X_ENABLE_PIN, LOW); 
    digitalWrite(X_DIR_PIN, LEFT);
    
    digitalWrite(Y_ENABLE_PIN, LOW); 
    digitalWrite(Y_DIR_PIN, FRONT);
    
    digitalWrite(Z_ENABLE_PIN, LOW); 
    digitalWrite(Z_DIR_PIN, UP);

    // water nozzle
    digitalWrite(WATER_NOZZLE_PIN, HIGH); // Turn off
}

void resetDirection(){
    digitalWrite(X_DIR_PIN, LEFT);
    digitalWrite(Y_DIR_PIN, FRONT);
    digitalWrite(Z_DIR_PIN, UP);
}

Move

void moveX(int dir, int dist){
    digitalWrite(X_DIR_PIN, dir);
    for (int i = 0; i < dist * XY_1cm; i++)  
    {
        digitalWrite(X_STEP_PIN, HIGH); 
        delayMicroseconds(motorSpeed);
        digitalWrite(X_STEP_PIN, LOW); 
        delayMicroseconds(motorSpeed);
    }
}

void moveY(int dir, int dist){
    digitalWrite(Y_DIR_PIN, dir);
    for (int i = 0; i < dist * XY_1cm; i++)  
    {
        digitalWrite(Y_STEP_PIN, HIGH); 
        delayMicroseconds(motorSpeed);
        digitalWrite(Y_STEP_PIN, LOW); 
        delayMicroseconds(motorSpeed);
    }
}

void moveXY(int x_dir, int y_dir, int dist){
    digitalWrite(X_DIR_PIN, x_dir);
    digitalWrite(Y_DIR_PIN, y_dir);
    for (int i = 0; i < dist * XY_1cm; i++)  
    {
        digitalWrite(X_STEP_PIN, HIGH); 
        digitalWrite(Y_STEP_PIN, HIGH); 
        delayMicroseconds(motorSpeed);
        digitalWrite(X_STEP_PIN, LOW); 
        digitalWrite(Y_STEP_PIN, LOW); 
        delayMicroseconds(motorSpeed);
    }
}

void moveZ(int dir, int dist){
    digitalWrite(Z_DIR_PIN, dir);
    for (int i = 0; i < dist * Z_1cm; i++)
    {
        digitalWrite(Z_STEP_PIN, HIGH); 
        delayMicroseconds(motorSpeed);
        digitalWrite(Z_STEP_PIN, LOW); 
        delayMicroseconds(motorSpeed);
    }  
}

Sensors

int Humidity = 0;

void getHumidity(){
    moveZ(DOWN, 2);
    delay(3000);
    int readValue = analogRead(HUMIDITY_SENSOR_PIN);
    Humidity = map(readValue, 1023, 0, 0, 100);
    Serial.print("Humidity Percentage: ");
    Serial.println(Humidity);
    moveZ(UP, 2);
}

Water

void giveWater(){
    //int waterTime = map(Humidity, 0, 100, 2000, 0); // need to test
    //Serial.println(waterTime);
    digitalWrite(WATER_NOZZLE_PIN, LOW);   //d12 = 25 in arduino mega
    //delay(waterTime);
    delay(1000);
    digitalWrite(WATER_NOZZLE_PIN, HIGH);
}

2019 03 06 Water Pump Test