Go Down

Topic: 4 PING))) to activate a relay (Read 665 times) previous topic - next topic

Djed

Hello
I have commented out the parts of the sections of the code I can not figure out
I know what my problem is, I just don't have enough knowledge to see the solution.

I am trying to make a "ping" barrier whereby when breached (in this case 5in or less) it will activate a relay that is hooked up to a siren that will go off
The relay activates on LOW

problem, I need to collect the 4 sensors data simultaneously, however, I need the siren to sound continuously when a sensor reads less than 5 in.

Currently....I can't even get the relay to activate
The code is below, all help is welcome
I couldn't find similar projects who's code i could study for possible solutions.

Thank you


Code: [Select]
/*

*/

int ultraSoundSignalPins[] = {3, 4, 5, 6}; // Front Left (3),Front Right(4), Rear Left (5), & Rear Right(6) Ultrasound signal pins
char *pingString[] = {"Front Left ", "Front Right ", "Rear Left ", "Rear Right "}; // just something to print to indicate direction

//const int HORN = 8; // HORN RELAY
const int greenLEDs = 10;// Test led to see when the sensors would be tripped

//int HORNs[] = {HORN};

void setup()
{
  //digitalWrite(HORN, HIGH);

  Serial.begin(9600);
}

//Ping function
unsigned long ping(int index)

{
  unsigned long echo;

  pinMode(ultraSoundSignalPins[index], OUTPUT); // Switch signalpin to output
  digitalWrite(ultraSoundSignalPins[index], LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignalPins[index], HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignalPins[index], LOW); // Holdoff
  pinMode(ultraSoundSignalPins[index], INPUT); // Switch signalpin to input
  digitalWrite(ultraSoundSignalPins[index], HIGH); // Turn on pullup resistor
  echo = pulseIn(ultraSoundSignalPins[index], HIGH); //Listen for echo
  //return (echo / 58.138); //convert to CM
  return (echo / 58.138) * .39; //convert to CM then to inches

}

void loop()
{
  unsigned long ultrasoundValue;

  for (int i = 0; i < 4; i++) {
    ultrasoundValue = ping(i);
    Serial.print(pingString[i]);
    Serial.print(ultrasoundValue);
    //Serial.print("CM, ");
    Serial.print(" in, ");
    delay(50);

    // int LOUD = HORNs[i];

    if ( ultrasoundValue <= 5) {
      //digitalWrite(LOUD, LOW);
   
    }
    else {
     // digitalWrite(LOUD, HIGH);
 

    }
    if ( ultrasoundValue > 10) {
      digitalWrite(greenLEDs, HIGH);
    }
    else {
      digitalWrite(greenLEDs, LOW);
    }
  }
  Serial.println();
  delay(200);
}

codlink

After skimming the code, I did notice that you forgot this in setup:

Code: [Select]
pinMode(HORN, OUTPUT);
//LiNK

Djed

After skimming the code, I did notice that you forgot this in setup:

Code: [Select]
pinMode(HORN, OUTPUT);
I played with that

When I do that, It will only activate on the first entry [from the first PING)))]
Is there a way to have it utilize the reading from all 4?

Thank you, lol at least i'm one step closer

OldSteve

#3
Feb 05, 2016, 01:17 am Last Edit: Feb 05, 2016, 01:20 am by OldSteve
This compiles OK:-
Code: [Select]
/*

*/

int ultraSoundSignalPins[] = {3, 4, 5, 6}; // Front Left (3),Front Right(4), Rear Left (5), & Rear Right(6) Ultrasound signal pins
char *pingString[] = {"Front Left ", "Front Right ", "Rear Left ", "Rear Right "}; // just something to print to indicate direction

const int HORN = 8; // HORN RELAY
const int greenLEDs = 10;// Test led to see when the sensors would be tripped

//int HORNs[] = {HORN};

void setup()
{
    pinMode(HORN, OUTPUT);
    digitalWrite(HORN, HIGH);
    Serial.begin(9600);
}

//Ping function
unsigned long ping(int index)
{
    unsigned long echo;
    pinMode(ultraSoundSignalPins[index], OUTPUT); // Switch signalpin to output
    digitalWrite(ultraSoundSignalPins[index], LOW); // Send low pulse
    delayMicroseconds(2); // Wait for 2 microseconds
    digitalWrite(ultraSoundSignalPins[index], HIGH); // Send high pulse
    delayMicroseconds(5); // Wait for 5 microseconds
    digitalWrite(ultraSoundSignalPins[index], LOW); // Holdoff
    pinMode(ultraSoundSignalPins[index], INPUT); // Switch signalpin to input
    digitalWrite(ultraSoundSignalPins[index], HIGH); // Turn on pullup resistor
    echo = pulseIn(ultraSoundSignalPins[index], HIGH); //Listen for echo
    //return (echo / 58.138); //convert to CM
    return (echo / 58.138) * .39; //convert to CM then to inches

}

