I need a bit of coding help!!

Hi All,
Well I've had my Uno for about 4 days and been leaning to program it about the same, so I'm very new, previous to this I used the Picaxe, with it's poor basic.. My code partly works in that it drives 2 DC motors forward, or reverse, turn etc. I am using an L293 to drive the motors and the Enable input as a PWM pin, but this is the bit I can't get to work, I may have it in the wrong place?? or somthing, perhaps I've just got the ide wrong. If I can insert my code right, here it is..

/* *******************************
;    Filename: 	  PololuDrive	
;    Date: 	   10/08/2013		
;    File Version: 	
;    Written by:   Mel Saunders		
;    Function:	  BuggyDrive	
;    Last Revision:
;    Target       Uno
; ******************************* 

This program demonstrates the use of a L293 dual H bridge.
This can control 2x DC motors  independantly.
*/

int MA1=2,MA2=3,MB1=4,MB2=5;  //Motor pins
int PWMpin=9;    //Enable pin to L293 for PWM

void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
   
}

void loop()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);
}
void speed()
{
   for (int i=0; i <= 225; i++)
   analogWrite(PWMpin,i);    //control motor speed with PWMpin
   delay(100);
 }
   
                         [/co 

The speed part does not work?? Sorry but I can see why?? 

Any help or advice very welcome.
Regards
Mel.

Can you define "can't get to work" ?

In what way?

You need to set PWMpin to output.

Hi,
I have now altered my code, but still I'm not getting what I expect!!

@Nick the code works as far as it drives the motors forward, but I expected void speed to give me a PWM signal from pin 9 to the Enable(s) pin on the L293 to start the motors off slowly and increase with the var i, but it dos'ent.

@Marco_c As you can see I have now set PWMpin to output, but still no joy!! I'm sure it's something quite simple that I've misted or don't understand..

Take a look at my altered code..

/* *******************************
;    Filename: 	  PololuDrive	
;    Date: 	   10/08/2013		
;    File Version: 	
;    Written by:   Mel Saunders		
;    Function:	  BuggyDrive	
;    Last Revision:
;    Target       Uno
; ******************************* 

This program demonstrates the use of a L293 dual H bridge.
This can control 2x DC motor and independantly control them.
*/

int MA1=2,MA2=3,MB1=4,MB2=5;  //Motor pins
int PWMpin=9;    //Enable pin on L293 for PWM   

void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
  pinMode(PWMpin, OUTPUT);   // set PWMpin for output  ADDED AS SUGGESTED ??
}

void loop()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);
}
void speed()
{
   for (int i=0; i <= 225; i++)    //THIS IS THE BIT THAT DON'T SEEM TO WORK, I THOUGHT IT WOULD START THE MOTORS SLOWLY AND INCREASE SPEED WITH i INCREASE?? 
   analogWrite(PWMpin,i);
   delay(10);
 }

Again any help would be welcome.

Regards
Mel.

You do not need to specifically set a pin to output to use analogWrite.

Your problem is that at no point are you actually calling the function speed(). A function will only run when you actually call it. Add

    speed();

inside your loop() function.

Also, if you don't provide braces { } around the code you want the for loop to control then it only loops the next line:

   for (int i=0; i <= 225; i++)    //THIS IS THE BIT THAT DON'T SEEM TO WORK, I THOUGHT IT WOULD START THE MOTORS SLOWLY AND INCREASE SPEED WITH i INCREASE?? 
   analogWrite(PWMpin,i);
   delay(10);

is equivalent to:

   for (int i=0; i <= 225; i++)
   {
      analogWrite(PWMpin,i);
   }
   delay(10);

What you want is:

   for (int i=0; i <= 225; i++)
   {
      analogWrite(PWMpin,i);
      delay(10);
   }

but I expected void speed to give me a PWM signal

I've got to write

#define warp void
...
warp speed (int setSpeed)..

:stuck_out_tongue_closed_eyes:

AWOL: groan... It makes me want to add:

#define EVER ;;

// a bunch of code...

   for (EVER) {
      // stuff
   }

Cactusface: As majenko pointed out, any time you want an expression to control more than a single statement, you must add opening and closing curly braces. Otherwise, only a single statement is controlled by the expression. This applies to many C expressions (e.g., if, while, do-while, else, etc.). Take some time to study the examples provided in the Arduino IDE and you'll quickly get a feel for how they should be used. Also, delay() is a frowned-upon method for pausing a program. Take a look at Blink Without Delay for a better way.

econjack:
AWOL: groan... It makes me want to add:

#define EVER ;;

// a bunch of code...

for (EVER) {
      // stuff
   }




Cactusface: As majenko pointed out, any time you want an expression to control more than a single statement, you must add opening and closing curly braces. Otherwise, only a single statement is controlled by the expression. This applies to many C expressions (e.g., *if, while, do-while, else*, etc.). Take some time to study the examples provided in the Arduino IDE and you'll quickly get a feel for how they should be used. Also, *delay()* is a frowned-upon method for pausing a program. Take a look at Blink Without Delay for a better way.
#define TIME_EXISTS 1

