Help with trouble shooting

so I have been trying to make a simple hand wash timer but something is off

 int trigger=13;
 int echo=12;
 int pingtime;
 int t=10;
 int sec=2000;
 int led1=11;
 int led2=10;
 int led3=9;
 int led4=8;
 int led5=7;
 int led6=6;
 int led7=5;
 int led8=4;
 int led9=3;
 int led10=2;
 
void setup(){
 pinMode (trigger,OUTPUT);
 pinMode (echo,INPUT);
 Serial.begin (9600);
}

void loop(){
  //the code below this line sends a ping through the ultrasonic sensor
 digitalWrite (trigger,LOW);
 delayMicroseconds(t);
 digitalWrite(trigger,HIGH);
 delayMicroseconds(t);
 digitalWrite (trigger,LOW);
 //the code below this line calculates the distance
 pingtime=pulseIn(echo,HIGH);
 pingtime=((pingtime/2)/29);
 Serial.println(pingtime);
 Serial.print(" cm");
 
 if (pingtime=5){
 digitalWrite (led1,HIGH);
 delay (sec);
 digitalWrite (led2,HIGH);
 delay (sec);
 digitalWrite (led3,HIGH);
 delay (sec);
 digitalWrite (led4,HIGH);
 delay (sec);
 digitalWrite (led5,HIGH);
 delay (sec);
 digitalWrite (led6,HIGH);
 delay (sec);
 digitalWrite (led7,HIGH);
 delay (sec);
 digitalWrite (led8,HIGH);
 delay (sec);
 digitalWrite (led9,HIGH);
 delay (sec);
 digitalWrite (led10,HIGH);
 delay (sec);
 digitalWrite (led1,LOW);
 digitalWrite (led2,LOW);
 digitalWrite (led3,LOW);
 digitalWrite (led4,LOW);
 digitalWrite (led5,LOW);
 digitalWrite (led6,LOW);
 digitalWrite (led7,LOW);
 digitalWrite (led8,LOW);
 digitalWrite (led9,LOW);
 digitalWrite (led10 ,LOW);
 
 pingtime=pingtime;
 }
}

the if command block starts even though the pingtime is above 5

Oops.

sp. "const byte trigger = 13;" etc, etc.

(This isn't a bootloader issue)

uhmm i am kinda new to all this stuff so i have no clue which category this comes under

This should be "unsigned long"
(Though I suspect a number that large would indicate an out-of-range condition for the sensor)

pingtime=pingtime;

I don't know what that's for.

@lalit_creations, your topic has been moved to a more suitable location on the forum. Avrdude, stk500 or Bootloader issues is for problems related to uploading your compiled sketches; see About the Avrdude, stk500, Bootloader issues category.

When you have more than 2 or 3 names that are the same except for a trailing number, it's time to consider arrays and loops:

const byte  triggerPin = 13;
const byte  echoPin = 12;
const unsigned long t = 10;
const unsigned long sec = 2000;
const byte ledPins[10] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};

void setup()
{
  pinMode (triggerPin, OUTPUT);
  pinMode (echoPin, INPUT);
  Serial.begin (9600);

/*
[quote="TheMemberFormerlyKnownAsAWOL, post:7, topic:898057"]
make the LED pins outputs
[/quote]
*/
    for (int i = 0; i < 10; i++)
    {
      pinMode (ledPins[i], OUTPUT);
    }
}

void loop()
{
  //the code below this line sends a ping through the ultrasonic sensor
  digitalWrite (triggerPin, LOW);
  delayMicroseconds(t);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(t);
  digitalWrite (triggerPin, LOW);
  // the code below this line calculates the distance
  unsigned long pingtime = pulseIn(echoPin, HIGH, 30000UL);
  int distanceCM = pingtime / 58;  // Round-trip time for sound is 58.2 microseconds per cm
  Serial.println(distanceCM);
  Serial.print(" cm");

  // A distanceCM of zero means no echo was seen
  if (distanceCM != 0 && distanceCM <= 5)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPins[i], HIGH);
      delay (sec);
    }
    for (int i = 0; i < 10; i++)
    {
      digitalWrite (ledPins[i], LOW);
    }
  }
}
1 Like

It might be a good idea to make the LED pins outputs.

Good point! They will be much brighter that way. I'll edit the sketch above.

thx very much have tried it . it worked and also made the led pins output

uhhm i did not understand what this code does

John's code uses an array const byte ledPins[10] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2}; that provides a "list" of all the led pins.

The for-loop iterates through this "list" and for every value in that "list", it sets the pinMode to OUTPUT.

yeah understood . this makes it much more easier

this is a small doubt of mine. i wanted two chunks of code to run at the same time . i think that the ide executes the code line by line. so is there any way to run two chinks of code at a time?

Not really unless you use a multi-core processor :wink:

The usual approach is to break each chunk into small pieces, execute one piece of chunk1, execute one piece of chunk2, execute the next piece of chunk1, the next piece of chunk2 and so on. That way there is the perception that it runs in parallel.

You can study Demonstration code for several things at the same time. There are also other articles on the web, I think that Adafruit has one as well.

and one last thing can you suggest me a book or a website where i can learn the ide from

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.