Hi, I have the above collection of items and seem to be having some trouble getting them to all work together.
I can get the ping sensor working well and driving the motor.
I have one power supply for the arduino and another for the motor and levers (6 levers). The ping is connected to the 5v and ground on the board (the grounds of the 2 supplies are connected). The motor and levers run through a transistor circuit (tip120, diode and resistor for each item) to protect the arduino. Some of the levers seem to work, but others don't. When I plug an led to check the output from the arduino I see that some of the output pins put out a lot less power than others (the light is very faint), even if everything else is disconnected. Is it possible that the pins have different outputs? They are pins 4 through 9. Would that matter for the transistor circuit since it's just allowing the 12v to go through?
Any thoughts?
Thank you
int pingPin = 2; //distance sensor
int amotPin = 4; // string dampeners (4 to 9)
int bmotPin = 5;
int cmotPin = 6;
int dmotPin = 7;
int emotPin = 8;
int fmotPin = 9;
int motorPin = 11; // main motor
int motPins[] = {4,5,6,7,8,9,11};
int num_pins = 11;
int timer = 200;
void setup()
{
Serial.begin(9600);
int i;
for (int i = 0; i < num_pins; i++)
pinMode(motPins[4,5,6,7,8,9,11], OUTPUT);
}
void loop()
{
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm >= 3 && cm <= 33) //close range
{
closeRoutine();
}
else if (cm >= 34 && cm <= 84) // medium close range
{
medCloseRoutine();
}
else if (cm >= 85 && cm <= 140) // medium range
{
mediumRoutine();
}
else if (cm >= 141 && cm <= 326) //far range
{
farRoutine();
}
else
digitalWrite(motPins[4,5,6,7,8,9],LOW);
analogWrite(motorPin,0);
}
void closeRoutine()
{
Serial.println("hiya there");
analogWrite(motorPin,255);
digitalWrite(amotPin,HIGH);
delay(1000);
digitalWrite(amotPin,LOW);
digitalWrite(bmotPin,HIGH);
delay(1500);
digitalWrite(bmotPin,LOW);
digitalWrite(fmotPin,HIGH);
delay(1000);
digitalWrite(fmotPin,LOW);
digitalWrite(emotPin,HIGH);
delay(500);
digitalWrite(emotPin,LOW);
digitalWrite(bmotPin,HIGH);
delay(500);
digitalWrite(bmotPin,LOW);
}
void medCloseRoutine()
{
Serial.println("hey you");
analogWrite(motorPin,200);
digitalWrite(cmotPin,HIGH);
digitalWrite(dmotPin,HIGH);
delay(600);
digitalWrite(cmotPin,LOW);
digitalWrite(dmotPin,LOW);
delay(1500);
digitalWrite(dmotPin,HIGH);
digitalWrite(amotPin,HIGH);
delay(1000);
digitalWrite(dmotPin,LOW);
digitalWrite(amotPin,LOW);
delay(800);
digitalWrite(dmotPin,HIGH);
digitalWrite(amotPin,HIGH);
delay(800);
digitalWrite(dmotPin,LOW);
digitalWrite(amotPin,LOW);
delay(600);
digitalWrite(cmotPin,HIGH);
delay(1500);
digitalWrite(cmotPin,LOW);
}
void mediumRoutine()
{
Serial.println("yo");
analogWrite(motorPin,220);
digitalWrite(amotPin,HIGH);
digitalWrite(cmotPin,HIGH);
digitalWrite(dmotPin,HIGH);
digitalWrite(fmotPin,HIGH);
delay(2000);
digitalWrite(emotPin,HIGH);
delay(1500);
digitalWrite(cmotPin,LOW);
delay(50);
digitalWrite(fmotPin,LOW);
delay(200);
digitalWrite(dmotPin,LOW);
digitalWrite(amotPin,LOW);
digitalWrite(emotPin,LOW);
delay(2000);
digitalWrite(emotPin,HIGH);
delay(1000);
digitalWrite(bmotPin,HIGH);
delay(1300);
digitalWrite(emotPin,LOW);
digitalWrite(bmotPin,LOW);
}
void farRoutine()
{
Serial.println("here we goes");
analogWrite(motorPin,190);
digitalWrite(bmotPin,HIGH);
digitalWrite(cmotPin,HIGH);
digitalWrite(emotPin,HIGH);
digitalWrite(fmotPin,HIGH);
delay(800);
digitalWrite(emotPin,LOW);
delay(125);
digitalWrite(bmotPin,LOW);
digitalWrite(cmotPin,LOW);
digitalWrite(fmotPin,LOW);
delay(1800);
analogWrite(motorPin,210);
digitalWrite(dmotPin,HIGH);
delay(250);
digitalWrite(amotPin,HIGH);
digitalWrite(cmotPin,HIGH);
digitalWrite(emotPin,HIGH);
delay(500);
digitalWrite(emotPin,LOW);
delay(1000);
digitalWrite(bmotPin,HIGH);
digitalWrite(amotPin,LOW);
digitalWrite(cmotPin,LOW);
digitalWrite(dmotPin,LOW);
delay(200);
digitalWrite(bmotPin,LOW);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
thanks, I did have that on another version of the code, but I changed the assigned pins and didn't think it would work because the number of pins (7) was smaller than the number for the largest pin (11) and they weren't consecutive.
I appreciate the help figuring this out.
update: that did something! Thank you so much! I will test it out with the whole piece tomorrow.
you can see a video for the sculpture at www.thessiamachado.com/listenindex.html
the instrument is called 'pluck'
yes, I changed that too after I did some more reading on arrays. I was obviously very confused about that.
One more thing that needed to be done to get the circuit working reliably was to connect the rx pin to ground with a 10k resistor (I'm using an external power supply for the arduino).
Thanks for the help!