Author Topic: Adding PWM capibilites to this Arduino sketch  (Read 2494 times)

0 Members and 1 Guest are viewing this topic.

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Adding PWM capibilites to this Arduino sketch
« on: August 03, 2011, 04:47:46 PM »
Hello, I am currently building a robot, and i have redesigned the way it "drives" so instead of using h-bridges, it uses a mosfet for speed control.

I have used the sketch, server program, and user interface program from the jbprojects website. i modified the scripts for my own use. Here is my understandings of the sketch

The server program that runs on the OpenWRT router acts like a gateway to transfer data from the visual basic program to the serial interface on the router, which then gets picked up by the Arduino.

The first task i have to accomplish is to implement PWM capabilities to the sketch, I'm 16 years old, and not much of a programmer, and totally lost when it comes to this, i know for a fact that i will not be able to do this myself. I'm good with hardware, mechanics, and code manipulation, not writing code. nor am i good with spelling, which is probably obvious.

In reality all i really need is 3 levels of speed, for each direction

0 (which would be off)

1 (which would be low)

2 (which would be medium)

3 (which would be fast)

4 (which needs to enable the reverse relay for that side, and be low)

5 (which needs to enable the reverse relay for that side, and be medium)

6 (which needs to enable the reverse relay for that side, and be fast)


i need separate speed controls for the right and left side, and the commands i would prefer the speed control to me activated by are
A0 (Left side off)
A1 (Left side low)
A2 (left side medium)
A3 (left side fast)
A4 (enable leftreverse relay and pwm low speed)
A5 ((enable leftreverse relay and pwm medium speed))
A6 (enable leftreverse relay and pwm low fast)

B0 (right side off)
B1 (right side low)
B2 (right side medium)
B3 (right side fast)
B4 (enable rightreverse relay and pwm low speed)
B5 (enable rightreverse relay and pwm medium speed)
B6 ((enable rightreverse relay and pwm fast speed)

and here is my code

Code: [Select]
#define DEBUG 0
#define WAIT_FOR_START 1

unsigned char incomingByte = 0;
unsigned long loop_count = 0;
unsigned char horn = 32;
unsigned char redLED = 64;
unsigned char BlueLED = 128;
unsigned char FireWeapon = 4;

unsigned char LeftForward = 1;
unsigned char LeftReverse = 2;
unsigned char RightForward = 4;
unsigned char RightReverse = 8;

unsigned char PORTB_val;
unsigned char PORTD_val;

unsigned char in_char = 0;

void setup()

{




//PORTD = digital IO 0-7
//FireWeapon, horn, redLED, BlueLED

  pinMode(4, OUTPUT);    // sets the digital pin as output
  pinMode(5, OUTPUT);      // sets the digital pin as output
  pinMode(6, OUTPUT);      // sets the digital pin as output
  pinMode(7, OUTPUT);      // sets the digital pin as output

//PORTB = digital IO 8 - 13 
//LeftForward, LeftReverse, RightForward, RightReverse

  pinMode(8, OUTPUT);      // sets the digital pin as output
  pinMode(9, OUTPUT);      // sets the digital pin as output
  pinMode(10, OUTPUT);     // sets the digital pin as output
  pinMode(11, OUTPUT);     // sets the digital pin as output
 
  Serial.begin(9600);      // set up Serial library at 9600 bps
 
  PORTD = redLED;    // turn on the red LED
 
  #if DEBUG
flash_led(3,500);
  #endif
 
  wait_for_start();  //Waits for startup message from router serial port
//continues after receiving it.
}


void flash_led(unsigned int count, unsigned int rate)
{
// debug routine that flashes an LED

 int n_count = 0;

 while (n_count < count)
 {
   n_count++;
   digitalWrite(13, HIGH);       // sets the LED on
   delay(rate);                  // waits for a bit
   digitalWrite(13, LOW);        // sets the LED off
   delay(rate);                  // waits for a bit
 }
}

char get_char()
{
//Function that waits for a character from the serial port
//If none are received, it returns 0.
//The timeout is so that if the router stops sending data to the microcontroller,
//the micrcontroller will stop driving the car, rather than just going forever with
//the last command.  Timeout is around 250mS.

  while (loop_count < 30000)
  {
    loop_count++;

    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      loop_count = 0;
      return incomingByte;
    }
  } 
 
  loop_count = 0;
 
  #if DEBUG
        Serial.print('X', BYTE);
  #endif
 
  return 0;
}

unsigned char wait_for_start()
{
//Waits for startup message from router serial port
#if WAIT_FOR_START

  #if DEBUG
    Serial.println("Waiting...");
  #endif

  while(1)
  {
    if (get_char() == 'j' && get_char() == 'b' && get_char() == 'p' && get_char() == 'r' && get_char() == 'o')
  {
   
  #if DEBUG
      Serial.print("Start Code Recieved");
  #endif

      return 0;
    }
  }
#endif
}

void loop()   


