i have a floor sensor, when something is sensed it will begin a count, when it reaches 10, an output will be activated until the sensor get released, then a count from 10 to 0 will begin then the output will be unactivated.
this is the code
const int sensor_masa = 2; // pin de la entrada de señal del loop
const int ledPin = 13; // pin del LED interno del arduino
const int gobo = 8; // salida del relevador de control del GOBO proyector
int estado_sensormasa= 0; // Estado de lectura del sensor Accesspro
int i = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Inicializacion de led interno Arduino como salida
pinMode(sensor_masa, INPUT); // Inicializacion de pin del sensor del Accesspro como entrada
pinMode(gobo, OUTPUT); // Inicializacion del pin de gobo como salida
Serial.begin(9600);
}
void loop() {
digitalWrite(ledPin,LOW);
digitalWrite(gobo, LOW);
estado_sensormasa = digitalRead(sensor_masa); // Lectura del estado del sensor de piso
if (estado_sensormasa == HIGH)
{
for ( i == 0; i <= 10 ; i++ )
{
if (i==10) {
digitalWrite(ledPin, HIGH);
digitalWrite(gobo, HIGH);
}
Serial.println(i);
delay(1000);
}
}
if (estado_sensormasa == LOW)
{
for (i>0;i>=0;i--){
if(i<=0){
digitalWrite(ledPin, LOW);
digitalWrite(gobo, LOW);
}
Serial.println(i);
delay(1000);
}
}
}
the problem is, when i receive just a pulse, the count starts and dont stop until count 10 then 0.
I interpret this and code to mean: When sensor goes HIGH, wait 10 seconds, and activate output. When sensor goes LOW, wait 10 seconds, and de-activate output.
Questions: What if sensor goes LOW before output is activated? Do you continue to activate it? What if the sensor goes LOW while it is activated do you de-activate it before 10 seconds?
Without these questions answered it is hard to code it properly.
yes bro, that´s my problem, the count should stop if the sensor goes LOW, look, i am trying to design a way to let some drivers to pass in an avenue, if the driver has been waiting for 10 seconds, a stop light will turn ON to stop the drivers in the other street.
i know, but is the only way to stop the for loop, i mean if the sensor is HIGH, the for loop is starting from 0 everytime it reaches 10, so if i use == the for loop stop in 10, hehe
Use an acceptably short delay in your for loop and exit the loop if your sensor becomes LOW. For example, if you use a delay of 500ms, you could do 20 iterations of the for loop while checking the sensor. You would have up to a 500ms delay before exiting the loop.
For example:
if (digitalRead(sensor_masa) == HIGH)
{
int i;
for (i= 0; i < 10 ; i++ )
{
if (digitalRead(sensor_masa) == LOW) break;
delay(500);
}
if (i == 10)
{
// Activate output is sensor remained HIGH for 10 seconds
digitalWrite(ledPin, HIGH);
digitalWrite(gobo, HIGH);
}
}
Use StateChangeDetection to detect changes in the sensor input and use millis() to time the 10 second activation delay
I am not sure if I have understood what you are trying to do, but maybe the following source code goes in the right direction...
const byte sensor_masa = 2; // pin de la entrada de señal del loop
const byte ledPin = 13; // pin del LED interno del arduino
const byte gobo = 8; // salida del relevador de control del GOBO proyector
constexpr byte MAX_COUNT = 10;
constexpr byte MIN_COUNT = 1;
byte estado_sensormasa = LOW; // Estado de lectura del sensor Accesspro
void setup() {
pinMode(ledPin, OUTPUT); // Inicializacion de led interno Arduino como salida
pinMode(sensor_masa, INPUT); // Inicializacion de pin del sensor del Accesspro como entrada
pinMode(gobo, OUTPUT); // Inicializacion del pin de gobo como salida
Serial.begin(9600);
}
void loop() {
static bool active = false;
estado_sensormasa = digitalRead(sensor_masa); // Lectura del estado del sensor de piso
if (estado_sensormasa == HIGH && active == false)
{
active = switchOn(MAX_COUNT, MIN_COUNT);
}
if (estado_sensormasa == LOW && active == true)
{
active = switchOff(MAX_COUNT, MIN_COUNT);
}
}
bool switchOn(byte cMax, byte cMin) {
Serial.println(F("Switch On"));
for (byte i = 0; i < cMax ; ++i )
{
if (i == (cMax - cMin)) {
digitalWrite(ledPin, HIGH);
digitalWrite(gobo, HIGH);
}
Serial.println(i);
delay(1000);
}
return true;
}
bool switchOff(byte cMax, byte cMin ) {
Serial.println(F("Switch Off"));
for (byte i = cMax; i > 0; --i) {
if (i == cMin) {
digitalWrite(ledPin, LOW);
digitalWrite(gobo, LOW);
}
Serial.println(i);
delay(1000);
}
return false;
}
Here are two links to a code simulation.
Because I don't know what kind of sensor you are using, I used a push button to generate a HIGH signal.
The second version of the programme is not blocking. This means that the controller can do other tasks while the sensor signals are being processed.
The programmes are designed in such a way that when a HIGH signal is present, the LED is switched on after 10 seconds. It remains on as long as the HIGH signal is present. If the sensor signal goes LOW, the LED is switched off after 10 seconds.
im using a floor sensor for industrial vehicles, the purpose of this system is
when the driver must work on the station he will put the carrier over the loop, then 10 seconds later a STOP signal will turn on on the corner, but if a driver just pass over the loop, the loop shouldnt start a counting, i mean, it will count just when the carrier is present, when is just a pulse, wont.
yeah! count just when the sensor is HIGH, but when the sensor is LOW after the light was on must wait 10 seconds before turn off, like a traffic light.
i can see your programming is very good, i just learn basics