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

I 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. :confused:

#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();
}

Lots of smiley faces there. Try reading the forum guidelines for how to post code

First, you don't need to configure a pin as input every time you read it:

boolean DigitalRead(int pinNumber)
{
  pinMode(pinNumber, INPUT);
  return digitalRead(pinNumber);
}

Secondly, you don't need near the number parenthesis as you are using:

    if (( DigitalRead(7) && ( ( eindstop ) == ( 1 ) ) )) //?????

could be:

    if (DigitalRead(7) && eindstop == 1)

Thirdly, how are your inputs wired? I assume they are switches and your code implies you have a pull-down resistor for each input. Is this the case? You could use INPUT_PULLUP and wire your switches to ground.

Also, you are testing the state of the switches rather than the state change of the switches. This may be what you want but if you hold the switch down for any length of time you will get multiple increments or decrements of the vacuum.

@pmagowan, Ok

thanks for the response ToddL1962.

yes i use a pullup resistor to the gnd.
the parenthesis that are used are the result of the Ardublock program, it is already an old program but for a person with dyslectic it is a nice solution especially because you have the arduino code that produces the program.
I will erase the parenthesis in the program.
But I don't want to make it too difficult for myself because of the dyslectic that bother me. I get lost in arduino code pretty quickly.

I'm going to clean up the code and see what the result is.

Thanks

if (( DigitalRead(7) aka digitalRead

How do i that for code in that window? like "if (( DigitalRead(7)"

by paying attention to letter case, upper and lower case...

thanks

The OP has his own boolean DigitalRead(int pinNumber) function which sets the pin mode (unnecessarily) and reads the pin. I have already pointed this out.

tl,dr, no code tags, crazy parentheses.

TheMemberFormerlyKnownAsAWOL:
tl,dr, no code tags, crazy parentheses.

LOL

I don't get the LOL. It must be me.
I am trying to "clean" the code but the result remains the same pin D7 is not being read.

maybe this is not a forum to help people who have a little extra difficulty understanding.
Too bad I had good hope.

Thanks for the comments, this topic can be closed for me

It's a great place to get help.
It's not a great place for those who make it hard for themselves to get help.

As a general guide , write your code in little bits and test it as you go . Add print statements to check the code is doing what you want and variables have the values you expect .

You also said something about a pull up resistor connected to 0v , which is wrong - pull ups connect to +5v .

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Thanks TomGeorge,

this is my schematic

Hi,
Thanks for the schematic.
Check your pump wiring, the way you have drawn it will short your 5V supply.

Tom... :slight_smile:
PS I could get to like the automated attachment posting thingyyy.

pmp.jpg

pmp.jpg

If you are suffering dyslexie and created your program using ArduBlock does there exist a sub-forum for AdruBlock?

Is ArduBlock officially supported by Arduino.cc or is ArduBlock a thrird-party-addon?
I tried to find an ArduBlock user-forum but did'nt find one (within 5 minutes of searching)

I think you have to make a fundamental decision:

Choice 1: just using ArduBlock and nothing else

Choice 2: not using ArduBlock but "classcial programming"

If somebody starts to modify your ArduBlock-code ArduBlock will be unable to translate it back to graphical blocks.
Does there exist an online-version of ArduBlock? If Yes this would give easy access to all users to modify your ArduBlock-Code. So this would enable to stay inside ArduBlock.

If not you may consider switching over to the micro:bit-microcontroller which has an online-version for editing programs.

The microcontroller itself has a 5x7 LED-matrix which is able to show "run-through" text. But I don't know if this is easy to read for you.
You can create your own symbols for the 5x7-LED-matrix. So if the visual feedback your device shall give is just 3 to 5 "modes" this could be done by graphical symbols.

The online-editor includes a simulator that shows the LED-matrix. So you might give it a try

If you want to stay with the arduino:
if the code stays 5 to 10 lines short. Are you able to read such short parts?
If yes you just have o switch to the most professional coding-sytle that can be written.

Dividing your complete code into small functions. Each function will be tested on its own and for itself.
You always stay focused on 5 to 10 lines of code.

If it works Your focus switches to the next function again containg onle 5 to 10 lines of code.

To put it all together the functions get called by their name.

So at the end your code of the main-loop looks as short as this

void loop()
   if (EndStop_triggered() ) {
     Stop_Pump()
   }

Of course it will contain a bit more lines inside the loop()
but this code-snippet gives you the impression how it is coded

best regards Stefan