Go Down

Topic: Decrease the Value in "Button State Change" (Read 1 time) previous topic - next topic


Jul 11, 2011, 03:40 pm Last Edit: Jul 11, 2011, 03:49 pm by DonTomato Reason: 1
Hi Everyone,

I need help with a (probably) small problem. I used the "Button State Change" to count how often my "vibration sensor" (http://www.conrad.de/ce/de/product/185232/VIBRATIONSSENSOR-JWR-001-02) switches between 0 and 1. The code works fine for me but now i want my arduino to constantly decrease the "number of times" the sensor switched.
For example: I have to LEDs. Number 1 should be on as long as the switch is lower than 20 times on/off. As soon as the switch was turned more than 20 times LED 2 should light... now if the sensor doesn't switch for a couple of seconds LED 1 should light up again (and Number 2 of course turns off).

Here is my code so far:

 const int  vibPin = 7;  
 const int  ledPin = 13;
 int vibcount = 0;
 int vibstate = 0;
 int viblast = 0;

void setup()

{pinMode(vibPin, INPUT);
 pinMode(ledPin, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(7, INPUT);
 Serial.begin (9600);}

void loop()

int druck = analogRead(0);
druck = map(druck, 0, 1023, 85, 200);
 //Serial.print("Drucksensor Feedback: ");
 //Serial.println("    ");
analogWrite(11, druck);

vibstate = digitalRead(vibPin);

 { if (vibstate != viblast)
   {if (vibstate == HIGH)
      Serial.print("Anzahl der Erschuetterungen: ");
      Serial.println(vibcount, DEC);}}}
    //else {
     if (vibcount > 25)
      Serial.print("Anzahl der Erschuetterungen: ");
      Serial.println(vibcount, DEC);}
viblast = vibstate;
 if (vibcount > 20)
   {digitalWrite(13, HIGH);
    digitalWrite(10, LOW);}
   {digitalWrite(10, HIGH);
    digitalWrite(13, LOW);}
Thanks for your help :)


Code: [Select]
vib = map(vib, 0, 1023, 30, 30);

    if (vib > 50)

The map function will map a value from the from range (0 to 1023) to the to range (30 to 30). Since the input value, be definition, be either 0 or 1 (since that is all that digitalRead() can return), the output from the map function will always be 30.

In which case, bothering to read from the digital pin was a waste of time and effort.

Since the output from the map function will always be 30, it will never be greater than 50.

What were you thinking? Some comments in the code might have been useful.
The art of getting good answers lies in asking good questions.


Oups.. big sorry... I posted the wrong code -.-

Now it's the right one... sorry again

It's mostly about the part below "Vibrationssensor" that I need help with...


Code: [Select]
{ if (vibstate != viblast)
    {if (vibstate == HIGH)
       Serial.print("Anzahl der Erschuetterungen: ");
       Serial.println(vibcount, DEC);}}}

Your code would be a lot easier to read if each { and } was on a separate line, and useless { and } were removed.

Your problem would be a lot easier to understand/solve if you explained what you expect this code to do, what it actually does, and how what it does differs from what you want.

Nowhere are you collecting information about time, so expecting vibcount to reset after some period of no changes is not reasonable.
The art of getting good answers lies in asking good questions.


It's my first Code....  :smiley-roll-sweat:

Ok right now it counts the number of times my Sensor switches between 1 and 0. I have two LEDs and as soon as the value reaches 20 the green turns off and my yellow one turns on. So far so good! Now I want that if the sensor doesn't switches on/off over some time the yellow one turns off again and the green one lights up.
But if possible I don't just want to reset the count value to 0 but to decrease it every second -1
The sensor is shaked --> Value goes up to 35 ---> Green LED lights up ---> Sensor is kept steady, after a couple of seconds the value is 25 ---> Still Green Led ----> couple of seconds ----> value is below 20 ----> Yellow LED

I hope it's understandable... :-/


So, each time that the count is changed, you need to record when the count changed. Use millis() to get the current time (milliseconds since reset).

On each pass through loop(), after checking the sensor, but before changing the LED state, compare the current time to the last sensor change time. If the time exceeds some threshold, decrement vibcount by some amount.

Code: [Select]
unsigned long lastSensorChange; // Put this before setup()

{ if (vibstate != viblast)
    {if (vibstate == HIGH)
       lastSensorChange = millis();
       Serial.print("Anzahl der Erschuetterungen: ");
       Serial.println(vibcount, DEC);}}}

unsigned long now = millis();
if(now - lastSensorChange > 1000) // If it has been more than a second with no activity
   vibcount--; // Remove one count (could be more)
   lastSensorChange = now; // Reset time
The art of getting good answers lies in asking good questions.


Jul 11, 2011, 04:57 pm Last Edit: Jul 11, 2011, 05:18 pm by DonTomato Reason: 1
Thanks a lot... I will try as soon as possible and post if it worked :-)

THANK YOU!! It's working exactly as i wanted it to. Kinda love you a little bit right now  XD

ok only one last tiny tiny thing... how can I prevent my value to be smaller than 0 (Son it doesn't count down to -119 whatsoever but only to 0)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131