void loop()
{
    unsigned long ultrasoundValue;
    for (int i = 0; i < 4; i++)
    {
        ultrasoundValue = ping(i);
        Serial.print(pingString[i]);
        Serial.print(ultrasoundValue);
        //Serial.print("CM, ");
        Serial.print(" in, ");
        delay(50);

        // int LOUD = HORNs[i];

        if ( ultrasoundValue <= 5)
        {
            digitalWrite(HORN, LOW);
        }
        else
        {
            digitalWrite(HORN, HIGH);
        }
        
        if ( ultrasoundValue > 10)
        {
            digitalWrite(greenLEDs, HIGH);
        }
        else
        {
            digitalWrite(greenLEDs, LOW);
        }
    }
    Serial.println();
    delay(200);
}

There was more to my reply, but it disappeared?

The problem I see is that the horn relay will only stay activated if something is within range of all 4 sensors. If something is within range of only one, the relay will trip momentarily, then instantly turn back off when the next sensor is read if nothing is within 5 inches.
You need to rearrange the code so that the relay turns on if something is within 5 inches of one sensor, but only turns off if nothing is within range of any of them.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Djed

This compiles OK:-

Yea, I may have made a few errors (such as the deleting codlink's suggestion)
I have a lot of comments to remind myself of the process of the code in the one i am using, I erased that in order to clean it up.
It compiles fine, and the sensors read accurately.

OldSteve

Yea, I may have made a few errors (such as the deleting codlink's suggestion)
I have a lot of comments to remind myself of the process of the code in the one i am using, I erased that in order to clean it up.
It compiles fine, and the sensors read accurately.
Take another look at my reply. I just added a little more that somehow disappeared originally.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Djed

Take another look at my reply. I just added a little more that somehow disappeared originally.
Thanks for modifying the code.

I just plugged it in, and you're right.
it cycles intermittently (while it reads the other sensors, but the relay does come on now.


THANK YOU.

I will try and figure out how to keep it on while one is low and the others are not.

OldSteve

Thanks for modifying the code.

I just plugged it in, and you're right.
it cycles intermittently (while it reads the other sensors, but the relay does come on now.

THANK YOU.

I will try and figure out how to keep it on while one is low and the others are not.
I could have re-written it, but figured that's your job, and it will be a good learning exercise. :)

It's pretty easy, so I'm sure it will come to you. ;)
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Djed

I could have re-written it, but figured that's your job, and it will be a good learning exercise. :)

It's pretty easy, so I'm sure it will come to you. ;)
lol what would I need to read up on to understand my mistake?


OldSteve

lol what would I need to read up on to understand my mistake?
Nothing in particular.
Just read all 4 sensors and store or flag the results, then write an 'if' statement that takes appropriate action.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Djed

Nothing in particular.
Just read all 4 sensors and store or flag the results, then write an 'if' statement that takes appropriate action.
I got nothing useful.
The switch or "serial.available" options will only complicate the code, and i only seem to be able to manipulate the last ping.

hints would be welcome.
(I'm using the arduino cookbook for reference/google and the search feature here)

OldSteve

I got nothing useful.
The switch or "serial.available" options will only complicate the code, and i only seem to be able to manipulate the last ping.

hints would be welcome.
(I'm using the arduino cookbook for reference/google and the search feature here)
I'm flat out trying to write my own fairly complex code right now, and don't have time to put time into your's as well.

'Serial.available()' has nothing to do with it - it only relates to serial communications.

Read all sensors, store the results as flags, maybe something like:-
Code: [Select]
bool targetFound[4];

With each sensor, if range is less than 5 inches, 'targetFound[index] = true';
otherwise, set it to false.

Then after reading all 4 sensors:-

Code: [Select]
bool hornState = LOW;
for(int i = 0; i < 4; i++)
{
    if(targetFound[i] == true)
        hornState = HIGH;
}
digitalWrite(HORN, hornState);


I am preoccupied, and haven't thought this right through, but it should help get you started.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Djed

Many thanks my good sir.

I got it.

I was using the || and it got too complicated.
This was a lot of help. Thank you.

Go Up