Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
#define DEBUG 0#define WAIT_FOR_START 1unsigned 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
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.
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.
ok, and to tell the arduino which pins are for PWM it would be likeint 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
//Write the processed values to the ports. PORTD = PORTD_val; PORTB = PORTB_val;
analogWrite (11, 255);
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.
-.- 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...
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