Go Down

Topic: Shock sensor controlling LEDpins (Read 62 times) previous topic - next topic

Wu-Mokun-Tian

hello,guys.

I made a program to test shock sensor.




int ep = 13;
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;

void setup() {
  pinMode(ep,INPUT);
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  
  Serial.begin(9600);
  Serial.println("_____________Captain: FULL SAIL!!!__________________");
}

    long TP_init(){
      delay(10);
      long measurement = pulseIn(ep,HIGH);
      return measurement;
      }


void loop() {
  long measurement = TP_init();
  delay(200);
  Serial.print("measurement = ");
  Serial.println(measurement);
  Serial.println("________________________________");
 
    if (measurement == 0){
      digitalWrite(led1,HIGH);
      return;
      }
    
    if ((measurement > 1 ) && (measurement <= 100)){
      digitalWrite(led1,HIGH);
      delay(1000);
      //digitalWrite(led1,LOW);
      return;      
      }
    
    
    if((measurement >100) && (measurement <= 1000)){
      digitalWrite(led2,HIGH);
      delay(1000);
      //digitalWrite(led2,LOW);
      return;
      }
    
    if ( (measurement > 1000) && (measurement <= 6000) ){
      digitalWrite(led3,HIGH);
      delay(1000);
      //digitalWrite(led3,LOW);
      return;
      }
    
    if ( (measurement > 6000) && (measurement <= 10000) ){
      digitalWrite(led4,HIGH);
      delay(1000);
      //digitalWrite(led4,LOW);
      return;
      }
    
    if ( (measurement > 10000) && (measurement <= 20000) ){
      digitalWrite(led5,HIGH);
      delay(1000);
      //digitalWrite(led5,LOW);
      return;
      }
    
    if (measurement > 20000){
      digitalWrite(led6,HIGH);
      delay(1000);
      //digitalWrite(led6,LOW);
      return;
      }
}


the problem is

all the led lights is not on.

Riva

We do no understand what the LED lighting sequence needs to be depending on the length of the pulse so can you explain in detail. Does only one LED need to be lit at a time or are they lit in sequence (like a VU meter).

Your code does not turn LED's off so once they are lit they will never go out. Are they to remain lit forever or for a fixed short interval?

Your using return in every 'if' check that will probably stop the rest of the loop code from running as expected. You're also using a lot of delays in the code that will slow the reaction time considerably.
Don't PM me for help as I will ignore it.

Wu-Mokun-Tian

Thank you very much.

2 hours after this topic posted, I have solved that. Indeed, my code does not turn them off leading all the problems.

it's my fault which not explained why existing so many "delay" ,"return".

I want to make a electric drum. when the shock level changes, drum will making different noise or sound. It makes me reserve time to show them completely. this is delay's reason

As for return, I don't know. I am a totally rookie in Arduino & C++ for 3 months. "return" gives me the peace and security in programming.

Now, I do not know that, are there other ways to write code simply in this genre of project.

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!

Arduino
via Egeo 16
Torino, 10131
Italy