//Function that processes input from serial port and drives the Robot based
//on that input.

  in_char = get_char();
 
  //Split byte received in to upper and lower halves.
  PORTB_val = in_char & 0x0F;
  PORTD_val = in_char & 0xF0;
 
  //Make sure the BlueLED is turned on now.
  if ((PORTD_val & BlueLED) == 0)
  {
    PORTD_val = PORTD_val + BlueLED;         
  }
 
  //The following IF statements are sanity checks to make sure that LeftForward and LeftReverse cannot be on at the same time
  //and that RightForward and RightReverse can't be on at the same time.
  if ((PORTB_val & (RightForward + RightReverse)) == (RightForward + RightReverse))
  {   
    PORTB_val = PORTB_val - RightReverse;   
  }
 
  if ((PORTB_val & (LeftForward + LeftReverse)) == (LeftForward + LeftReverse))
  {
    PORTB_val = PORTB_val - LeftForward;
  }

  //Write the processed values to the ports.
  PORTD = PORTD_val;
  PORTB = PORTB_val;

  #if DEBUG
    Serial.print(PORTD, HEX);
    Serial.print(PORTB, HEX);
  #endif

}

i need someone to either add the pwm capabilities to this, or to tell me who can do it... Here is a little info about my setup

(Windows PC)  (((WIFI)))  (Linksys wrt54gs v4 router running openwrt white Russian 0.9)  ---(Serial Connection)--- (Arduino)

the speed commands MUST be momentary, as in only on when the command is being sent to the arduino, otherwise the arduino must stop the pwm output.

on the sketch, the RightForward and Left Forward can be removed and replaced with RightDrive and LeftDrive.

