Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //librerie
- #include <Arduino.h>
- #include "A4988.h"
- #include <LiquidCrystal.h>
- //definizione pin
- int DIR = 2;
- #define STEP 3
- #define ENABLE 13
- #define MS1 A5
- #define MS2 A4
- #define MS3 A3
- int button = A0;
- //definizione variabili
- #define MOTOR_STEPS 200 //step motore
- int MICROSTEPS = 1; //velocità
- int giri = 1; //giri*minuto
- int vel = 1;
- int value = 0;
- //definizione array
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- A4988 stepper(MOTOR_STEPS, DIR, STEP, ENABLE, MS1, MS2, MS3);
- void setup() {
- //impostazioni pinMode
- pinMode(STEP, OUTPUT);
- pinMode(DIR, OUTPUT);
- //output LCD
- lcd.begin(16, 2);
- lcd.print("giri/min:");
- lcd.setCursor(0, 1);
- lcd.print("velocita:");
- Serial.begin(9600);
- }
- void loop() {
- //lettura pulsanti
- int sensorValue = analogRead(button);
- //incremento/decremento velocità
- if (sensorValue == 0) {
- vel = vel + 1;
- delay (500);
- }
- if (sensorValue == 504) {
- vel = vel - 1;
- delay (500);
- }
- if (vel == 1) {
- MICROSTEPS = 1;
- }
- if (vel == 2) {
- MICROSTEPS = 2;
- }
- if (vel == 3) {
- MICROSTEPS = 3;
- }
- if (vel == 4) {
- MICROSTEPS = 4;
- }
- if (vel >= 5) {
- vel = 1;
- }
- if (vel <= 0) {
- vel = 1;
- }
- //incremento/decremento giri*min
- if (sensorValue == 143) {
- giri = giri + 1;
- delay (500);
- }
- if (sensorValue == 331) {
- giri = giri - 1;
- delay (500);
- }
- //movimento motore
- //direzione antioraria con giri negativi
- if (giri < 0) {
- stepper.rotate(-360);
- }
- //direzione oraria con giri positivi
- else {
- stepper.rotate(360);
- }
- //stampa impostazioni a LCD
- lcd.setCursor(9, 0);
- lcd.print(giri);
- lcd.setCursor(9, 1);
- lcd.print(MICROSTEPS);
- //stop con giri = 0
- if (giri == 0) {
- stepper.disable();
- }
- //stop con pulsante
- else if (value == 1) {
- stepper.disable();
- lcd.setCursor(12, 0);
- lcd.print("STOP");
- }
- //moviemnto motore
- else {
- stepper.begin(giri, MICROSTEPS);
- stepper.enable();
- }
- //STOP con pulsante select
- if (sensorValue == 740) {
- value = value + 1;
- delay (500);
- }
- if (value == 2) {
- value = 0;
- lcd.setCursor(12, 0);
- lcd.print(" ");
- }
- Serial.println(sensorValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement