Changing a limiting variable and Counting with a Sensor

I'm making a code that reads the distance from the ultrasonic sensor and uses it to count the amount of times something went X cm close to it. I think that it'll count more than once when it gets close enough and I need to be able to change this X value after I pressed two of my buttons for, at least, two seconds. I know it probably is answered in here but I'm kinda new to this forums so I don't know where to look for things.

Thanks for your help and sorry to bother you guys!

I think that it'll count more than once when it gets close enough

You need to detect when it becomes close enough rather than when it is close enough

Look at the StateChangeDetection example in the IDE

And if you want help with your code you'll need to post it here so we can see it. And perhaps be clearer about what it is supposed to do e.g. how does pressing 2 buttons for 2 seconds change the value of X?

Steve

slipstick:
And if you want help with your code you’ll need to post it here so we can see it. And perhaps be clearer about what it is supposed to do e.g. how does pressing 2 buttons for 2 seconds change the value of X?

Steve

I though of doing so but it’s written in portuguese and I didn’t do it, but I think it’s pretty straight forward so I’ll do it now.

It’s a pretty long code, I didn’t knew Arduino could make functions other than setup and loop, and I’m too lazy to change it…

#include <Wire.h> // permite o uso de modulos I2C
#include <LiquidCrystal_I2C.h> // adapta o LCD para uso com modulo I2C
#include <Ultrasonic.h> // permite o uso do sensor ultrassonico

int distance;
int distMinima;
int repeticoes;
int limRepeticoes; // botao +/-
int series;
int limSeries; // botao +/-
int reset; 
int menu;
int menuSelect; // botao menu
int tempoDescanso[] = {0, 15, 30, 45, 60, 1.5*60, 2*60, 2.5*60, 3*60, 3.5*60, 4*60};
int tempoDescansoSelect; // botao
int mais;
int menos;
unsigned long startMillis;
unsigned long currentMillis;
const int resetPin = 8;
const int menuPin = 9;
const int maisPin = 10;
const int menosPin = 11;
const int triggerPin = 12;
const int echoPin = 13;

// display inicializado no endereço 0x27
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE);

// pinos 12 e 13 do arduino recebem o Trigger e Echo do sensor, respectivamente.
Ultrasonic ultrasonic(triggerPin, echoPin);

void setup() {
 pinMode(resetPin, INPUT);
 pinMode(menuPin, INPUT);
 pinMode(maisPin, INPUT);
 pinMode(menosPin, INPUT);
 Serial.begin(9600);
 lcd.begin (16,2); // 16 linas e 2 colunas
 // zerando as variaveis controladas pelo usuario ou por sensor
 repeticoes = 0;
 limRepeticoes = 0;
 series = 0;
 limSeries = 0;
 menuSelect = 0;
 tempoDescansoSelect = 0;
 distMinima = 5;
}

void loop() {
 distance = ultrasonic.read(); // dados do sensor ultrassonico em CM
 reset = digitalRead(resetPin); // botao de reset do sistema
 menu = digitalRead(menuPin); // botao de troca de menus
 mais = digitalRead(maisPin); // botao de incremento
 menos = digitalRead(menosPin); // botao de decremento
 // ajuste da distancia minima para que uma repeticao conte
 if ((mais == HIGH) && (menos == HIGH))
 {
   distMinima = distance;
 }
 // trabalha com a selecao dos menus
 if (menu == HIGH)
 {
   menuSelect++;  
 }
 else if (reset == HIGH)
 {
   repeticoes = 0;
   limRepeticoes = 0;
   series = 0;
   limSeries = 0;
   menuSelect = 0;
   tempoDescansoSelect = 0;
 }
 
 lcd.setBacklight(HIGH);
 Serial.print("Distancia em CM: ");
 Serial.println(distance);
 delay(400);

 switch (menuSelect) {
 case 0: // mostra
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Series:");
   lcd.setCursor(7,0);
   lcd.print(series);
   lcd.setCursor(9,0);
   lcd.print("/");
   lcd.setCursor(10,0);
   lcd.print(limSeries); 
   lcd.setCursor(0,1);
   lcd.print("Repeticoes:");
   lcd.setCursor(11,1);
   lcd.print(repeticoes);
   lcd.setCursor(13,1);
   lcd.print("/");
   lcd.setCursor(14,1);
   lcd.print(limRepeticoes);    
   break;
 case 1: // limita serie
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Limite de Series");
   lcd.setCursor(7,1);
   lcd.print(limSeries);
   if (mais == HIGH)
   {
     limSeries++;
   }
   else if ((menos == HIGH) && (limSeries > 0))
   {
     limSeries--;
   }
   break;
 case 2: // limita repeticao
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Limite de");
   lcd.setCursor(0,1);
   lcd.print("Repeticoes");
   lcd.setCursor(12,1);
   lcd.print(limRepeticoes);
   if (mais == HIGH)
   {
     limRepeticoes++;
   }
   else if ((menos == HIGH) && (limRepeticoes > 0))
   {
     limRepeticoes--;
   }
   break;
 case 3: // seta tempo de descanso
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Tempo de");
   lcd.setCursor(0,1);
   lcd.print("Descanso");
   lcd.setCursor(12,1);
   lcd.print(tempoDescanso[tempoDescansoSelect]);
   if ((mais == HIGH) && (tempoDescansoSelect < 10))
   {
     tempoDescansoSelect++;
   }
   else if ((menos == HIGH) && (tempoDescansoSelect > 0))
   {
     tempoDescansoSelect--;
   }
   else if ((mais == HIGH) && (tempoDescansoSelect == 10))
   {
     tempoDescansoSelect = 0;
   }
   break;
 case 4:
   lcd.clear();
   lcd.setCursor(3,0);
   lcd.print("Exercicio");
   lcd.setCursor(3,1);
   lcd.print("Concluido!");
   delay(400);
   lcd.setBacklight(LOW);
   delay(400);
   lcd.setBacklight(HIGH);
   delay(400);
   lcd.setBacklight(LOW);
   delay(400);
   lcd.setBacklight(HIGH);
   delay(400);
   lcd.setBacklight(LOW);
   delay(400);
   lcd.setBacklight(HIGH);
   delay(400);
   lcd.setBacklight(LOW);
   delay(400);
   lcd.setBacklight(HIGH);
   delay(400);
   menuSelect = 0;
 default:
   menuSelect = 0;
 }

 // trabalha com as repeticoes
 if ((distance <= distMinima) && (reset == LOW) && (repeticoes < limRepeticoes))
 {
   repeticoes++;
   Serial.print("Repeticao Concluida: ");
   Serial.println(repeticoes);
 }
 if (repeticoes == limRepeticoes)
 {
   for (int i = tempoDescanso[tempoDescansoSelect] ; i >= 0 ; i--) {
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("Tempo de");
     lcd.setCursor(0,1);
     lcd.print("Descanso");
     lcd.setCursor(12,1);
     lcd.print(i);
     delay(1000);
   }
   menuSelect = 0;
   series++;
 }
 if (series == limSeries)
 {
   menuSelect = 4;
 }
}

mais = plus
menos = minus
so I want to hold plus and minus for, at least, two seconds so It saves the distance the ultrasonic sensor is reading at the moment as the minimum distance.

My code counts the amount of repetitions you make in a gym equipment based on the distance to the weight.

UKHeliBob:
You need to detect when it becomes close enough rather than when it is close enough

Look at the StateChangeDetection example in the IDE

I got how he did it at the example but I can't think of a way to do it for the ultrasonic sensor

I got how he did it at the example but I can't think of a way to do it for the ultrasonic sensor

Set a boolean variable to false. If the boolean is false and the distance is less than your target minimum add 1 to the count and set the boolean variable to true. If the distance is greater than your minimum set the boolean to false.

UKHeliBob:
Set a boolean variable to false. If the boolean is false and the distance is less than your target minimum add 1 to the count and set the boolean variable to true. If the distance is greater than your minimum set the boolean to false.

Man you're a genius hahahaha This will help me a lot, thanks!!!

Now I just need to figure out how to get the minimum distance set after pressing + and - for, at least, 2 seconds and I'll be done!!

I though of doing so but it's written in portuguese

The Arduino is NOT programmed in Portuguese. It is programmed in C++.

PaulS:
The Arduino is NOT programmed in Portuguese. It is programmed in C++.

ohh i was trying to tell it to do something by writing a formal letter and asking it if it could...
Jokes aside, I meant that the variable names are in portuguese and also the comments, so people might not understand it that easily.