Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- //
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
- #define pot_in A3
- #define rocker 9
- #define PUL 7
- #define DIR 6
- #define ENA 5
- float minimum_speed_delay = 1000;
- float maximum_speed_delay = 1;
- float acceleration_delay = 20;
- int count = 0;
- float current_speed_delay = 10000;
- void setup() {
- lcd.begin(20, 4);
- lcd.setCursor(0, 0);
- lcd.print("Welcome to");
- lcd.setCursor(0, 1);
- lcd.print("Team Plastic");
- lcd.setCursor(0, 2);
- lcd.print("String Theory's");
- lcd.setCursor(0, 3);
- lcd.print("Plastic Extruder!");
- pinMode (PUL, OUTPUT);
- pinMode (DIR, OUTPUT);
- pinMode (ENA, OUTPUT);
- pinMode (pot_in, INPUT);
- pinMode (rocker, INPUT);
- delay(5000);
- }
- void loop() {
- if (digitalRead(rocker) == HIGH) {
- if (count == 0) { //Initial acceleration to minimum speed
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print("Motor is");
- lcd.setCursor(0, 2);
- lcd.print("accelerating");
- while (current_speed_delay > minimum_speed_delay) {
- digitalWrite(DIR, LOW);
- digitalWrite(ENA, HIGH);
- digitalWrite(PUL, HIGH);
- delayMicroseconds(current_speed_delay);
- digitalWrite(PUL, LOW);
- delayMicroseconds(current_speed_delay);
- current_speed_delay = current_speed_delay - acceleration_delay;
- count = count + 1;
- }
- }
- else if (count > 0) { //Motor speed = multiple of potiometer reading
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Motor is operating");
- lcd.setCursor(0, 2);
- lcd.print("at");
- lcd.setCursor(8, 2);
- lcd.print("RPMs");
- while (digitalRead(rocker) == HIGH) {
- digitalWrite(DIR, LOW);
- digitalWrite(ENA, HIGH);
- digitalWrite(PUL, HIGH);
- delayMicroseconds(current_speed_delay);
- digitalWrite(PUL, LOW);
- delayMicroseconds(current_speed_delay);
- int potent = analogRead(pot_in);
- current_speed_delay = constrain(potent, maximum_speed_delay, minimum_speed_delay);
- count = count + 1;
- lcd.setCursor(3, 2);
- lcd.print(int(current_speed_delay));
- }
- }
- }
- else if (digitalRead(rocker) == LOW) { //Turning motor off
- if (count > 0) { //Deceleration to 0
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print("Motor is");
- lcd.setCursor(0, 2);
- lcd.print("decelerating");
- while (current_speed_delay < 100) {
- digitalWrite(DIR, LOW);
- digitalWrite(ENA, HIGH);
- digitalWrite(PUL, HIGH);
- delayMicroseconds(current_speed_delay);
- digitalWrite(PUL, LOW);
- delayMicroseconds(current_speed_delay);
- current_speed_delay = current_speed_delay + acceleration_delay;
- count = count + 1;
- }
- count = 0;
- }
- else if (count == 0) { //Motor speed = 0
- lcd.clear();
- lcd.setCursor(3, 1);
- lcd.print("Motor is off");
- while (digitalRead(rocker) == LOW) {
- delay(10);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement