he code is made in such a way, that when you press the button it will push a small amount of paste for a short time and then it will stop. But then, till you don’t release the button, the state won’t change, and in that way, we make sure it won’t pour more paste than the correct amount.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h> //download here: https://www.electronoobs.com/eng_arduino_Adafruit_GFX.php
#include <Adafruit_SSD1306.h> //downlaod here: https://www.electronoobs.com/eng_arduino_Adafruit_SSD1306.php
#define OLED_RESET 7
Adafruit_SSD1306 display(OLED_RESET);
//Inputs/Outputs
#define push_pin 2
#define pull_pin 3
#define mode_pin 4
#define rotate_CW 5
#define rotate_CCW 6
//Variables
int mode = 0;
bool push_pin_state = false;
bool pull_pin_state = false;
bool mode_pin_state = false;
unsigned long previous_time = 0;
unsigned long current_time = 0;
int refresh_rate = 300;
int rotate_time = 50;
int max_rotate_time = 600;
bool pushing = false;
unsigned long push_time = 0;
unsigned long mode_pin_pushed_time = 0;
void setup() {
pinMode(push_pin, INPUT_PULLUP);
pinMode(pull_pin, INPUT_PULLUP);
pinMode(mode_pin, INPUT_PULLUP);
pinMode(rotate_CW, OUTPUT);
pinMode(rotate_CCW, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32 or 64 from eBay)
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
previous_time = millis();
current_time = millis();
digitalWrite(rotate_CW, LOW);
digitalWrite(rotate_CCW, LOW);
}
void loop() {
current_time = millis();
if(current_time - previous_time > refresh_rate){
previous_time=current_time;
if(rotate_time < max_rotate_time+10){
display.clearDisplay();
display.setCursor(0,0);
display.print("Time ");
display.print(rotate_time);
display.println("ms");
display.display();
}
else if(rotate_time >= max_rotate_time+10){
display.clearDisplay();
display.setCursor(0,0);
display.println("Time ");
display.println("Continuous");
display.display();
}
}//end of > refresh rate
if(!digitalRead(mode_pin) && !mode_pin_state){
mode_pin_state = true;
rotate_time = rotate_time + 50;
mode_pin_pushed_time = millis();
delay(10);
if(rotate_time > max_rotate_time){
rotate_time = 50;
}
}
else if(!digitalRead(mode_pin) && mode_pin_state){
current_time = millis();
if(current_time - mode_pin_pushed_time > 1000){
rotate_time = 50;
}
}
else if(digitalRead(mode_pin) && mode_pin_state){
mode_pin_state = false;
delay(10);
}
if(!digitalRead(pull_pin) && !pull_pin_state){
pull_pin_state = true;
digitalWrite(rotate_CW, LOW);
digitalWrite(rotate_CCW, HIGH);
display.setCursor(0,19);
display.print("PULL");
display.display();
delay(20);
}
else if(digitalRead(pull_pin) && pull_pin_state){
pull_pin_state = false;
digitalWrite(rotate_CW, LOW);
digitalWrite(rotate_CCW, LOW);
delay(10);
}
if(!digitalRead(push_pin) && !push_pin_state){
push_pin_state = true;
pushing = true;
delay(10);
push_time = millis();;
}
else if(digitalRead(push_pin) && push_pin_state){
push_pin_state = false;
delay(10);
}
if(pushing){
if(rotate_time < max_rotate_time + 10){
digitalWrite(rotate_CW, HIGH);
display.setCursor(0,19);
display.print("PUSH");
display.display();
current_time = millis();
if(current_time - push_time > rotate_time){
digitalWrite(rotate_CW, LOW);
pushing = false;
}
}
else if(rotate_time >= max_rotate_time + 10){
digitalWrite(rotate_CW, HIGH);
if(digitalRead(push_pin)){
pushing = false;
digitalWrite(rotate_CW, LOW);
}
}
}
}//end of void loop