Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Decrease the Value in "Button State Change"  (Read 796 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()

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


 vibstate = digitalRead(vibPin);
 
 
  { if (vibstate != viblast)
    {if (vibstate == HIGH)
      {vibcount++;
       Serial.print("Anzahl der Erschuetterungen: ");
       Serial.println(vibcount, DEC);}}}
      
     //else {
      if (vibcount > 25)
       {vibcount--;
       Serial.print("Anzahl der Erschuetterungen: ");
       Serial.println(vibcount, DEC);}
      
 viblast = vibstate;
      
  if (vibcount > 20)
    {digitalWrite(13, HIGH);
     digitalWrite(10, LOW);}
    
  else    
    {digitalWrite(10, HIGH);
     digitalWrite(13, LOW);}
}
Thanks for your help smiley
« Last Edit: July 11, 2011, 08:49:52 am by DonTomato » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
{ if (vibstate != viblast)
    {if (vibstate == HIGH)
      {vibcount++;
       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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 e.g.
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... :-/
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
unsigned long lastSensorChange; // Put this before setup()

{ if (vibstate != viblast)
    {if (vibstate == HIGH)
      {vibcount++;
       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
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot... I will try as soon as possible and post if it worked :-)

UPDATE:
THANK YOU!! It's working exactly as i wanted it to. Kinda love you a little bit right now  smiley-lol

UPDATE 2:
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)
« Last Edit: July 11, 2011, 10:18:22 am by DonTomato » Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: