Pages: [1]   Go Down
Author Topic: Vibration sensors from MEAS  (Read 946 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

I have connected two sensors (MEAS) that sense vibrations to my Arduino Uno. I'm a real beginner on both programming and electronics but I have written a short code that allows me to get two different LED to lit up when I trigger the sensors (the LEDs are triggered separately by each sensor). However, I want to see which of the sensors that was triggered first, and my code just lit both of the LEDs. I would like to extend my code in some way so that once one of the sensors has been triggered the Arduino should not allow the other sensor to trigger (and vice versa). Does anybody knows how to extend my code so that becomes possible?

Here is the code, Cheers Martin

int analogPin0 = 0; // input for 1st vibration sensor (MEAS)
int analogPin5 = 5; // input for 2nd vibration sensor (MEAS)
int ledPin13 = 13; // LED connected to digital pin 13
int ledPin12 = 12; // LED connected to digital pin 12
int val0 = 0; // variable to store the value read
int val1 = 5;
int low = 10;
int high = 800;

void setup()
{
//Serial.begin(9600); // setup serial, has to be stopped to allow LED to lit
pinMode(ledPin12, OUTPUT); // sets the pin as output
pinMode(analogPin0, INPUT); // sets the pin as input
pinMode(ledPin13, OUTPUT); // sets the pin as output
pinMode(analogPin5, INPUT); // sets the pin as input

  }

void loop()
{
val0 = analogRead(analogPin0); // read the input pin
Serial.println(val0); // debug value

val1 = analogRead(analogPin5); // read the input pin
Serial.println(val1); // debug value

if ( val0 > high ) {
    digitalWrite(ledPin12, HIGH);
    delay(500);   //seems very important to get each sensor trigger only ONE LED
}

if (val1 > high) {
    digitalWrite(ledPin13, HIGH);
    delay(500); //seems very important to get each sensors trigger only ONE LED
   }
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here some first try (not tested)

analog pins are refered to as A0 A1 A2 etc
You do not need to set them to INPUT

removed comments
Code:
int analogPin0 = A0; // input for 1st vibration sensor (MEAS)
int analogPin5 = A5; // input for 2nd vibration sensor (MEAS)

int ledPin13 = 13; 
int ledPin12 = 12;

int val0 = 0;   // variable to store the value read
int val1 = 5;

int high = 800;

int first = -1; // who was first is undecided

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);

  digitalWrite(ledPin12, LOW);
  digitalWrite(ledPin13, LOW);
}

void loop()
{
  val0 = analogRead(analogPin0);
  val1 = analogRead(analogPin5);

  if (val0 > high && val1 < high)
  {
    first = 0;
    digitalWrite(ledPin12, HIGH);
    Serial.println("0 was first");
  }

  if (val0 < high && val1 > high)
  {
    first = 1;
    digitalWrite(ledPin13, HIGH);
    Serial.println("1 was first");
  }

  if (val0 > high && val1 > high)
  {
    first = 2;
    Serial.println("equal .... retry");
  }

  if (first >= 0 ) 
  {
    first = -1;
    delay(5000);
    digitalWrite(ledPin12, LOW);
    digitalWrite(ledPin13, LOW);
    delay(3000);
    Serial.println("GO!");
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

Thanks Rob, that helped a lot!

However, I would like the sensor that is triggered first to lit its LED and that the LED continuous to glow (and that the other sensor cannot be triggered). The reason is because I will use it in an application where I want to be able to judge (afterwards) which of the two sensors that was triggered first. I'm a biologist and interested in adaptive coloration in prey and I will use this little gadget to see which out of two offered prey types that wild birds will attack first.

Cheers Martin
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int analogPin0 = A0; // input for 1st vibration sensor (MEAS)
int analogPin5 = A5; // input for 2nd vibration sensor (MEAS)

int ledPin13 = 13; 
int ledPin12 = 12;

int val0 = 0;   // variable to store the value read
int val1 = 5;

int high = 800;

void setup()
{
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);

  digitalWrite(ledPin12, LOW);
  digitalWrite(ledPin13, LOW);
}

void loop()
{
  val0 = analogRead(analogPin0);
  val1 = analogRead(analogPin5);

  if (val0 > high && val1 < high)
  {
    digitalWrite(ledPin12, HIGH);
    while(1);
  }

  if (val0 < high && val1 > high)
  {
    digitalWrite(ledPin13, HIGH);
    while(1);
  }
}
totally stripped, you need to reset after each usage.
Next changes it is your turn to try, as you have some basic examples now smiley-wink

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: