Go Down

Topic: Shaking a tilt sensor (Read 4347 times) previous topic - next topic

Samory

Hello,

I'm totally new to Arduino, and I previously posted a question about a tilt sensor, and that part is working.
Now every time I turn the tilt sensor, an extra LED is shining. As you can see in the code below.

I'm using a 4-directional tilt sensor and an Arduino Nano


Now my problem is: I want to make it start (turn the first LED (ledPin7) on + start the rest of the code) by shaking.
And of course the same for turning the thing OFF (shut all the LED's down)
I can't make it in Arduino-code, so if you can help me please?

I was thinking:


if inPin2 and/or inPin3 goes on and off (HIGH & LOW) 5 times in 3 seconds
          'start the code below'

Code: [Select]
int ledPin12 = 12;
int ledPin11 = 11;
int ledPin10 = 10;
int ledPin9 = 9;
int ledPin8 = 8;
int ledPin7 = 7;
int inPin2 = 2;
int inPin3 = 3;
int val = 0;

void setup()
{
  pinMode(ledPin7,OUTPUT);
  pinMode(ledPin8,OUTPUT);
  pinMode(ledPin9,OUTPUT);
  pinMode(ledPin10,OUTPUT);
  pinMode(ledPin11,OUTPUT);
  pinMode(ledPin12,OUTPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);
}

void loop()
{
val = digitalRead(inPin2); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin7, HIGH); // turn LED ON
} else {
digitalWrite(ledPin7, HIGH); // turn LED ON
digitalWrite(ledPin8, HIGH); // turn LED ON


    val = digitalRead(inPin2); // read input value
    if (val == HIGH) { // check if the input is HIGH (button released)
    digitalWrite(ledPin7, HIGH); // turn LED ON
    } else {
    digitalWrite(ledPin7, HIGH); // turn LED ON
    digitalWrite(ledPin8, LOW); // turn LED OFF


val = digitalRead(inPin3); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin9, LOW); // turn LED ON
} else {
digitalWrite(ledPin8, HIGH); // turn LED ON
digitalWrite(ledPin9, HIGH); // turn LED ON

val = digitalRead(inPin2);
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin7, HIGH); // turn LED ON
digitalWrite(ledPin9, HIGH); // turn LED ON
digitalWrite(ledPin10, HIGH); // turn LED ON
digitalWrite(ledPin8, HIGH); // turn LED ON
} else {
digitalWrite(ledPin10, LOW); // turn LED OFF


val = digitalRead(inPin2); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin7, HIGH); // turn LED ON
} else {
digitalWrite(ledPin7, HIGH); // turn LED ON
digitalWrite(ledPin8, HIGH); // turn LED ON
}
}
}
}
}


Thanks!

Samory

Samory

I have found something, I can make one LED light up by shaking, but when i want to put the other part underneath the 'if..' it won't work! ;(
Please, it is a school project and very important! :)

Code: [Select]
int ledPin12 = 12;
int ledPin11 = 11;
int ledPin10 = 10;
int ledPin9 = 9;
int ledPin8 = 8;
int ledPin7 = 7;
int inPin2 = 2;
int inPin3 = 3;
int val = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
long firstPress = 0;
long countingTime = 5000;
int presses  = 0;

void setup()
{
  pinMode(ledPin7,OUTPUT);
  pinMode(ledPin8,OUTPUT);
  pinMode(ledPin9,OUTPUT);
  pinMode(ledPin10,OUTPUT);
  pinMode(ledPin11,OUTPUT);
  pinMode(ledPin12,OUTPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);
 
  }

void loop()
{
     if(digitalRead(inPin2) == HIGH)
   {
firstPress = millis();
presses = 1;
while(millis() - firstPress < countingTime)
{
     if(digitalRead(inPin2) == HIGH)
     {
   presses++;
}

}
if(digitalRead(inPin2) == HIGH)
{
   firstPress = millis();
   presses = 5;
   
   digitalWrite(ledPin7,HIGH);  // here there should be the other part, where there are more LEDS ON                          by turning the device, this does not work by replacing!
}
}
}

Grumpy_Mike

You are only ever reading inPin2 so if the first if() is true so is the second.

Grumpy_Mike

Stop reading each of the inputs individually and combine into one with something like this:-
Code: [Select]
pos = digitalRead(inPin2) + digitalRead(inPin3)<<1;
Then the value of pos will be 0 to 3 depending on the orientation. Do this once at the start of the loop and use the variable pos in all your if() statements. This ensures they are always consistent through one time round the loop.

Write a small program to print out just these values and look and see how they change when you shake them. Then you can use that information to write a holding loop at the start of the function loop(). You only enter this loop if a 'running' flag is clear. When you exit this loop set this flag and make a note of the time using millis(). Refresh this time each time an active movement, then at the end of the loop() check if the time now is greater that the "go to sleep time", if so then clear the 'running' flag and your code will be caught at the top of the next time round loop().

Samory

For the shaking, combining is not necessary I think!?
And for the other code, where extra LEDs are lightening, are you sure this is relevant? Because, when you turn it 90°, there is another output that is detecting the movement (inPin2 en inPin3).

But still, I don't understand how to write the code for the shaking.
I looked all over the internet for similar things, but I only find that one I posted.
It's hard for me to improvise on such things due to the fact I never programmed anything.

Thanks!

Grumpy_Mike

Look itis your project you are supposed to write it yourself not look at the Internet for someone who has done it before?

Quote
For the shaking, combining is not necessary I think!?

No point in telling you then.
Good luck.

Samory

I'm student industrial product design, and I need that Arduino to work for in my prototype. So there is nothing bad I use knowledge of others. The other codes I didn't found literally on the internet, but I used pieces from several codes I found on the interet and used my logical thinking, watched a lot of tutorials. And the circuit I figured out myself. If the product I'm designing is coming on the market, It won't be with an Arduino.
I put a lot of effort in it ;)

Go Up