I don't understand how to put 2 codes together...

Hello,

So I'm fairly new to this Arudino programming and I feel like my professor hasn't explained to us properly in terms of how to put various codes together.

I'm trying to put a sensor code (my sensor is a distance sensor) and sweep or knob? code. And then after that I was told that I need to incorporate the smoothing code as well.

I have three mini-servos that I want it to work by the trigger of the distance sensor. I'm so confused!!

// Controlling a servo using a distance sensor
// by Michal Rinott ,http:/people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int distancesensor = 0; // analog pin used to connect the distance sensor
int sensorValue;  // variable to read the value from the analog pin

void setup()
{
myservo. attach(8); // attaches the servo on pin 9 to the servo object
}

void loop()
{
//this part i added
while(sensorValue >330)
myservo.write(0);
delay(1);
myservo.write(180);
delay(1);
//val = analogRead(distancesensor);      //reads the value of the distance sensor (value between 0 + 1023)
// myservo.write(val);    // sets the servo position according to the scaled value
//delay(1);    //waits for the servo to get reach the position
}

the code above is what i was trying to make my thing work with but it didnt work. the sensor was running super slow. i am sitting here waving my yellow flag here. somebody please help!

sincerely,
joanne

Hi,

I did not exactly figured out what you want to do, I think you want to read value from a distance sensor and then map it to 0 to 180, and then set the servo position to this value. When the value read (non-mapped) is below 330, you want the servo to change its position to 0, then to 180.

Is this right?

