Help with code - easy one

I saw post #1 and took @eddiefollow literally.

If pin 13 is made an output with pinMode() in setup(), then two statements in the loop suffice, viz:

  if (vin < 10) digitalWrite(13, LOW);

  if (vin > 13) digitalWrite(13, HIGH);

If the voltage is between 10 and 13, no change is made. I thought it was hysteresis.

The use of < versus <= and the other test is not clear. Measuring with the Arduino analog read it is unlikely to be important which is used.

a7

Remove the "V".

In code, numbers are just numbers, they don't have any units like some values in the real world.

This works for me fine.
If the voltage is below 10V pin13 goes LOW and if higher than 10V goes HIGH.
Now I need to add next Pin that will use the opposite.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET          A4

#define SSD1306_WHITE 1
#define SSD1306_BLACK 0
#define SSD1306_INVERSE 2

Adafruit_SSD1306 display(OLED_RESET);

int analogInput = A0;  //Sensor Input
int digitalOutput = 13;

// These constants won't change:

const int threshold = 10;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  pinMode(analogInput, INPUT);
  pinMode(13, OUTPUT);

  Serial.begin(9600);
  Serial.println("VOLTMETER");

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.display();
  display.clearDisplay();

  display.display();

  display.drawRect(0, 0,  128 , 12, SSD1306_WHITE);
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  display.setTextSize(1);
  display.setCursor(18, 3);
  display.println("    VOLTMETER   ");
  display.display();
}

float vout = 0.0;
float vin = 0.0;

float R1 = 51000.0;
float R2 = 30000.0;

int value = 0;

void loop() {

  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));

  Serial.print("INPUT Voltage= ");
  Serial.println(vin, 2);

  display.setTextSize(2);
  display.setCursor(20, 15);
  display.println(vin, 2);
  display.setCursor(80, 15);
  display.println("V");
  display.display();

  delay(1000);

  // read the input pin
  int analogValue = analogRead(vin);

  // if the analog value
  if (analogValue > threshold) {
  if (vin < 10) digitalWrite(13, LOW);

  if (vin > 10) digitalWrite(13, HIGH);
  }

}

Do you really want to have the delay before you change the led?

I changed it to 300 because it also affects the oled display I don't want to remove it completely - voltage read by analogPin fluctuates by mV...

Small delay don't bother me :slight_smile:

It's where the delay is that matters. You need a delay to prevent change from making the display unreadable but now you have the led change and right after that the display changes and then the pause that separates the two.

Think about it for a while.

How can I separate delay from display and output pin?

Just added another output pins to execute ON/OFF state of DC motor.
Need to filter some noises in circuit to get more stable Voltage readings.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET          A4

#define SSD1306_WHITE 1
#define SSD1306_BLACK 0
#define SSD1306_INVERSE 2

Adafruit_SSD1306 display(OLED_RESET);

int analogInput = A0;  //Sensor Input
int digitalOutput = 13;
int IN1 = 7;  // Pin IN1
int IN2 = 6;  // Pin IN2

// These constants won't change:

const int threshold = 10;   // an arbitrary threshold level that's in the range of the analog input



void setup() {
  pinMode(analogInput, INPUT);
  pinMode(13, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  Serial.begin(9600);
  Serial.println("VOLTMETER");

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.display();
  display.clearDisplay();

  display.display();

  display.drawRect(0, 0,  128 , 12, SSD1306_WHITE);
  display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  display.setTextSize(1);
  display.setCursor(18, 3);
  display.println("    VOLTMETER   ");
  display.display();
}

float vout = 0.0;
float vin = 0.0;

float R1 = 51000.0;
float R2 = 30000.0;

int value = 0;

void loop() {

  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));

  Serial.print("INPUT Voltage= ");
  Serial.println(vin, 2);

  display.setTextSize(2);
  display.setCursor(20, 15);
  display.println(vin, 2);
  display.setCursor(80, 15);
  display.println("V");
  display.display();

  delay(100);

  // read the input pin
  int analogValue = analogRead(vin);

  // if the analog value
  if (analogValue > threshold) {
  if (vin < 10) digitalWrite(13, LOW);
  if (vin > 10) digitalWrite(13, HIGH);
  
  if (vin > 10) digitalWrite(IN1, HIGH);
  if (vin > 10) digitalWrite(IN2, LOW);
  if (vin < 10) digitalWrite(IN1, LOW);
  if (vin < 10) digitalWrite(IN2, LOW);

  }

}

