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