Go Down

Topic: How to create a string in c++ (Read 587 times) previous topic - next topic

vistaguru

im gonna make a robot with an ardurino usb board soon and i want it to go around my room and avoid obstacles, here's what it would kinda look like in visual basic

private sub me_load(...) Handles Me.load
Dim location As String
Location.String = "0"
If Location.String = "0" Then
power on motor1 and 2
then do that for x amount of time
then change location.string to "2" once everything has already been executed

sorry of my lack of knowledge but if you can help me then that would be great.



AlphaBeta

#1
Mar 12, 2009, 12:41 am Last Edit: Mar 12, 2009, 12:42 am by AlphaBeta Reason: 1
http://arduino.cc/en/Reference/String

I do not know VB but:

Quote

private sub me_load(...) Handles Me.load
Dim location As String
Location.String = "0"
If Location.String = "0" Then
power on motor1 and 2
then do that for x amount of time
then change location.string to "2" once everything has already been executed


could look something like:

Code: [Select]

char location[] = "0";
byte amountOfTimes = 2;

void powerMotor(byte motor); //TODO - implement function

void me_load(){
 for(byte i=0; i<amountOfTimes; i++){
   if(location[0]=='0'){
     powerMotor(1);
     powerMotor(2);
   }
 }
 location[0]='2';
}



It would be simpler to help you if you explained a bit more detailed what it is you want to accomplish though :)

mem

#2
Mar 12, 2009, 07:02 am Last Edit: Mar 12, 2009, 07:02 am by mem Reason: 1
if you just want a one character values as in the VB code you can just do:
Code: [Select]
char location = '0';
byte amountOfTimes = 2;

void powerMotor(byte motor); //TODO - implement function

void me_load(){
 for(byte i=0; i<amountOfTimes; i++){
   if(location == '0'){
     powerMotor(1);
     powerMotor(2);
   }
 }
 location='2';
}


I completly agree with AlphaBeta:
Quote
It would be simpler to help you if you explained a bit more detailed what it is you want to accomplish though

vistaguru

i figured it out, im not done yet, see if it powers on the motors on your arduino boards,  
Code: [Select]
int mfl = 13;                // front left motor connected to digital pin 13
int mfr = 10;                // front right motor connected to digital pin 10

void setup()                    // run once, when the sketch starts
{
 pinMode(mfl, OUTPUT);      // sets the digital pin as output
 pinMode(mfr, OUTPUT);
}


 boolean L1 = true;

   void loop()                     // run over and over again
{

   if(L1 == true){
     digitalWrite(mfl, HIGH);   // sets the front left motor on
     digitalWrite(mfr, HIGH); // sets the front right motor on
    delay(10000);                  // waits for 10 seconds
   }

}

AlphaBeta

You code sets pin 13 and 10 HIGH.


For more information about arduino and motors: http://itp.nyu.edu/physcomp/Labs/DCMotorControl

Go Up