Move the line with delay() to after the if with the led code, make it the last thing that runs in loop().

If the delay() is too short the display will change too fast to read properly when V changes much.

You're just starting, you'll catch on.

Understand.

Hi guys,
I would like to make changes to the last function of the code.

this part:

 // if the analog value
  if (analogValue > threshold) {
  if (vin < 10) digitalWrite(13, LOW);
  if (vin > 10) digitalWrite(13, HIGH);
  
  if (vin > 10) digitalWrite(IN1, HIGH);
  if (vin > 10) digitalWrite(IN2, LOW);
  if (vin < 10) digitalWrite(IN1, LOW);
  if (vin < 10) digitalWrite(IN2, LOW);

is replaced with:

 if (vin >= loLimit && vin <= hiLimit) {
    digitalWrite(13, LOW);
} else {
    digitalWrite(13, HIGH);

  delay(200);

limits are set:

const int loLimit = 10;
const int hiLimit = 12;

When VIN value is read 10V(loLimit) -12V (hiLimit) Pin13 is set LOW and any reading outside the range will set it HIGH (funcion above)

Now I want to add another part were the VIN is read 12V or above then lets say Pin8 is set LOW and set HIGH when VIN is read 10V.

I need to make them work one after another(only one at time) because they both use the same range from 12V to 10V but I don't know how to do it.

Any suggestions?

You can reuse numbers. Just because one code section does some tests doesn't mean you can use the same or different logic and the same or different numbers for another test.

You have proposed using a separate pin for another LED, so I can't see any problem.

You can put more statements to do other stuff in the loop() function.

Hint: at this point in your journey, you can use copy, paste and edit to grow code. Just don't make too much of a habit of that, when you are writing more complicate sketches it is not a good way to code.

a7

Sorry but maybe I wasn't too specific describing it.

I need to make these two work separately and only one at time.

 // if the analog value

  if (vin >= loLimit && vin <= hiLimit) {
    digitalWrite(13, LOW);
} else {
    digitalWrite(13, HIGH);

// this part of code to be only utilized when first part "if" is executed and cannot happen at the sime time

  if (vin >= loLimit && vin <= hiLimit) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
} else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    }

Sorry, I can make no sense of that.

If the second thing should happen only if the first thing did, how can it not be at the same time? How long should it wait after

digitalWrite(13, LOW);

the only meanig I can imagine for

only utilized when first part "if" is executed

The LED at 13 is turned off. Then... ?

a7

Well its not LED in my case but mosfet. We can assume its charging process so voltage will be rising until reaches 12V, the second part should be discharging.

I may add a push button to the second part but it won't be fully automated...

Charging a 12V battery is done with 13+V, usually 14V.

This could be any battery and voltage used in my code is just an example, this going to be change to suit.

@eddiefollow

Don't use this.

if (vin >= loLimit && vin <= hiLimit) 

Use 2 if lines , you only want switch when Batt is 10V or 13.8.

if(vin < 10.0)
{
digitalWrite(13,HIGH);//choose any pin 
}

if(vin > 13.8)
{
digitalWrite(13,LOW);//choose any pin 
}


With a little fix:

else if's can be 'laddered' to slice off conditions in ways more flexible than a switch-case statement.

A structure can make "is it more than this, or this, or this..." for values to text lengths to wait times to handle all kinds of nuance where the first if is for the most common condition that exits the ladder when finished, never checks the else's and only later conditions execute multiple checks. Whether you start high or low depends on what should run faster on average.

There are times to not use the else but in this case if the first ( < 10.0 ) is true the next ( > 13.8 ) can't be true and that is when to use the else.

The thing to do is look over your code with awareness of what it says even if it is later on. The more double-checking extra passes you make the more you see ahead later. Going back over my code for many years really helped and be sure there's still some things I missed but I'm not stuck in my 3rd or 4th year pattern-ruts.

Just to get the OP in some sort of "easy one" bare basic's.

It's just me , but I see no deference with or without the else.

if(vin < 10.0)
{
  digitalWrite(13,HIGH);//choose any pin 
}

else 

if(vin > 13.8)
{
  digitalWrite(13,LOW);//choose any pin 
}

No cut there. There is no difference to the compiler. It may even build the else into optimization.

I just want the extra thought made as once it becomes routine it will be there when planning, right in the approach before any code is written at all. What I did as I got more and more experience was like stages that I haven't made my last stage yet.