while (TIME_EXISTS) {
  // ...
}

Hi Chaps,
Many thanks for your help, I think I might just be getting the hang of how C programs flow!! Remember I've been spoiled with Gosub, goto etc and it's not really good pratice! I know. So the main loop() runs all the time.

I have rewritten my code and it now works, in a basic way, there's going to be tons to add yet!!! and I am going to need more help. Like now in my loop() I start with startup() but I only want this to run once at the start, so after it as run can I say go=false where go is boolean and change my code to this but it keeps thowing up compile errors, that I don't understand.

/* *******************************
;    Filename: 	   PololuDrive	
;    Date: 	   10/08/2013		
;    File Version: 	
;    Written by:   Mel Saunders		
;    Function:	   BuggyDrive	
;    Last Revision:
;    Target        Uno
; ******************************* 

This program demonstrates the use of an L293 dual H bridge.
This can run 2x DC motors and independantly control their speed and direction.
*/

#include <Servo.h>

const int MA1=2,MA2=3,MB1=4,MB2=5;  //Motor pins
const int PWMpin=9, scanner=10;    //Enable pin on L293 for PWM //Servo pin=10
const int Left=0, Centre=90, Right=180;  //Servo angle in Degrees
Servo scanservo; 
boolean go; 

void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
  //scanservo.attach(scanner);  // Attach the scan serv
  go = true;
}
void loop()
(
 
 if go = true
( startup();
}else
  Rturn();
  Halt();
  back();
  Lturn();
 
}

//***************Functions++++++++++++++++++++++++

  void startup()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);

  for (int i=30; i <= 255; i++) //start slow, increase speed
 { 
   analogWrite(PWMpin,i);
   delay(50);
}
{
    go = !go;
}
}



 void Halt()
{
  digitalWrite(MA1,LOW);        //Both motors STOP     
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(2000);
}
void Rturn()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);       //Right motor reverse 
  digitalWrite(MB2,HIGH);
  delay(1000); 
} 
void Lturn()
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,HIGH);       //Right motor forward
  digitalWrite(MB2,LOW);
  delay(1000); 
} 

void back()
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,LOW);      //Right motor reverse
  digitalWrite(MB2,HIGH);
  delay(2000);
 }

Again any help most welcome.
Regards
Mel.

warp speed - that's pretty funny :smiley:

two things
need () and == for a test condition, thus:
if (go == true)

if you want to run once, simple way is to just put it at the end of void setup.
Otherwise, your idea to set a flag is valid as well.

Actually:

void loop()
{
 
 if (go == true){
   startup();
   }
else{
  Rturn();
  Halt();
  back();
  Lturn();
  }
} // end void loop

I'd go with this in startup, but !go might work too:
go = false;

Can you see the format better now?

Less typing (and I have formatted the braces the way I prefer), but maybe not so obvious what it means

if (go)
  {
    startup();
  }

Hi All,
And thanks to Crossroads, yes your fix did the trick and things are beginning to work, but I soon got stuck with my next problem and I can't see why!! Like many bots mine will have a servo and US/SRF005 on the front! Actually it's already built it my last Picaxe project, which was'ent too bad !! So I've had a go at getting the servo to work but am having a problem with the line scanservo.attach(scanservopin); Please see code for comments and problem.

Also looking it seems some say connect servos to pins 9/10 others say A0.... etc. My servos are the cheap chinese type?

Next problem can I add the function void scan() to the { startup} braces to also run once only, It don't seem to work, scan runs but not startup.

sure that's enough for now..

Mel.

/* *******************************
;    Filename: 	   PololuDrive	
;    Date: 	   10/08/2013		
;    File Version: 	
;    Written by:   Mel Saunders		
;    Function:	   BuggyDrive	
;    Last Revision:
;    Target        Uno
; ******************************* 

This program demonstrates the use of an L293 dual H bridge.
This can run 2x DC motors and independantly control their speed and direction.
*/

#include <Servo.h>

Servo scanservo;     //Ping Sensor Servo
const int MA1=2,MA2=3,MB1=4,MB2=5;  //Motor pins
const int PWMpin=9;  //Enable pin on L293 for PWM 
const int Left=0, Centre=90, Right=180;  //Servo angle in Degrees
const int scanservopin = 15;   // Pin number for scan servo 
boolean go; 

void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
  pinMode(scanservopin, OUTPUT);
  go = true;
 // scanservo.attach(scanservopin);  // Attach the scan servo  <<<<<<<<<<<<<< This line is the problem, when commented out things go as they should, atartup runs, then the items after else.
                                                               <<<<<<<<<<<<< But remove the // and startup is missed, after a long pause items after else run.
} 

void loop()
{
 if (go == true)
 {
   startup();
   }
else{
  Rturn();
  Halt();
  back();
  Lturn();
  }
} // end void loop


