system
March 17, 2012, 3:27pm
1
hello i am a newbie here
i am having a hard time on how could i combine this two program codes i wrote
here's my code
First
[code][code]const int trigger=5;
const int echo=6;
const int motorPin=7;
long int duration, distanceInches, distanceCm;
int LimitCm = 100;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);
distanceInches=microsecondsToInches(duration);
distanceCm=microsecondsToCentimeters(duration);
checkLimit();
delay (100);
}
void checkLimit()
{
if(distanceCm < LimitCm)
{
digitalWrite(motorPin,HIGH);
}
else
{
digitalWrite(motorPin,LOW);
}
}
long microsecondsToInches (long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}
[/code][/code]
Second
const int trigger=5;
const int echo=6;
const int LedPin=7;
const int speaker=8;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
int LimitCm = 100;
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(LedAlert, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop()
{
digitalWrite(LedPin, HIGH);
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);
distanceInches=microsecondsToInches(duration);
distanceCm=microsecondsToCentimeters(duration);
checkLimit();
delay (100);
}
void checkLimit()
{
if(distanceCm < LimitCm)
{
digitalWrite(LedAlert,HIGH);
delayMicroseconds(2);
digitalWrite(speaker,HIGH);
delayMicroseconds(5);
}
else
{
digitalWrite(LedAlert,LOW);
delayMicroseconds(2);
digitalWrite(speaker,LOW);
delayMicroseconds(5);
}
}
long microsecondsToInches (long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}
system
March 17, 2012, 3:31pm
2
I want to use two ultrasonic sensor
the first code uses vibrating motor as an alarm
while the second sensor uses a two LED and a buzzer/speaker a an alarm too..
this two codes works fine when i tested it with my sensor
but when i combined it in the loop and modifying the codes and pin designations it does not work fine..
SO i am looking for a help or tutorial on how could i make this two code be in one
thanks for those who would help..
system
March 17, 2012, 3:39pm
3
Go back to the first post, click on "modify", highlight the first code block and click on the # icon on the editor's toolbar.
Repeat for the second code block, then click on "save"
system
March 17, 2012, 3:43pm
4
sir could you please help me with this code..
i'll post my worked code combining the two..
system
March 17, 2012, 3:52pm
5
const int trigger=5;
const int echo=6;
const int trig=8;
const int ech=9;
const int motorPin=7;
const int LedPin=3;
const int speaker=2;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
long int distance, distanceInch, distanceCms;
int LimitCm = 100;
int LimitCms = 100;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(LedPin, OUTPUT);
pinMode(LedAlert, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop()
{
{
digitalWrite(LedPin, HIGH);
}
{
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);
}
{
pinMode (trig, OUTPUT);
pinMode (ech, INPUT);
digitalWrite(trig, LOW);
delayMicroseconds (2);
digitalWrite(trig,HIGH);
delayMicroseconds(5);
digitalWrite(trig,LOW);
}
pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);
pinMode(ech,INPUT);
distance=pulseIn(ech,HIGH);
distanceInches=microsecondsToInches(duration);
distanceCm=microsecondsToCentimeters(duration);
distanceInch=microsecondsToInches(distance);
distanceCms=microsecondsToCentimeters(distance);
checkLimit();
checkLimits();
delay (100);
}
void checkLimit()
{
if(distanceCm < LimitCm)
{
digitalWrite(motorPin,HIGH);
}
else
{
digitalWrite(motorPin,LOW);
}
}
void checkLimits()
{
if(distanceCms < LimitCms)
{
digitalWrite(LedAlert,HIGH);
delayMicroseconds(2);
digitalWrite(speaker,HIGH);
delayMicroseconds(5);
}
else
{
digitalWrite(LedAlert,LOW);
delayMicroseconds(2);
digitalWrite(speaker,LOW);
delayMicroseconds(5);
}
}
long microsecondsToInches (long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}
the two led lights and the vibrating motor vibrates continuously..
that's my problem and my speaker doesn't work
hoping that you could help me sir/mam
thanks in advance
system
March 17, 2012, 4:14pm
7
AWOL:
Please read reply #2
sorry sir for the mistake i made..
and i didn't know if i am in the correct thread to ask questions regrading with my project..
but hopefully you could help me getting rid of my problem
thanks in advance
system
March 17, 2012, 4:17pm
8
-Ok, factor the code for the Ping sensors into a single function, taking the pin number as a parameter.
This comes up on the forum every other week, so it shouldn't be too difficult to find.
Once you start reducing the volume of code, it is easier to see the stuff that really matters.
system
March 17, 2012, 4:24pm
9
AWOL:
-Ok, factor the code for the Ping sensors into a single function.
This comes up on the forum every other week, so it shouldn't be too difficult to find.
Once you start reducing the volume of code, it is easier to see the stuff that really matters.
thanks sir..but i still have questions in my mind
i am just new with arduino and have a little background in c programming
i wish someone could make a sample code for me or a full instruction on how to do this because i really need this for my final requirement=(
again thanks a lot sir!!
i'll post questions and problems when i make my code..
system
March 17, 2012, 4:34pm
10
pinMode (trigger, OUTPUT); // move to setup ()
pinMode (echo, INPUT); // move to setup ()
unsigned long getRange (const int triggerPin, const int echoPin)
{
digitalWrite(triggerPin, LOW);
delayMicroseconds (2);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin,LOW);
return pulseIn(echoPin,HIGH);
}
system
March 17, 2012, 4:44pm
11
thanks sir..
did i also need to put the other codes into set up..
the one that has no statements //
or i should leaved it with the loop()
system
March 17, 2012, 4:50pm
12
Why don't you try stuff out, and come back when you've got specific questions?
system
March 17, 2012, 6:05pm
13
sir thanks for the idea and after some searching in the internet
my problem was now solved..
here's my final sketch
const int trigger=5;
const int echo=6;
const int trig=8;
const int ech=9;
const int motorPin=7;
const int LedPin=3;
const int speaker=2;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
long int distance, distanceInch, distanceCms;
int LimitCm = 100;
int LimitCms = 100;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(LedPin, OUTPUT);
pinMode(LedAlert, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop()
{
{
digitalWrite(LedPin, HIGH);
}
{
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);
distanceInches=microsecondsToInches(duration);
distanceCm=microsecondsToCentimeters(duration);
checkLimit();
delay (100);
}
{
pinMode (trig, OUTPUT);
pinMode (ech, INPUT);
digitalWrite(trig, LOW);
delayMicroseconds (2);
digitalWrite(trig,HIGH);
delayMicroseconds(5);
digitalWrite(trig,LOW);
pinMode(ech,INPUT);
distance=pulseIn(ech,HIGH);
distanceInch=microsecondsToInches(distance);
distanceCms=microsecondsToCentimeters(distance);
checkLimits();
delay (100);
}
}
void checkLimit()
{
if(distanceCm < LimitCm)
{
digitalWrite(motorPin,HIGH);
}
else
{
digitalWrite(motorPin,LOW);
}
}
void checkLimits()
{
if(distanceCms < LimitCms)
{
digitalWrite(LedAlert,HIGH);
delayMicroseconds(2);
digitalWrite(speaker,HIGH);
delayMicroseconds(5);
}
else
{
digitalWrite(LedAlert,LOW);
delayMicroseconds(2);
digitalWrite(speaker,LOW);
delayMicroseconds(5);
}
}
long microsecondsToInches (long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}
system
March 17, 2012, 6:07pm
14
{
digitalWrite(LedPin, HIGH);
}
What are these { and } for?
How does this:
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
digitalWrite(trigger, LOW);
delayMicroseconds (2);
digitalWrite(trigger,HIGH);
delayMicroseconds(5);
digitalWrite(trigger,LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo,HIGH);
differ from:
pinMode (trig, OUTPUT);
pinMode (ech, INPUT);
digitalWrite(trig, LOW);
delayMicroseconds (2);
digitalWrite(trig,HIGH);
delayMicroseconds(5);
digitalWrite(trig,LOW);
pinMode(ech,INPUT);
distance=pulseIn(ech,HIGH);
You were advised to create a function. You ignored that advice. Why?