본문 바로가기

Project/KUSMO

[KUSMO] 8/24 Arduino 수위센서 피에조 추가, 폐기부 제어 코드

728x90
  • 기존의 Arduino + 수위 센서 + LED 로 구성한 배액량 모니터링 구조에 피에조 부저 추가
// Nonconnection_Water_Level_LED_Buzzor

#define senpin 2
#define led 7
#define buzzor 3

bool fill = false;              // Flag that Indicates Tank is full

void setup()
{     
  pinMode(led,OUTPUT);
  pinMode(buzzor, OUTPUT);
}
 
void loop () 
{
  fill = digitalRead(senpin);   // Read Sensor`s Raw Data
  
  if(fill){                     // Tank if full
    digitalWrite(led, HIGH);    // Turn on LED
    digitalWrite(buzzor, HIGH); // Turn on Buzzor
    
  }
  else{                         // Tank is not full
    digitalWrite(led, LOW);     // Turn off LED
    digitalWrite(buzzor, LOW);  // Turn off Buzzor
  }
}
  • 동작 코드

 

 

 

 

  • Arduino + CNC Shield + A4988 + L298N을 통한 Y, Z축 Actuator 동시 수동 제어
// Connection_Control

#include <Stepper.h>        // Library to use Stepper Motor

#define EN 8
const int stepPin = 2;      // X.STEP Motor's direction control. Motor moves one step by pulse sent to this pin
const int dirPin = 5;       // X.DIR Motor's Rotation Direction Control
const int degree_per_revolition = 200;

// Y Direction Actuator Connection
const int enA = 52;         // Motor's PWM Input Pin. Set the motor's speed by this pin's value
const int in1 = 50;         // Set Motor's Direction
const int in2 = 48;         // Set Motor's Direction
bool Z_Running = false;     // Variable to track if the motor is currently running

unsigned long currentMicros = 0;
unsigned long previousMicros = 0;
unsigned long periodMicros = 0;
int pwmFrequency = 100;      // PWM frequency in Hz                 
int pwmDutyCycle = 255;      // PWM duty cycle (0-255) for speed control

const int sw = 22;
char sw_state = HIGH;

void setup() {
  Serial.begin(9600);
  // Set all pins to Output related to Z Actuator
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(EN, OUTPUT);
  pinMode(sw, INPUT);
  digitalWrite(EN, LOW);

  // Set all pins to Output related to Y Actuator
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enA, OUTPUT);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

void loop() {
  if (Serial.available()) {         // Set to read Keyboard Input
    char input = Serial.read();
    Serial.print("Input : ");
    Serial.println(input);

    /* Z Actuator Control */
    if (input == 'q') {
      digitalWrite(dirPin, HIGH);   // Set Z Actuator to move Upward
      Z_Running = true;             // Start Z Actuator
    }
    else if (input == 'e') {
      digitalWrite(dirPin, LOW);    // Set Z Actuator to move Downward
      Z_Running = true;             // Start Z Actuator
    }
    else if (input == 'w') {
      Z_Running = false;            // Stop Z Actuator
    }

    /* Y Actuator Control */
    else if (input == 'a') {        // Set Y Actuator to move Forward
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
    }
    else if (input == 'd') {        // Set Y Actuator to move Backward
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
    }
    else if (input == 's') {        // Stop Y Actuator
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
    }

    /* Stop all the Actuators */
    else if (input == 'x') {
      Z_Running = false;
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
    }
  }

  sw_state = digitalRead(sw);       // Read the push of Switch
  if(sw_state == LOW){              // Indicates that Z axis actuator reached to top
    Serial.println("Z direction Acutator reached to top point");
    Z_Running = false;              // Stop Z axis actuator
    }

  /* Set Z Actuator */
  if (Z_Running) {
    // 1.8 degrees per 1 revolution
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(800);         // Set Z Actuator's speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(800);
  }

 /* Set Y Actuator speed using software PWM */
  currentMicros = micros();
  periodMicros = 1000000 / pwmFrequency;

  if (currentMicros - previousMicros >= periodMicros) {
    previousMicros = currentMicros;
    analogWrite(enA, pwmDutyCycle);
  }
}
  • 동작 코드