So I worked a bit on your code (I'm just a beginner but maybe this can help you).

 // Controlling a servo using a distance sensor
 // by Michal Rinott ,http:/people.interaction-ivrea.it/m.rinott>
 
 #include <Servo.h>
 
 Servo myservo;  // create servo object to control a servo
 
 int distancesensor = [glow]A0[/glow]; // analog pin used to connect the distance sensor
 int sensorval;  // variable to read the value from the analog pin
 int val;
 
 void setup()
 {
 myservo. attach(8); // attaches the servo on pin 9 to the servo object
 }
 
 void loop()
 {
 sensorval = analogRead(distancesensor);      //reads the value of the distance sensor (value between 0 + 1023)
 [glow]val = map(sensorval, 0, 1023, 0, 180);[/glow]
 
 while(sensorval >330)
 myservo.write(0);
 delay(1);
 myservo.write(180);
 delay(1);
 
 myservo.write(val);    // sets the servo position according to the scaled value
 delay(1);    //waits for the servo to get reach the position
 }

So take a look at it, I did not got well what you wanted to do but I just changed a few things.

  1. I think you need to write A0 if you want to read from analog pin 0
  2. I declared "val"
  3. "val" is know the mapped value of "sensorval"
  4. you should change the delay time (I don't think 1 milisec. is enough).

So maybe this will work!

Good luck!

Your problem may be bigger than a class assignment. A kind soul Binette228

did not exactly figured out what you want to do

bkoz you did not clearly state it, perhaps you yourself did not figgured it out either. Its hard to give someone directions when they dont know where they want to go.

First tell us WHAT YOU WANT ie "Need a servo position to be proportional to distance" or else "need to wag a servo when distance is greater than 330". Those are pretty clear and many folks here will help you solve a specific problem.

If you cant formulate a clear question then you are really asking "I dont understand what programming is, will someone do my homework for me?" and you may not get so much help here.

Programming is more than "how to put 2 codes together", it is a way of thinking about the world and problem solving.
(1) understand the problem and create a solution
(2) break the solution into parts a machine could do
(3) code the parts until they work
(4) put the parts together until everything works
If professor gives you working code parts for #3 and you cant do #4, we begin to suspect you are not clear on #1 or #2.

Study the two codes you were given, until you really understand them. Not just the comments, but the actual code. REALLY understand how someone programmed a machine do that. Push yourself. Use the Arduino tutorials and the simple examples that come with the IDE. Have someone take out the comments in a simple code example and see if you can figure out what the code does without comments or any other help. This is called "reverse engineering" and being able to do this even with a simple code shows that you "get it". Ask help from professor or a classmate who really "gets it", or if youre too far back to catch up this class maybe drop the class and retake it later from the start.

This is hard advice, and not what you came here seeking, but dont get too discouraged. Programming is a rather odd way for humans to think; some people have an aptitude for it, but most have to struggle before their brain begins to click that way. Many give up and change majors.

Almost gave up myself, in Differential Equations II, I just didnt "get it". It was the low point of my education, I made friends with folks good at math, studied with others, tried different professors. On the 4th and final try I passed the course. An engineer needs more than intermediate differential equations but funny thing is the farther I went, thru vector calculus, fields, etc. the easier it got. Now I help design the most advanced jets in the world and other engineers seek my advice.

Marathon runners talk about "hitting the wall" over and over again but you dont give up until one day you break through and get your "second wind". It happened to me in math and it can happen to you in programming.

Binette228 - Thanks for trying I'll try your code but I think there are some parts that might not be suitable for me so I may need to change it a bit but it might be a great starting point so thank you.

AltairLabs - I completely understand what you have explained to me. My professor has told me too that I need to ask her more concrete questions. I feel so silly! Now I've been told twice and I haven't fixed my problem. I think I'm at a state where I'm still trying to understand all the new terminology and then trying to use those words to construct a question has been a new challenge and I'm unable to communicate. I'm going to go home and try reading the codes over and over again. But it would be really cool if I had access to an arduino software that not only highlights where the problem is (once you click 'upload') but a little pop up box that comes up to tell you why that code doesn't work there and give you options on what you want to do. that would make my life SO much easier and I would actually understand what kind of mistakes I've been making. Anyway I'm going to work on my code now. If it still doesn't work perhaps then I will be able to construct a clearer question to ask you guys. Thanks for your thoughts.

But it would be really cool if I had access to an arduino software that not only highlights where the problem is (once you click 'upload') but a little pop up box that comes up to tell you why that code doesn't work there and give you options on what you want to do. that would make my life SO much easier and I would actually understand what kind of mistakes I've been making.

It would be even better if you just said in plain English and in a sentence and the compiler programmed everything for... If that were the case you wouldn't understand the errors you've been making because you were only interested in getting it to work no matter what.

Anyway I'm going to work on my code now. If it still doesn't work perhaps then I will be able to construct a clearer question to ask you guys. Thanks for your thoughts.

That's not the way either...
So far I gather that you need to read a sensor. Try getting that done by yourself...
Then you need to control a servo... try doing that without any of the sensor part of the software. do that by reading the state of a pin, or by receiving commands from the serial port.

You need to break down the big problem you have in very small problems and then connect them together to fix the big problem. You normally connect them using variables of the same type.

reading sensor gives a number.
controlling the servo takes a number.

But you need to do it yourself and not read through and trying to figure what it's doing. Experiment!!!!

Ok I'm starting to feel really retarded. I must have missed out on something or my professor didn't go over this very well with us (well she only explained this section for under 10 minutes) I don't know.

But here's something new I tried and automatically the first line was highlighted. :-[

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.
// Code edited by JJ to incorporate distance sensor as an on/off switch for the mini-servos. January 12th, 2010.

#include <Servo.h>

Servo myservo = 7 // create servo object to control a servo
Servo myservo = 8 // a maximum of eight servo objects can be created
Servo myservo = 9 // I'm using 3 mini-servos and I want to connect them at 7, 8, 9

int pos = 0; // variable to store the servo position

int distanceSensor = 0; // analog pin used to connect the distance sensor
int sensorVal; // variable to read the value from the analog pin/ sensor

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object

myservo.attach(8); // <-- are the following 2 right???
myservo.attach(7);
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

oh wait. nevermind i think i got it.

Eureka? :smiley:

actually i didn't. when i clicked verify it didn't highlight anything so i was like "ahh! its finally working" but when i uploaded it on to my arduino it didnt do anything :frowning:

ok can anybody answer me this. i decided to give up trying to incorporate the sensor code into the sweep code (because I just dont get it) so I decided to just use the sweep code but have more than one mini servo motor on it. in the void setup I just declared 2 servos attached to pin 8 and 9. i didnt think i have to change void loop so i just kept it that way.

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.
// little bit revised by jj to add on extra servo jan 12 2011

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(8); // attaches the servo on pin 8 to the servo object
myservo.attach(9); // attaching an extra servo on to pin 9
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
}

i uploaded this code and only one of my servo works. what do i have to do to get the other servo working???

what do i have to do to get the other servo working???

Look at the code. How many servo instances have you created? One.

How many servos move? One. Coincidence? No.

You need to declare another servo instance, and attach the other servo to the other pin.

Servo myOtherServo;

myOtherServo.attach(9);

Then. in loop, you need to tell both instances to move.

Glad you are hangin in there JJ87 keep at it and you will become a programmer !

But it would be really cool if I had access to an arduino software that not only highlights where the problem is (once you click 'upload') but a little pop up box that comes up to tell you why that code doesn't work there and give you options on what you want to do. that would make my life SO much easier and I would actually understand what kind of mistakes I've been making.

Too funny ! If that ever comes true programmers everywhere will go back to work at Burger King.

when i clicked verify it didn't highlight anything so i was like "ahh! its finally working" but when i uploaded it on to my arduino it didnt do anything

Verify "highlights where the problem is". When there was no problem with your program, then Arduino did exactly what you TOLD it to do but not what you WANTED it to do. This is the very essence of programming! How to TELL a machine what you WANT.

Your code is looking better, when you get what PaulS has given you I think you will have success in your sweep program. Good luck !

oh yay PaulS you've totally helped me before in the past. I think with my super last minute 2nd project? Anyway I'm glad you spotted me waving my yellow flag on here for help.

Ok so this is what I'm doing?

// Sweep
// by BARRAGAN http://barraganstudio.com;
// This example code is in the public domain.
// little bit revised by jj to add on extra servo jan 12 2011

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

Servo myOtherservo; // so this is for the second servo

void setup()
{
myservo.attach(8); // attaches the servo on pin 8 to the servo object
myOtherservo.attach(9); // attaching an extra servo on to pin 9
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myOtherservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
}

Is that correct? I think it's going to work. Yay yay yay I should get out of bed to test it.

AltairLabs - Thanks for the support. I laughed pretty hard at the Burger King comment.

So what did it do?

Was that what you WANTED ?

Was that what you TOLD it to do ?

Its pretty much an axiom that the 3rd answer is YES :wink:

You have a lot of "for" loop in there

see http://arduino.cc/en/Reference/For

You still havent said clearly what you WANT to happen, but I've highlighted your FOR loops below according to what I imagine your intent must be. Take a look and see if anything looks wrong.....

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myOtherservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
}

After the pink for you start a blue for which looks like you wanted a for loop inside another for loop, we call those "nested loops". If thats what you wanted it needs some curly braces.

But its more likely you just lost some lines of your pink for

Actually I got pretty distracted after my last reply so I haven't been able to touch my arduino project. i had to show my prof what i have done so far and yeah, she likes the construction (im a fibre/ textiles major) but mechanical part definitely needs work - lol. there's a place called hacklab in toronto that im going to tomorrow. there's a free workshop there. i'll take a better look at what you sent me before i go to the workshop tomorrow. thanks a bunch AltairLabs! :slight_smile: