// defines pins
#define STEP 2
#define DIR 3
#define SLEEP 8
#define RESET 9
#define poti_v A0
#define poti_fw A1
#define poti_re A2
#define poti_pause A3
#define MS1 4
#define MS2 5
#define MS3 6
#define button_01 7
#define MOTOR_STEPS 400
#define MOTOR_ACCEL 2000
#define MOTOR_DECEL 1000
int MICROSTEPS = 16;
int RPM = 200;
//int distance_s=108;
//int distance_r=54;
int speed, distance, retract, pause;
int analog_speed, analog_distance, analog_retract, analog_pause;
int old_analog_speed, old_analog_distance, old_analog_retract, old_analog_pause;
#include "A4988.h"
#include <Arduino.h>
#include <U8g2lib.h>
#include <YetAnotherPcInt.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
A4988 stepper(MOTOR_STEPS, DIR, STEP,SLEEP, MS1, MS2, MS3);
int button_status = 1;
int pinValue;
int cycle = 5;
int pass = 1;
static int step = 0;
int pausenzeit;
const uint8_t tile_area_x_pos = 7; // Update area left position (in tiles)
const uint8_t tile_area_y_pos = 0; // Update area upper position (distance from top in tiles)
const uint8_t tile_area_width = 6;
const uint8_t tile_area_height = 8; // this will allow cour18 chars to fit into the area
const u8g2_uint_t pixel_area_x_pos = tile_area_x_pos*8;
const u8g2_uint_t pixel_area_y_pos = tile_area_y_pos*8;
const u8g2_uint_t pixel_area_width = tile_area_width*8;
const u8g2_uint_t pixel_area_height = tile_area_height*8;
void playAnimation(int fDelay) {
u8g2.sendBuffer();
u8g2.clearBuffer();
delay(1000);
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setFont(u8g2_font_t0_13_tr);
u8g2.drawStr(0, 16, "Welcome");
u8g2.updateDisplayArea(0,0,8,2);
u8g2.drawStr(17, 29, "to");
u8g2.drawStr(4, 43, "Weldo");
u8g2.updateDisplayArea(0,2,5,6);
delay(1000);
u8g2.setFont(u8g2_font_t0_13b_tr);
u8g2.drawStr(8, 57, "Pro");
u8g2.updateDisplayArea(0,6,4,2);
}
void check_button() {
if (digitalRead((button_01)) == HIGH){
cycle = 0;
//Serial.println("knopf");
}
else if(digitalRead((button_01)) == LOW){
cycle = 4;
}
}
void vorschub_speed() {
analog_speed = analogRead(poti_v);
if (analog_speed <= old_analog_speed+10 && analog_speed >= old_analog_speed-10 ){
}
else {
old_analog_speed = analog_speed;// Read the potentiometer value
speed = map(analog_speed, 0, 1023, 10, RPM) ;
stepper.setRPM(speed);
u8g2.updateDisplayArea(tile_area_x_pos, tile_area_y_pos, tile_area_width, tile_area_height);
//stepper.begin(speed, MOTOR_STEPS); // Convert the analog input from 0 to 1024, to 0 to 100
//Serial.println(speed);
}
}
void vorschub_distance() {
analog_distance = analogRead(poti_fw); // Read the potentiometer value
distance = map(analog_distance, 0, 1023, 0, 1000) * 720/1000; // Convert the analog input from 0 to 1024, to 0 to 100
}
void retract_distance() {
analog_retract = analogRead(poti_re); // Read the potentiometer value
retract = map(analog_retract, 0, 1023, 0, 1000) * 180/1000; // Convert the analog input from 0 to 1024, to 0 to 100
}
void pause_time() {
analog_pause = analogRead(poti_pause); // Read the potentiometer value
pause = map(analog_pause, 0, 1023, 0, 1000) ; // Convert the analog input from 0 to 1024, to 0 to 100
Serial.print(pause);
}
void display_refresh(){
u8g2.clearBuffer();
u8g2.setCursor(60, 32);
u8g2.print(distance);
u8g2.setCursor(60, 64);
u8g2.print(pause);
u8g2.setCursor(60, 48);
u8g2.print( retract);
u8g2.setCursor(60, 16);
u8g2.print(speed);
u8g2.updateDisplayArea(tile_area_x_pos, tile_area_y_pos, tile_area_width, tile_area_height);
}
void setup() {
// Sets the two pins as Outputs
pinMode(button_01, INPUT_PULLUP);
pinMode(RESET, OUTPUT);
digitalWrite(RESET,LOW);
//Serial.begin(9600);
// Serial.println("start");
stepper.setMicrostep(MICROSTEPS);
//stepper.setSpeedProfile(stepper.LINEAR_SPEED, MOTOR_ACCEL, MOTOR_DECEL);
//stepper.stop();
PcInt::attachInterrupt(button_01, check_button,CHANGE);
old_analog_speed = 2000;
//stepper.disable();
u8g2.begin();
playAnimation(350);
delay(500);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_helvR10_tr); // choose a suitable font
u8g2.drawStr(0,16,"Speed:"); // write something to the internal memory
u8g2.drawStr(0,32,"Feed:");
u8g2.drawStr(0,48,"Retracte:");
u8g2.drawStr(0,64,"Pause:");
u8g2.drawStr(105,16,"m/s"); // write something to the internal memory
u8g2.drawStr(105,32,"mm");
u8g2.drawStr(105,48,"mm");
u8g2.drawStr(105,64,"ms");
u8g2.sendBuffer();
pause_time();
retract_distance();
vorschub_distance();
vorschub_speed();
}
void loop() {
pause_time();
retract_distance();
vorschub_distance();
vorschub_speed();
display_refresh();
delay(20);
switch(cycle) {
//forward
case 0: {
digitalWrite(RESET,HIGH);
stepper.begin(speed, MOTOR_STEPS);
//delay(2);
vorschub_distance();
vorschub_speed();
stepper.rotate(distance);
// unsigned wait_time = stepper.nextAction();
//stepper.startMove(distance*MOTOR_STEPS*MICROSTEPS);
//int steps = stepper.getStepsRemaining();
//while( steps > 0){
//stepper.nextAction();
//steps = stepper.getStepsRemaining();
//vorschub_speed();
//}
//Serial.print("Vorschub:");
//Serial.println(distance);
}
case 1 : {
//Serial.println("sequenz");
stepper.begin(120, MOTOR_STEPS);
pause_time();
retract_distance();
//Serial.print("retract:");
//Serial.println(-retract);
stepper.rotate(-retract);
digitalWrite(RESET,LOW);
}
case 2: {
//Serial.print("Pause:");
//Serial.println(pause);
delay(pause);
}
break;
case 3:{
// Serial.println("ENDE");
digitalWrite(RESET,LOW);
cycle = 5;
}
break;
case 4: {
digitalWrite(RESET,HIGH);
// Serial.println("rueck");
stepper.begin(120, MOTOR_STEPS);
pause_time();
retract_distance();
vorschub_distance();
vorschub_speed();
//Serial.print("retract:");
// Serial.println(-retract);
stepper.rotate(-retract);
digitalWrite(RESET,LOW);
cycle= 5;
}
break;
}
}