Go Down

Topic: 2 servos (or any functions) operating simultaneously? (Read 13 times) previous topic - next topic


Jan 02, 2013, 09:27 pm Last Edit: Jan 02, 2013, 09:29 pm by AWOL Reason: 1
The code in reply #19 needs to have the single quotes replaced with double quotes where necessary, otherwise chances of success are slim.
When you've done that and compiled it, repost your code, and tell us how it behaves.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Sweet, I'll try that.  I thought that might possibly be part of the problem ~hopefully it's the whole problem. 
It will probably be several hours before I can get back online with results.


Jan 03, 2013, 07:28 am Last Edit: Jan 06, 2013, 06:09 am by evolion Reason: 1
AWOL:  The use of double quotes for defining the strings was definitely part of, if not the entire, problem.

RESULTS:   The code below prints the defined strings {","}; line by line in sequence to the serial monitor and timed according to the delay.  This is only partially what I wanted because Serial.read either is not reading them as they are being printed or they are not being converted to integers and/or not being written to the servos.  The 'Serial.read/write' section [from zoomkat] of the loop (everything at the bottom after 'if(Serial.available())' ) works fine on its own with manual input into the serial monitor but apparently doesn't do anything in combination with the PROGMEM section/s.  I'm not sure what needs to happen now for the strings to get written to the servos, I don't know what the disconnect is.  It may only be one or two lines away from working, or may need an entirely new approach.   :|
I did rearrange a few things several times, notably: PROGMEM, so I'll try repositioning a few things, before continuing to research.

Code: [Select]
'zoomkat 11-22-12 simple delimited ',' string parse' is combined with 'PROGMEM string demo'
inside of a while loop that gets started with a bumper sensor used as a manual switch.
Intended to control one leg prototyped for a hexapod ~will be further expanded as the bugs
get worked out...

zoomkat 11-22-12 simple delimited ',' string parse
        from serial port input (via serial monitor)
        and print result out serial port
        multi servos added
PROGMEM string demo
       -How to store a table of strings in program memory (flash), and retrieve them.
          Information summarized from:
              -Here is a good template to follow for setting up a
               table (array) of strings in program memory.               */

#include <avr/pgmspace.h>
PROGMEM prog_char string_0[] = {"60a, 90b"};   //positions leg at ready (may have no use)
PROGMEM prog_char string_1[] = {"25a, 170b"};  //gait for one leg (2 motors(a,b))
PROGMEM prog_char string_2[] = {"160a"};       //
PROGMEM prog_char string_3[] = {"5b"};         //
PROGMEM prog_char string_4[] = {"25a"};        //

 // Then set up a table to refer to strings.
PROGMEM const char *string_table[] =   //change "string_table" name to suit
char buffer[4];  //needs to be large enough for the largest string it must hold
                     //may eventually hold up to 30 indexed variables for all servo/sensor functions  
String readString;

#include <Servo.h>
Servo myservoa, myservob;   //create servo object to control a servo

#include <Wire.h>
const int bumperPinR = 2;    // the number of the bumper (whisker) pin
 int bumperStateR = 0;       // variable for reading the bumper status

void setup()

 //myservoa.writeMicroseconds(1500);   //set initial servo position if desired?
 //myservob.writeMicroseconds(1500);   //
myservob.attach(8);     //the pin for the servob control
myservoa.attach(9);     //
 //Serial.println("");     //keep track of what is loaded

pinMode(bumperPinR, INPUT);     //sets the bumper pins as an inputs

Wire.begin();      //initializes/activates voltage sensing for bumperpins?  
void loop()
 bumperStateR = digitalRead(bumperPinR);    //read state of bumper sensors

while (bumperStateR == LOW)      //begin a loop(one leg walking) with bumper sensor
  for (int i = 1; i < 4; i++)    //prints successive strings in table?
        //'strcpy_P' copies strings from program memory to buffer,
        //necessary casts and dereferencing required to gather data from table.
    strcpy_P(buffer, (char*) pgm_read_word(&(string_table[i])));
    //delay(500);                 //delay will certainly need adjustment    

//everything beyond this point does not seem to correlate with that above, although
 //it does control the servos when values are manually entered into the serial monitor
   if (Serial.available())  
     char c = Serial.read();   //gets one byte from serial buffer
     if (c == ',')              //where/what is 'char c'??  
       if (readString.length() >1)   //greater than 1 char??
         Serial.println(readString);  //prints string to serial port out
         int n = readString.toInt();   //convert readString into a number
            // auto select appropriate value ~my servos use 0-180, so 181 should work
         if(n >= 500)                  //don't know if I'm interpreting this correctly
           Serial.print("writing Microseconds: ");       //header for input 'n >= 500' in monitor
           if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
           if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
           Serial.print("writing Angle: ");                 //header for input 'n < 500'
           if(readString.indexOf('a') >0) myservoa.write(n);
           if(readString.indexOf('b') >0) myservob.write(n);
         readString="";    //clears variable for new input
       readString += c;    //makes the string readString

                                                       I need help!
      Here's what I'm trying to do:
1) Store strings to PROGMEM
2) Retrieve strings from PROGMEM using Serial()
3) Write strings to multiple servos in sequence/s
Mainly to have simultaneous operation of multiple servos and sensors, the other would be to conserve memory for functions.
  The 'disjunction' appears to be between 'strcpy_P' and 'if (Serial.available())'.
  I've made too many changes, additions, omissions to the code to remember everything I've tried.  I  can't really go much further with my project without solving this problem.

                              Any suggestions or additional info will be greatly appreciated!


You may need to look at just what PROGMEM does and how it works, and see if it supports the way you are trying to use it.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)



Yes, I have been trying to find as much info on it as possible.  I think I'm heading in the right direction and what I now understand or at least think I understand is that there is a conversion problem.  It is between the 'serial buffer' and the 'readString' variable which is then indexed to 'n' in 'servo.write()'.  Maybe, if possible, I should not do a conversion and try to work with only the buffer up to '.indexOf()'.  I'm still looking for applicable information on this subject. 

Go Up