//***************Functions++++++++++++++++++++++++
  void startup()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);

  for (int i=20; i <= 255; i++) //start slow, increase speed
 { 
   analogWrite(PWMpin,i);
   delay(50);
}
   go = false;      //Set go to false
}

 void Halt()      //Both motors STOP  
{
  digitalWrite(MA1,LOW);           
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(2000);
}
void Rturn()      //Turn right
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);       //Right motor reverse 
  digitalWrite(MB2,HIGH);
  delay(1000); 
} 
void Lturn()      //Turn left
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,HIGH);       //Right motor forward
  digitalWrite(MB2,LOW);
  delay(1000); 
} 
void back()      //Reverse
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,LOW);      //Right motor reverse
  digitalWrite(MB2,HIGH);
  delay(2000);
 }
 
void scan()
{
    for (int i=Left; i <= Right; i++) 
{
      scanservo.write(i); 
      delay(20);
}
      scanservo.write(Centre); 
}

That'll be the antiquated Copy for Forum facility in the IDE... Something that UECIDE fixes :wink:

Hi,
Yes that did look a bit of a mess, but I just right clicked on the code to copy. Not really the way to do it, it would seem... So re done and now you can read it!!

Regards

Mel.

Read the documentation for the servo library. Using it prevents PWM from working on pins 9 and 10 because they share the same timer, which is why attaching your servo object prevents the startup function from working. Use a different pin for the motor speed control if you can or consider using the ServoTimer2 library.

Hi Bob?
Yes I did see something about using servos on certain pins, had an idea it must be something like a timer conflict, but I know very little about the Uno or the Maga328 timers?? So I'm learing all the time, once again you chaps have got my out of another jam!! I've moved the PWM to pin 11. So the next thing is getting the SRF005 U/S to look ahead at objects and read the distance, seen this somewhere may even be a example....

So thanks yet again, regards.
Mel.

Hi,
Sorry I'm here again, with another problem! I've looked around be can't see what I am sure is a simple answer!! It's to do with passing variables to and from functions.

At the bottom of my code is a function called scanner, this needs to return the USvalue result to the calling in the main loop, done what I think is right BUT it's not. This subject don't seem to be covered very well on the Reference pages. Could someone give me a guide on how it's done..

Thanks again.
Mel.

/* *******************************
;    Filename: 	   PololuDrive	
;    Date: 	   10/08/2013		
;    File Version: 	
;    Written by:   Mel Saunders		
;    Function:	   BuggyDrive	
;    Last Revision:
;    Target        Uno
; ******************************* 

This program demonstrates the use of an L293 dual H bridge.
This can run 2x DC motors and independantly control their speed and direction.
*/

#include <Servo.h>

Servo scanservo;     //Ping Sensor Servo
int Left=0, Centre=90, Right=180;  //Servo angle in Degrees
const int Ping=7, Echo=8;
const int MA1=2,MA2=3,MB1=4,MB2=5;  //Motor pins
const int PWMpin=11;  //Enable pin on L293 for PWM 
const int scanservopin = 15;   // Pin number for scan servo 
float distance, USvalue;
boolean go; 

  void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
  pinMode(Ping, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(scanservopin, OUTPUT);
  go = true;
  scanservo.attach(scanservopin);  // Attach the scan servo
} 

void loop()
{
 if (go == true)
 {
  scan();    
  startup();
   }
else{
  scanner(USvalue);      // test US sensor for obstuction, etc...
  Rturn();
  Halt();
  back();
  Lturn();
  }
} 


//***************Functions++++++++++++++++++++++++
  void startup()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);

  for (int i=0; i <= 255; i++) //start slow, increase speed
 { 
   analogWrite(PWMpin,i);
   delay(50);
}
   go = false;      //Set go to false
}
//-------------------------------------------------------------------
 void Halt()      //Both motors STOP  
{
  digitalWrite(MA1,LOW);           
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(2000);
}
//-------------------------------------------------------------------
void Rturn()      //Turn right
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);       //Right motor reverse 
  digitalWrite(MB2,HIGH);
  delay(1000); 
} 
//-------------------------------------------------------------------
void Lturn()      //Turn left
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,HIGH);       //Right motor forward
  digitalWrite(MB2,LOW);
  delay(1000); 
} 
//-------------------------------------------------------------------
void back()      //Reverse
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,LOW);      //Right motor reverse
  digitalWrite(MB2,HIGH);
  delay(2000);
 }
//------------------------------------------------------------------- 
void scan()
{
    for (int i=Left; i <= Right; i++) 
{
      scanservo.write(i); 
      delay(20);
}
      scanservo.write(Centre); 
}
//-------------------------------------------------------------------
float scanner(USvalue)      //Get distance from SRF005 sensor
{
 digitalWrite(Ping, LOW); // Send low pulse
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(Ping, HIGH); // Send high pulse
 delayMicroseconds(5); // Wait for 5 microseconds
 digitalWrite(Ping, LOW); // Holdoff
 distance = pulseIn(Echo, HIGH); //Listen for echo
 USvalue = (distance / 58.138) * .39; //convert to CM then to inches
 return USvalue 
}
float scanner(USvalue)

What is the type of USvalue?

You probably want

float scanner(void)      //Get distance from SRF005 sensor
{
  float USvalue;
//convert to CM then to inches

Why?