Go Down

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



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(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  Serial.println("_____________Captain: FULL SAIL!!!__________________");

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

void loop() {
  long measurement = TP_init();
  Serial.print("measurement = ");
    if (measurement == 0){
    if ((measurement > 1 ) && (measurement <= 100)){
    if((measurement >100) && (measurement <= 1000)){
    if ( (measurement > 1000) && (measurement <= 6000) ){
    if ( (measurement > 6000) && (measurement <= 10000) ){
    if ( (measurement > 10000) && (measurement <= 20000) ){
    if (measurement > 20000){

the problem is

all the led lights is not on.


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.


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