My first code, but I can't figure it out

33 views
Skip to first unread message

Aldrik Koster

unread,
Sep 14, 2020, 4:13:19 PM9/14/20
to Ardublock
ardublock.pngI created my first code using ArduBlock.
Almost everything works, only the part of the endstop does not work.

It is a vacuum meter that you can set, but if the "end stop" is triggered, the vaccum pump must stop and the servo must go up.

The setting of the vacuum works and the reading also works, only when the "end stop" is triggered, what I want does not happen.

What I don't see well.  


#include <HX711.h>
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <U8g2_for_Adafruit_GFX.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

#define OLED_RESET     -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;

HX711 scale;
int vacuum = 0 ;
int eindstop = 0 ;
boolean DigitalRead(int pinNumber)
{
  pinMode(pinNumber, INPUT);
  return digitalRead(pinNumber);
}

Servo servo_pin_11;

void setup()
{
  display.begin(SSD1306_SWITCHCAPVCC);
  u8g2_for_adafruit_gfx.begin(display);
  Serial.begin(9600);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(; ;
  }

  display.clearDisplay();
  u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
  u8g2_for_adafruit_gfx.setFontMode(2);
  u8g2_for_adafruit_gfx.setFontDirection(0);
  u8g2_for_adafruit_gfx.setForegroundColor(WHITE);
  u8g2_for_adafruit_gfx.setCursor(0,25);
  u8g2_for_adafruit_gfx.print("VACUUM");
  display.display();
  delay(2000);
 
  scale.begin(2,3);//DOUT, CLK
  scale.set_scale(77550);
  scale.tare();
  pinMode( 9 , OUTPUT);
  servo_pin_11.attach(11);
  Serial.begin(9600);

  vacuum = 1 ;

  eindstop = 0 ;

}

void loop()
{
  if (( ( vacuum ) > ( scale.get_units() ) ))
  {
    if (( DigitalRead(7) && ( ( eindstop ) == ( 1 ) ) )) //??
    {
      digitalWrite(9 , LOW);         //??
      servo_pin_11.write( 60 );    //??
    }
    eindstop = 0 ;                       //?
  }
  else
  {
    digitalWrite(9 , HIGH);
    servo_pin_11.write( 90 );
  }
  if (DigitalRead(4))
  {
    vacuum = ( vacuum + 4 ) ;
  }
  else
  {
    if (( ( vacuum ) <= ( -40 ) ))
    {
      vacuum = -40 ;
    }
  }
  delay( 60 );
  if (DigitalRead(5))
  {
    vacuum = ( vacuum - 4 ) ;
  }
  else
  {
    if (( ( vacuum ) >= ( 1 ) ))
    {
      vacuum = 1 ;
    }
  }
  display.clearDisplay();
  Serial.print(vacuum);
  Serial.print(" ");
  Serial.println();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,5);
  display.println("set cmHg:" );
  display.setCursor(55,5);
  display.print(vacuum );
  display.setCursor(0,20);
  display.println("Vacuum:" );
  display.setCursor(55,20);
  display.print(scale.get_units() );
  display.display();
}  
Reply all
Reply to author
Forward
0 new messages