View unanswered posts | View active topics It is currently Tue Oct 21, 2014 8:10 am






Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Help with simple programming 
Author Message
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Help with simple programming
Hi.
I was wondering if anyone on this board could give me a sample program to start with. We're using robot C and tetrix.
Me and my team have to do something like this:
Begin a loop with a sonar (ultrasonic) sensor spinning. Once it detects something (a wall), it moves forward. When it detects the wall again, it moves backwards until its touch sensor hits a wall.


I realize this must seem easy, but our teacher, told us to google answers if we had any problems.
I felt as if going to pro's would be a better route.


Wed Nov 30, 2011 6:08 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
This sounds like a homework question. I don't think that these forums are about "do this for me", but rather about "help me do this" (of course, that's just my opinion). Don't get me wrong, I'm not saying that you are wrong in asking your question. I'm just saying that you need to include "here's what I tried and here's where I got stuck" in your post. What code did you try? Did you try looking at sample programs?

_________________
sudo rm -rf /


Wed Nov 30, 2011 7:32 pm
Profile
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Re: Help with simple programming
You're absolutely right.
And its more of a project, rather than homework. I've been working on it for about a month now, and I'm really stuck.

I can't bring my robot outside of the classroom, therefore I don't have the exact programming in front of me (hence why I'd like a sample)
But I more or less get stuck at the programming of the sensors. I don't really know how to go about having everything all in one program. :/

EDIT: And yes, I've tried looking at a lot of samples, and I've been on these forums looking at similar issues I ran into.


Wed Nov 30, 2011 8:22 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
van2007 wrote:
And yes, I've tried looking at a lot of samples, and I've been on these forums looking at similar issues I ran into
Thanks, I'm sure that everybody appreciates that.
Now as to your question, what are you having problems with specifically? What particular part can you not get to work?

_________________
sudo rm -rf /


Wed Nov 30, 2011 8:28 pm
Profile
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Re: Help with simple programming
She makes us do everything in class, so I don't have the specific issues in front of me.

But, I can make it move, of course.
When it comes to the ultrasonic sensor, I suppose I don't know how to make it see something and then move.
Every sample program I tried gives me errors.

And I honestly don't even know where to begin with the sound Sensor. xD


Wed Nov 30, 2011 8:34 pm
Profile
Rookie

Joined: Fri Nov 04, 2011 10:56 pm
Posts: 3
Post Re: Help with simple programming
So your problem is finding out how to integrate sensors into your program. In the case of the Sonar/UltraSonic sensor... you will want to update a variable with the sensor's value constantly.Create the variable with "int sonar;", then the code might be "sonar = SensorValue(sonarSensor);", placed at the beginning of your main while(true) loop.Then later in the loop you can use the variable "sonar" to check how far away the wall is, using more ifs or whiles.

I hope that helped you understand what you need to do. :P
You may need to define the names and ports of your sensors in the Motor and Sensors window if you are getting errors from sample programs.


Wed Nov 30, 2011 8:50 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
This is directly from the sample program list, one called "Detecting Obstacles With Sonar":
Code:
task main()
{
  int distance_in_cm = 20;   // Create variable 'distance_in_cm' and initialize it to 20(cm).

   while(SensorValue[sonarSensor] > distance_in_cm)   // While the Sonar Sensor readings are less than the specified, 'distance_in_cm':
   {
      motor[motorB] = 75;                     // Motor B is run at a 75 power level
      motor[motorC] = 75;                     // Motor C is run at a 75 power level
   }
   motor[motorB] = 75;    // Motor B is stopped at a 0 power level
   motor[motorC] = 75;    // Motor C is stopped at a 0 power level
}

Those last two statements should be
Code:
   motor[motorB] = 0;    // Motor B is stopped at a 0 power level
   motor[motorC] = 0;    // Motor C is stopped at a 0 power level
, but I guess someone copied and pasted incorrectly.
so that demonstrates how to use the sonar sensor. calling SensorValue[sonarSensor] will give you the distance in cm of what is in front of it. You can tell the program to go something while it is a certain distance away, or until it gets to a certain distance...ect.

And what are you using your sound sensor for? It senses volume.

_________________
sudo rm -rf /


Wed Nov 30, 2011 8:53 pm
Profile
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Re: Help with simple programming
Haha. I barely understood what that meant; would probably be easier if I had my program with me :/

In any case. Is there anyway you can show me what you mean? You don't have to give me detailed programming, I'm just more of a visual learner.


Wed Nov 30, 2011 8:55 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
To whom are you asking. In times of ambiguity, it is a good idea to quote the poster to whom you are responding.

_________________
sudo rm -rf /


Wed Nov 30, 2011 9:00 pm
Profile
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Re: Help with simple programming
magicode wrote:
This is directly from the sample program list, one called "Detecting Obstacles With Sonar":
Code:
task main()
{
  int distance_in_cm = 20;   // Create variable 'distance_in_cm' and initialize it to 20(cm).

   while(SensorValue[sonarSensor] > distance_in_cm)   // While the Sonar Sensor readings are less than the specified, 'distance_in_cm':
   {
      motor[motorB] = 75;                     // Motor B is run at a 75 power level
      motor[motorC] = 75;                     // Motor C is run at a 75 power level
   }
   motor[motorB] = 75;    // Motor B is stopped at a 0 power level
   motor[motorC] = 75;    // Motor C is stopped at a 0 power level
}

Those last two statements should be
Code:
   motor[motorB] = 0;    // Motor B is stopped at a 0 power level
   motor[motorC] = 0;    // Motor C is stopped at a 0 power level
, but I guess someone copied and pasted incorrectly.
so that demonstrates how to use the sonar sensor. calling SensorValue[sonarSensor] will give you the distance in cm of what is in front of it. You can tell the program to go something while it is a certain distance away, or until it gets to a certain distance...ect.

And what are you using your sound sensor for? It senses volume.


I remember seeing this one.
It helped a bit.
But this also reminds me that sometimes since I'm using tetrix, and have to use motors D&E; RobotC will sometimes not let me use them.

In response to your question. I just need it to be able to stop when it hears a sound.


Wed Nov 30, 2011 9:02 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
To use Tetrix motors, you need to enable the Tetrix platform. Go to Robot -> Platform Type -> NXT+Tetrix (at least, that what I remember it being. I don't have ROBOTC for Mindstorms in front of me). Then you can add the extra motors in the Motors and Sensors setup menu.

As for the Sound sensor, here is the sample program for that:
Code:
task main()
{
   wait1Msec(1000);                        // Wait for 1 second to ignore initial readings of the Sound Sensor.

   while(SensorValue(soundSensor) < 70)    // While the Sound Sensor is less than 70 (quiet):
   {
      motor[motorC] = 75;                       // Motor C is run at a 75 power level.
      motor[motorB] = 75;                       // Motor B is run at a 75 power level.
   }

   motor[motorC] = 0;                      /* Otherwise, when loud noises are heard, Motor C */
   motor[motorB] = 0;                      /* and motor B stop.                              */

}

It does pretty much exactly what you described. Unless you want it to stop anytime you clap during the program, in which case it will be a little different.

_________________
sudo rm -rf /


Wed Nov 30, 2011 9:16 pm
Profile
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Re: Help with simple programming
Oh wow! Thanks for all your help.

Here's another issue I've run into.
How would I put them all in one program. Or would they all have to be separate programs?


Wed Nov 30, 2011 9:24 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
Ok, say that you wanted to go forward until you were 15 cm away from an object. Then back away until you were 30 cm from that object. Then you want the robot to keep going forward until you clapped. Here is how that would be done.
Code:
task main()
{
   while(SensorValue[sonarSensor] > 15)
   {
      motor[motorB] = 75;
      motor[motorC] = 75;
   }
   motor[motorB] = 0;
   motor[motorC] = 0;

   while(SensorValue[sonarSensor] < 30)
   {
      motor[motorB] = -75;
      motor[motorC] = -75;
   }
   motor[motorB] = 0;
   motor[motorC] = 0;

   while(SensorValue(soundSensor) < 70)
   {
      motor[motorC] = 75;
      motor[motorB] = 75;
   }
   motor[motorC] = 0;
   motor[motorB] = 0;
}

Does that help?

_________________
sudo rm -rf /


Wed Nov 30, 2011 9:33 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Help with simple programming
EDIT: Repeat Post.

_________________
sudo rm -rf /


Wed Nov 30, 2011 9:33 pm
Profile
Rookie

Joined: Wed Nov 30, 2011 6:00 pm
Posts: 13
Post Re: Help with simple programming
I think thats a pretty good starting point.
Again, thanks for all of your help.
I'm pretty sure I can figure it out from here.
And at the very least, I can ask more questions.

Wish me luck :)


Wed Nov 30, 2011 9:43 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 27 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.