1

I made a vibration sensor using an Arduino. It is connected to an Android app. When the start button is pressed on the app, a red light LED turns on (stand by), and after 2 - 5 seconds, the red LED turns off and a green LED turns on (meaning the system is ready). The green LED turns off if the sensor detects a vibration. The green LED works perfectly. However, the red LED is always dim. I've tried testing the red LED separately, it works fine. I've tried changing the pin it's connected to, it's still dim. I've included my code below if it helps.

Thanks in advance.

int ledPin = 13;
int ledPin2 = 9;
int EP = 7;
int cal = 500;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(EP, INPUT); //set EP input for measurement
  Serial.begin(9600); //init serial 9600
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c == '1') {
      cal = 100;
    }
    if (c == '2') {
      cal = 200;
    }
    if (c == '3') {
      cal = 300;
    }
    if (c == '4') {
      cal = 400;
    }
    if (c == '5') {
      cal = 500;
    }
    if (c == '6') {
      cal = 600;
    }
    if (c == '7') {
      cal = 700;
    }
    if (c == '8') {
      cal = 800;
    }
    if (c == '9') {
      cal = 900;
    }
    if (c == '0') {
      int randm = random(2, 6);
      int x = randm * 1000;
      digitalWrite(ledPin2, HIGH);
      delay(x);
      unsigned int tm = millis();
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPin2, LOW);
      while (1) {
        long measurement = TP_init();
        char br = Serial.read();
        if (br == 'x') {
          break;
        }
        if (measurement > cal) {
          digitalWrite(ledPin, LOW);
          unsigned int out = millis() - tm;
          unsigned char i = out;
          Serial.print(out);
          break;
        }
      }
    }
  }
}

long TP_init() {
  delay(0);
  long measurement = pulseIn (EP, HIGH);
  //wait for the pin to get HIGH and returns measurement
  return measurement;
}
  • 2
    try pinMode(ledPin2, OUTPUT) – Juraj Apr 13 '18 at 3:44
4

You need to add this line for the other LED pin:

pinMode(ledPin2, OUTPUT);
1

You need this same line for the other LED pin:

pinMode(ledPin2, OUTPUT);

  • 1
    This should have been a comment to the other answer; to fix his typo. – Blair Fonville Apr 13 '18 at 3:58
  • 1
    @Blair Fonville - If you hover your mouse over the timestamp at the end of Juraj's comment, you notice a "tool tip" popup with the UTC timestamp of that event. If you compare that to the timestamp of CrossRoads answer, you will see that Juraj's comment gave the correct answer before CrossRoads. If anyone deserves credit for the first correct answer, it's Juraj. – VE7JRO Apr 13 '18 at 4:33
  • Irrelevant. .... – Blair Fonville Apr 13 '18 at 4:41
  • 1
    If it would make you feel any better, you could just down-vote my answer. It will only cost you 1 rep. – VE7JRO Apr 13 '18 at 4:45
  • Crossroads answered only 19 seconds after Juarj's comment. I doubt he had time to read the comment, and type in his own reply, in 19 seconds. Comments under the question are not for answering it. That's why we have answer boxes. Please don't start bickering about this sort of thing. If you like an answer, vote it up, if you dislike it, vote it down, otherwise ignore it, or maybe make a comment explaining why it is wrong. – Nick Gammon Apr 14 '18 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.