Help with code - easy one

Hi everyone.

Newbie here to coding.
I'm looking for help with code.

Analog pin measures voltage- I use voltage divider here ( voltage higher >5V). I need code that will set digital output pin "X" LOW if read voltage is 10V or lower and set that pin HIGH if voltage is 13 or higher.

Any help will be appreciated.

Part of the code that I use for voltage measurement :

#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

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

  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);


}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

1 Like

"Voltage". Named after the Italian physicist Alessandro Volta.

1 Like

Lose all the display stuff and get the analog thing working using the serial monitor for output.

Divide and conquer. Work on getting a simple thing like you are trying to do to work.

Post that sketch I said you shoud try first. It should be a complete it compiles we clu,d try it ourselves (many of us can and would) program.

You didn't actually say what was going wrong, so that would help too.

What does it do that it shouldn't, or fail to do what it should?

a7

The part of code I have posted is working properly;
Measured voltage is also displayed on small oled display.

I need to add this part ;

digital output pin "X" LOW if read voltage is 10V or lower and set that pin HIGH if voltage is 13 or higher.

Corrected :slight_smile:

1 Like

Just edit your statement of requirements into the simple bit of code it represents. CLUE, it starts with if.

I got in my head how in general it should looks like but I am completely new to it and will be easier to modify something that already exist.

So i will simplify whithout "proper" terminology.

if
analog pin (0) read 10V or less
digital pin (13) low
else
digital pin(13) high

Very good attempt, but your original statement left a voltage range of >10 and <13 with no pin active.

if
analog pin (0) read 10V or less
digital pin (13) low
if
analog pin (0) read 10V or more
digital pin (13) high

0 and 13 are just pin numbers

if (analogValue <=10V) {
  digitalWrite(13, HIGH);
  } else {
  digitalWrite(13, LOW); 
}

or something like that

Can anyone fix that for me please?

Do you really mean that 10V exactly is the same as less than 10V? I see far too many cases of folks using <= when they really mean <.

I meant anything below 10V or equal, or if there is no difference just <10V.

I give up.

Why does it matter, I just ask questions and I already said I am new to it so not everything is obvious.

Can't you just point what is wrong and how it should be done.

So much of programming is about attention and detail.

What is missing in the code in post #1?
One thing of many is the part where the digital pin goes LOW or HIGH.
It's like someone didn't bother learning how.

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

How to use analogpin that reads voltage and combine it properly with "if" function.
Im trying to understand basic stuff.

NO, you don't learn anything that way.
Just an FYI, my first computer was 1959, before Steve Jobbs attended kindergarten.
I had to learn by reading. I will point you in the right direction, but I rarely give complete solutions.
You really need to learn boolean logic, less than or equal is NOT the same as less than.

No, it's not the same, but one or the other should work in my case, i guess.

It's my day 2 with arduino... so again, basics, basics, basics.

I dont expect anything from anyone. I just assumed forum like this is a good place for noobs like me, I could be wrong, though.

There is no matrix download, day 2.
What you would learn to keep takes long term memory.

Here is a Section 5 Tutorial built into the IDE:

/*
  Conditionals - If statement

  This example demonstrates the use of if() statements.
  It reads the state of a potentiometer (an analog input) and turns on an LED
  only if the potentiometer goes above a certain threshold level. It prints the
  analog value regardless of the level.

  The circuit:
  - potentiometer
    Center pin of the potentiometer goes to analog pin 0.
    Side pins of the potentiometer go to +5V and ground.
  - LED connected from digital pin 13 to ground through 220 ohm resistor

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 17 Jan 2009
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/ifStatementConditional
*/

// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}