analog input counter

hi guys, i've seen many topics about counters but using only push buttons or digital inputs, im doing a counter using the analogs pins as inputs, my goal is that when an analog pin reach a specific value (let's say 3v) the count add 1, i was trying to adapt a digital counter sketch to this but with no results, hope that you can help me..

ps: sorry for the crappy english :cold_sweat:

i was trying to adapt a digital counter sketch to this but with no results

Let us see it.

Show us a schematic of the actual circuit as it is wired ( and/or a good picture).

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int viauno = 7;
const int viados = 8;
const int viatres = 9;
const int viacuatro = 10; 
int valoruno,valordos,valortres,valorcuatro = 0;
int aforouno,aforodos,aforotres,aforocuatro = 0;
int anterioruno,anteriordos,anteriortres,anteriorcuatro = 0;

void setup (){
  pinMode (viauno, INPUT);
  pinMode (viados, INPUT);
  pinMode (viatres, INPUT);
  pinMode (viacuatro,INPUT);
  digitalWrite(viauno,LOW);
  digitalWrite(viados,LOW);
  digitalWrite(viatres,LOW);
  digitalWrite(viacuatro,LOW);
  lcd.begin(16,2);
  lcd.print("V1:");
  lcd.setCursor(9,0);
  lcd.print("V2:");
  lcd.setCursor(0,1);
  lcd.print("V3:");
  lcd.setCursor(9,1);
  lcd.print("V4:");
  }
void loop(){
  
  int SensorV1 = analogRead (A0);
  int SensorV2 = analogRead (A1);
  int SensorV3 = analogRead (A2);
  int SensorV4 = analogRead (A3);
  float VoltajeV1 = SensorV1 * (5.0 / 1023.0);
  float VoltajeV2 = SensorV2 * (5.0 / 1023.0);
  float VoltajeV3 = SensorV3 * (5.0 / 1023.0);
  float VoltajeV4 = SensorV4 * (5.0 / 1023.0);
  
   lcd.setCursor (3,0);
  valoruno=digitalRead(viauno);
    if(valoruno!=anterioruno){
      if(valoruno==1){
         aforouno++;
         lcd.print(aforouno);
       }
       }
  lcd.setCursor (12,0);
  valordos=digitalRead(viados);
    if(valordos!=anteriordos){
      if(valordos==1){
         aforodos++;
         lcd.print(aforodos);
       } 
       }
  lcd.setCursor (3,1);
  valortres=digitalRead(viatres);
    if(valortres!=anteriortres){
      if(valortres==1){
         aforotres++;
         lcd.print(aforotres);
       }
       }
 lcd.setCursor (12,1);
  valorcuatro=digitalRead(viacuatro);
    if(valorcuatro!=anteriorcuatro){
      if(valorcuatro==1){
         aforocuatro++;
         lcd.print(aforocuatro);
       }
       }
  anterioruno=valoruno;
  anteriordos=valordos;
  anteriortres=valortres;
  anteriorcuatro=valorcuatro;
  
  if (VoltajeV1 >= 4.0){
   digitalWrite (7,LOW);
   delay (10);
   digitalWrite (7,HIGH);
 }
 if (VoltajeV2 >= 4.0){ 
    digitalWrite (8,LOW);
    delay (10); 
    digitalWrite (8,HIGH);
 }
 if (VoltajeV3 >= 4.0){
    digitalWrite (9,LOW);
    delay (10);
    digitalWrite (9,HIGH);
 }
 if (VoltajeV4 >= 4.0){
    digitalWrite (10,LOW);
    delay (10);
    digitalWrite (10,HIGH);
 }
}

it’s a mess i know :~

To start off:
In your words, what happens in these lines?
pinMode (viauno, INPUT);
digitalWrite(viauno,LOW);

Do you have anything connected to Digital Pins 7,8,9 or 10?

LarryD:
To start off:
In your words, what happens in these lines?
pinMode (viauno, INPUT);
digitalWrite(viauno,LOW);

Do you have anything connected to Digital Pins 7,8,9 or 10?

nope, in digital pins 7,8,9,10 i want a pulse (viauno = 7, viados = 8,...), that pulse in this code will generate +1 in their respective count.
basically what i wanted to do is that when the analog pin read a voltage of 4v or more, the digital pin (7,8,9,10) will read a pulse to add 1 to the count.

const int viauno = 7;

pinMode (viauno, INPUT); //This makes viauno (pin 7 on the arduino) an INPUT. Which allows you to read digital state on that pin.