The reverse relay (RightReverse and LeftReverse change the polarity of the voltage supplied to the motor, all the microcontroller needs to do to change the speed is to turn on the relay and tell the mosfet what speed to go.

Please help. I would really appreciate it.
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Adding PWM capibilites to this Arduino sketch
« Reply #1 on: August 03, 2011, 04:58:58 PM »
i need someone to either add the pwm capabilities to this, or to tell me who can do it... Here is a little info about my setup
They have a language reference section on the Arduino site: http://arduino.cc/en/Tutorial/HomePage

Basically you just use analogWrite() on one of the analog pins.
Quote
the speed commands MUST be momentary, as in only on when the command is being sent to the arduino, otherwise the arduino must stop the pwm output.
Use analogWrite() to set the duty cycle to 0

Joe

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Re: Adding PWM capibilites to this Arduino sketch
« Reply #2 on: August 04, 2011, 01:51:18 PM »
Like i said, i wont be able to do this myself, i have tried, but it didnt work... so yea... i need help.
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Adding PWM capibilites to this Arduino sketch
« Reply #3 on: August 04, 2011, 02:09:57 PM »
Oh, sorry, I scanned your original message too quickly, and thought you wanted help to learn how to do it. Looking back I see now that's not what you were asking.

I could try helping you do it, but I don't know anyone who will do it for you. Perhaps someone here would. Sorry for the misunderstanding.

Joe

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Re: Adding PWM capibilites to this Arduino sketch
« Reply #4 on: August 04, 2011, 03:15:43 PM »
its ok, i appreciate your help though.

 i always make a attempt at doing something before i post it to a forum, i would like help with this, because my skills are not up to that. thanks.
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Adding PWM capibilites to this Arduino sketch
« Reply #5 on: August 04, 2011, 03:45:36 PM »
its ok, i appreciate your help though.

 i always make a attempt at doing something before i post it to a forum, i would like help with this, because my skills are not up to that. thanks.

Well, if you want to try, writing code for the arduino isn't really that complicated. If you can describe it and are willing to learn a few bits of C I don't see why you couldn't. You seem pretty bright, and I suspect you'll pick things up easily.

For example, to set output pin 11 to the highest setting, all you have to do is

analogWrite (11, 255);

To stop it:

analogWrite (11, 0);

If you know the values you want to set, and what pins the devices are connected to, that's pretty much it.

Joe

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Re: Adding PWM capibilites to this Arduino sketch
« Reply #6 on: August 04, 2011, 03:55:14 PM »
ok, and to tell the arduino which pins are for PWM it would be like

int pwm01 = 5;  //Digital Pin 5

here is my problem, i dont know how to get it to be controlled by a simple hex code, such as A1 or B1, also i need to intergrate the pwm capibilites to my existing sketch, and thats where i am totally lost.


Im sure if i was doing this from scratch i would be able to do it, but because i want to keep the existing sketch, but replace the RightForward and LeftForward with the pwm capibilites, im totally lost :(
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Adding PWM capibilites to this Arduino sketch
« Reply #7 on: August 04, 2011, 04:56:30 PM »
ok, and to tell the arduino which pins are for PWM it would be like

int pwm01 = 5;  //Digital Pin 5
Not really. The pins available for PWM are fixed by the processor. You can use them for non-pwm stuff if you want, but if you want to do pwm those are the ones you need.

Quote
here is my problem, i dont know how to get it to be controlled by a simple hex code, such as A1 or B1, also i need to intergrate the pwm capibilites to my existing sketch, and thats where i am totally lost.


Im sure if i was doing this from scratch i would be able to do it, but because i want to keep the existing sketch, but replace the RightForward and LeftForward with the pwm capibilites, im totally lost :(

Ok, well I looked at the code more closely and I suspect that the following lines

Code: [Select]
   
    //Write the processed values to the ports.                                 
    PORTD = PORTD_val;                                                         
    PORTB = PORTB_val;                                                         

Are intended to set the speed of the motor, is that right? I don't see offhand how it would do that. The problem is that setting pwm requires you to use specific pins. As I understand it, the values being set into these ports could be some range that may or may not have anything to do with pwm.

The normal way is to connect a transistor to a pwm capable pin. The pwm is actually done by a timer. The timer uses a special register to hold the value for the duty cycle, and the value you want for the speed should be put into the register. Setting bits in the port will only turn pins off and on, it won't do pwm.

But don't panic yet. The arduino analogWrite() function does all this for you under the hood, all you have to tell it is which pin the transistor is on, and what speed you want set.

So let's say you have motor A, which is controlled by transistor T1. Connect T1 to a pwm capable pin (lets say arduino pin 11). When you want to set the speed for the motor to 255 (which is the most you can set an 8-bit timer to). You would say

Code: [Select]
analogWrite (11, 255);

I don't know if that helps, and if you want to chuck it I can certainly understand - I've wanted to do that a few times myself.

Joe

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Re: Adding PWM capibilites to this Arduino sketch
« Reply #8 on: August 04, 2011, 06:07:29 PM »
everything in my existing sketch is to control turning on and off of motors, no speed control, the sketch has a debug script, a delay start script, a sanity check to ensure only one direction is on at a time, and the script to recieve and handle serial commands.
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Adding PWM capibilites to this Arduino sketch
« Reply #9 on: August 04, 2011, 11:50:55 PM »
everything in my existing sketch is to control turning on and off of motors, no speed control, the sketch has a debug script, a delay start script, a sanity check to ensure only one direction is on at a time, and the script to recieve and handle serial commands.
Ok, well there are only six PWM capable pins available. You're using 5 of them to turn the motor off and on. I see now why you wanted another one, but there isn't one. The only possibility is to do it in software on another pin, but that's not a viable option.

You're going to have to change the code to do what you want. You can't just add to it.

Joe

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Re: Adding PWM capibilites to this Arduino sketch
« Reply #10 on: August 05, 2011, 12:14:03 AM »
-.- no... only 4 pins are used for motor control... only 2 of them have to be PWM... 1 pwm for each set of motors, and 1 more pin to activate a relay that would switchthe motor into reverse...

 >:( 
*bangs head against wall repeatedly* i should hire a programmer... sadly i dont have the money...

« Last Edit: August 05, 2011, 12:17:42 AM by aouate3 »
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Adding PWM capibilites to this Arduino sketch
« Reply #11 on: August 05, 2011, 07:35:16 AM »
-.- no... only 4 pins are used for motor control... only 2 of them have to be PWM... 1 pwm for each set of motors, and 1 more pin to activate a relay that would switchthe motor into reverse...
 >:( 
*bangs head against wall repeatedly* i should hire a programmer... sadly i dont have the money...
Well, according to the code you posted

Code: [Select]
  pinMode(4, OUTPUT);    // sets the digital pin as output
  pinMode(5, OUTPUT);      // sets the digital pin as output
  pinMode(6, OUTPUT);      // sets the digital pin as output
  pinMode(7, OUTPUT);      // sets the digital pin as output

//PORTB = digital IO 8 - 13 
//LeftForward, LeftReverse, RightForward, RightReverse

  pinMode(8, OUTPUT);      // sets the digital pin as output
  pinMode(9, OUTPUT);      // sets the digital pin as output
  pinMode(10, OUTPUT);     // sets the digital pin as output
  pinMode(11, OUTPUT);     // sets the digital pin as output
 

You're using them for something other than pwm. Same result

Tell you what, give me a precise description of what you want to do (not how to do it) and I'll see what I can come up with this weekend. You probably can do it if you use different pins for what you're doing now.

So, when you get character x over serial, you want y to happen, that kind of thing.

Joe

Offline aouate3Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
  • When i heard someone say "Data storage" i paniced
    • ARS
Re: Adding PWM capibilites to this Arduino sketch
« Reply #12 on: August 05, 2011, 09:48:02 AM »
ok, i do not like my computer (at least the windows xp install i have on my quad boot setup (windows 7 as my main os, Ubuntu for linux needs, Xp for legacy programs and autocad 2012, and OSX86 because i can!) (dual boot is for wimps without a lot of harddrive space!!) i was typing it all up, had it all typed up, stating A LOT  about my sketch and how i would like it to be modified... when it decided it wanted to make google chrome crash..... So i will type it up again later.. i just woke up... so im not in the mood to type it again...
« Last Edit: August 05, 2011, 09:50:18 AM by aouate3 »
Been working with electronics since i was 2 (i was mostly taking them apart) around the age of 6 is when i started repairing electronics, ever since i have been fixing EVERYTHING, even pools and cars

 


Get Your Ad Here

data_list