Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › Sub routines?

Page Index Toggle Pages: 1
Sub routines? (Read 4329 times)
06/28/07 at 17:23:00

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Hello!
          I am starting to write a program in wiring.   I will need several sub routines and need to return to the loop.   I have a robot with two drive motors and I have a speed controller tha I will feed a analog out put to control speed and direction.  I will be using the first two PWM outputs.


Code for first test.

// Milkman motor program 1

int MotorLpin = 0;           // Left drive motor Speed Control conected to the Wiring I/O board pin 0
int MotorRPin = 1;           // Right drive motor Speed Control conected to the Wiring I/O board pin 1

int valL = 0;
int valR = 0;
void setup() {

}

void loop() {
  analogWrite(MotorLpin, valL);  // sets the value of analog pin 0
  valL = (valL + 10) % 255;    // increment value and keep it in
                            // the 0-255 range.
  analogWrite(MotorRPin, valR);  // sets the value of analog pin 1
  valR = (valR + 10) % 255;    // increment value and keep it in
                            // the 0-255 range.
}

But I have about a 8 of these I want to use for forward, backward, right turn, left turn and so on.  could you give an idea of what commands I use.  I have write a lot of basic programs, fortan and quick basic programs.   is there a "Got/Return"  or can I name a loop and have it run it and then goto the next step in the main loop?

Rich
 
IP Logged
 
Reply #1 - 06/28/07 at 20:21:53

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
Perhaps looking at this example might help ypu a bit: http://wiring.org.co/learning/examples/stepper.html see how the stepForward() and stepBackward() are created in the code (for driving a stepper motor in this case), and how they can be used (called) from the loop() method.
 
IP Logged
 
Reply #2 - 07/06/07 at 21:01:19

Vaccumdiode2   Offline
Full Member
chicago, IL USA

Posts: 120
***
 
Hi Hernando and everyone.

I am writing a test program to run my motor drive.  it uses a 0.5 volts to 5.5 volt to control it.  at around 3 volts the motor sits still and if you increase or decrease the voltage the motor turns in on of it's two directions.  I will use the 5.0 volts for reverse.  As we know the analog out goes from 0 volts to five volts.  here is the code for my test.  I plan on measuring the analog out voltages to confirm my calculations.  So here is the code...

// Motor drive k166 test program 003
// by Rich Berton <grb2000@peoplepc.com>

// Initializing variables

int valueL = 166;               // Left Motor variable to keep the actual value
int motLpin = 0;                // Left Motor connected to analog pin 0
int LFswitchPin = 0;            // digital pin to attach the L Forward switch
int ledPin = 48;                // Wiring onboard LED

void setup()
{
  pinMode(LFswitchPin, INPUT);   // sets digital pin 0 as input
  pinMode(ledPin, OUTPUT);       // sets digital pin 48 as output
  // pinmode(motLpin, OUTPUT);     // AnalogWrite pins to Left Motor Driver

  void MotLrampUforward()
  {
  // 26 value is full speed one direction and 255
  // value is full speed in the opposite
  // direction.  156 to 179 values is stand still
 
    While(digitalRead(LFswitchPin) == HIGH){  // run while LFswitchpin is HIGH

       If(valueL >= 28) // Ramp up speed from Stand to Full)
     {
       analogWrite(motLpin, valueL);         // sets the value (range from 166 to 26)
       ValueL -= 2;    // ramp valueL to higher speed
       delay(100);        // waits for 100 milliseconds for smooth ramping of speed
     }
     Else{
       analogWrite(motLpin, 26)         // sets the motLpin to 26 High Speed
       delay(100);        // waits for 100 milliseconds for smooth ramping of speed
     }
    }
  }
}

  void loop()
  {
    if(digitalRead(LFswitchPin) == HIGH)  // if the Left Forward Switch is pressed
    {
     digitalWrite(ledPin, HIGH);    // turns the light on
     MotLrampUforward;              // ramp up left motor to full speed
    }
    else                              
    {
     digitalWrite(ledPin, LOW);     // turns the light off
     ValueL = 166;                  // set valueL to a stand state
     analogWrite(motLpin, valueL); // stop motor to a stand state       
    }
    delay(200);                    // waits 200 milliseconds
  }
}


When I compile it I get an error
"error: a function-definition allowed here before "{" token"

I think it is a simple syntax error

After I get the bug out of it I will submit my info on it for others to use.

By the way so far ver 0012 is working great on both my win XP computers.

Rich

 
IP Logged
 
Page Index Toggle Pages: 1