Now you have:
if (VoltajeV1 >= 4.0){
digitalWrite (7,LOW); // You are trying to use a digitalWrite to an INPUT pin (remenber viauno=7) . Why?
delay (10);
digitalWrite (7,HIGH);

LarryD:

const int viauno = 7;

pinMode (viauno, INPUT); //This makes viauno (pin 7 on the arduino) an INPUT. Which allows you to read digital state on that pin.

Now you have:
if (VoltajeV1 >= 4.0){
digitalWrite (7,LOW); // You are trying to use a digitalWrite to an INPUT pin (remenber viauno=7) . Why?
delay (10);
digitalWrite (7,HIGH);

that was my attemp to recreate a pulse in pin 7, but i guess it doesnt work like that, what can i do? i just want a pulse in 7,8,9,10 if there’s a voltage of 4 or higher on a specific analog pin

i just want a pulse in 7,8,9,10

Not sure what you mean by "want a pulse".

A Pulse is defined as a voltage change on a pin from some voltage to another voltage then back again.
Example: from 0 volts to 5 volts then back to 0 volts on pin 7 if it has been set up as an Output pin.
You wait at 5 volts for the period of time you desire. Is this what you want?

A Pulse is defined as a voltage change on a pin from some voltage to another voltage then back again.
Example: from 0 volts to 5 volts then back to 0 volts on pin 7 if it has been set up as an Output pin.

exactly that is what i want, a pulse, a 5-0-5 volt change on pin 7,8,9,10, for example, if i place a push button on pin 7 grounded, when i press that button, the counting is perfect, but i want that to happen with the analog input instead of a push button

nope, in digital pins 7,8,9,10

You said you had nothing connected to these pins so I see no reason to do a digitalWrite to any of them.

I believe you want to display a count on the LCD.
If analog I/P A0 is >=4 volts you want the counter to increment and be displayed after V1:

I don’t have a spare LCD so I cannot check the following code.
Try this 1 analog I/P sketch.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int SensorV1;  //Used to store the analog reading
float VoltajeV1;
int aforouno=0;  //our counter

void setup (){
  lcd.begin(16,2);
  lcd.setCursor (0,0);
  lcd.print("V1:");
}
void loop()
{
  SensorV1 = analogRead (A0);
  VoltajeV1 = SensorV1 * (5.0 / 1023.0);

  if (VoltajeV1 >= 4.0)
  {
    aforouno++;
    lcd.setCursor (3,0);
    lcd.print(aforouno);
    }
   delay(1000);
}

Try this 1 analog I/P sketch.

tried that code and the counting works good, but it keeps counting forever when i only need only one count for each rise to more than 4v in analog pin

for example, i want that if i have a 0v in analog pin and rise to 4.5v and stays in 4.5v (for any amount of time) the count will still remain in "1", when the voltage fall to less than 4v and rise again to 4.5v the count will add one and so on.

When the analog >=4Volts you have to disable counting after it has incremented once.
When the voltage becomes <4Volts you have to enable counting.
So you have to introduce a blocking mechanism to achieve this.
See code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int SensorV1;             //Used to store the analog reading
float VoltajeV1;
int aforouno=0;          //our counter
boolean lock = false;   //used to disable incrementing while voltage is >=4.0V 

//***************
void setup (){
//  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.setCursor (0,0);
  lcd.print("V1:");
}                          //End of setup()

//***************
void loop()
{
  SensorV1 = analogRead (A0);
  VoltajeV1 = SensorV1 * (5.0 / 1023.0);

  if (VoltajeV1 >= 4.0 && lock ==false)
  {
    aforouno++;
//    Serial.println(aforouno);  
    lcd.setCursor (3,0);
    lcd.print(aforouno);
    lock = true;         //prevent further incrementing of the counter
  }

  if (VoltajeV1 < 4.0 )   
  {
    lock = false;        //the voltage is below 4.0
                            //allow incrementing of the counter
  }

  delay(100);
}                           //End of loop()

When the voltage becomes <4Volts you have to enable counting.
So you have to introduce a blocking mechanism to achieve this.

THIS! works perfect!

this was really helpful

thank you so much :smiley:

Do you understand how things are working?

Do you understand how things are working?

I do thanks :slight_smile:

the first condition allows the counting and when the voltage reach 4 or more add one and blocks the counter, and the second condition unblock the counter when the voltage is less than 4.

Good for you.
You are on your way to a good hobby.

Thanks :slight_smile:

i just notice another thing, i need to count 4 analog inputs, each one are independent of the other, so the lock doesnt work alright

its like 4 counters (A0,A1,A2,A3) one in each analog input, and i need like a specific lock for each one of them.

for example, if the A0 input is in 4.3v it will be locked after that, but the A2 input is in 2v so this one should not be locked

So you need to have 4 locks then.
lock0, lock1, lock2 and lock3

i realized that as soon as i wrote that reply haha, so silly, thanks.