Show Posts
Pages: [1]
1  Using Arduino / Installation & Troubleshooting / servotimer2 library install errors on: May 16, 2014, 08:59:53 am
I am trying to install the servotime2r library to use with my Uno. Here are my error messages:

_5_4_14long:63: error: 'ServoTimer2' does not name a type
_5_4_14long:64: error: 'ServoTimer2' does not name a type
_5_4_14long.ino: In function 'void setup()':
_5_4_14long:68: error: 'servo1' was not declared in this scope
_5_4_14long:69: error: 'servo2' was not declared in this scope
_5_4_14long.ino: In function 'int move()':
_5_4_14long:226: error: 'servo1' was not declared in this scope
_5_4_14long:230: error: 'servo2' was not declared in this scope

The servotimer2 does not show up in my library list after installation but if I try to re-install it says that it already exists! I am using windows 7.  The code works on an arduino uno downloaded from another computer so the type complaint is not valid. Thank you any suggestions!
2  Using Arduino / Programming Questions / Re: array of servo objects on: February 26, 2014, 09:01:02 am
Thank you for the syntax to create an array of objects...it worked. I am trying now to index into an SD file for both reading and writing. I want to write to a file and read three other files at the same index. Here is my attempt to use the file.position syntax-
Code:
/*
              Record and Playback program
 Cappy Jack 10/10/13
 
 editing for turn switch fred
 This program takes the manuel manipulation of 4 potentiometers and moves 4 servo motors.
 Recording each of them individually while playing back SD files for the other three.
 The record button saves the pot values to a SD memory card and drives the servos.
 The playback button reads the values from the SD card and drives the servos.
 The stop_ button halts the record or playback function.
 There are 4 pots selected by 2 switches in a state diagram. one at a  time
 */

#include <SD.h>
#include <Servo.h>

const int pin6 = 6;      //attaches servos to pins 6 & 8
const int pin7 = 7;
const int pin8 = 8;
const int pin9 = 9;

Servo servo0[4];      // declare objects for servos
File routine0[4];     // creates a file object for the SD card
char rountine[] ={
  'routin0.txt','routin1.txt','routin2','routin3.txt'};

const int halt = 0;   // buttons in main
const int rec = 2;
const int play = 3;

const int switch1 = 4;  // switches to select which pot to record
const int switch2 = 5;

int pot;
int move_val;

void setup() {
  Serial.begin(9600);      // initialize serial communication
  Serial.print("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());
  servo0[0].attach(pin6);     // attach pins to the servos
  servo0[1].attach(pin7);
  servo0[2].attach(pin8);
  servo0[3].attach(pin9);
  pinMode(halt, INPUT_PULLUP);   // the halt button
  pinMode(rec, INPUT_PULLUP);    // the record button
  pinMode(play, INPUT_PULLUP);   // the playback button
  pinMode(switch1, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(switch2, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(10, OUTPUT);           // SD card on
  SD.begin();                    //Initializes the SD library and card
}   // end of set up

int select_pot () {
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 1))
    pot = 0;
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 1))
    pot = 1;
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 0))
    pot = 2;
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 0))
    pot = 3;
  return pot;
}

int readpot(int pot) {
  move_val = analogRead(pot);  // reads the value of the potentiometer (value between 0 and 1023)
  move_val = map(move_val, 0, 1023, 0, 89);  // sets the servo position according to the scaled value
  return move_val;
}

void move_servo(int pot,int move_val) {  // function to move the servo   
  servo0[pot].write(move_val);
  delay(40);   // waits for the servo to get there


void halt_loop(int pot) {
  if (digitalRead(halt)== 0){
    routine0[pot].close();
  }
}

void writesd(int pot, int move_val, int index){
  routine0[pot] = SD.open("routine[pot]",FILE_WRITE);
  routine0[pot].position(index);
  routine0[pot].write(move_val);
  delay(40);
}

void record(int pot, int index){      // function to read the pot, move the servos and write to the SD card
  //  SD.remove("routine.txt");
  readpot(pot);
  move_servo(pot, move_val);
  writesd(pot, move_val, index);
  halt_loop(pot);
}

void playback(int pot, int index){
  int value;
  routine0[pot] = SD.open("routine[pot]");
  routine0[pot].position(index);
  value = routine0[pot].read();
  move_servo(pot,value);
  halt_loop(pot);
}

void switch_pot(){
  switch (pot){
    int index = 0;
  case 0:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 1:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 2:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 3:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 4:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  }
}

void loop() {    // main loop

    if ((digitalRead(rec)) == 0){
    select_pot();
    switch_pot();
    Serial.print(digitalRead(rec));
    Serial.println("  rec   ");
  }

  if ((digitalRead(play)) == 0){
    pot = 4;
    switch_pot();
    Serial.print(digitalRead(play));
    Serial.println("  play   ");
  }
  Serial.print("  looping ");
  delay(100);
}  // end void loop


And here are the error messages from the compiler-
rec_1_play_3m:85: error: no matching function for call to 'File::position(int&)'
C:\Program Files (x86)\Arduino\libraries\SD/SD.h:43: note: candidates are: uint32_t File::position()
rec_1_play_3m.ino: In function 'void playback(int, int)':
rec_1_play_3m:101: error: no matching function for call to 'File::position(int&)'
C:\Program Files (x86)\Arduino\libraries\SD/SD.h:43: note: candidates are: uint32_t File::position()
rec_1_play_3m.ino: In function 'void switch_pot()':
rec_1_play_3m:110: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of 'int index'
rec_1_play_3m:119: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of 'int index'
rec_1_play_3m:128: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of 'int index'
rec_1_play_3m:137: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of 'int index'
rec_1_play_3m:146: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of 'int index'

Could you suggest syntax that will work? Thanking you in advance!
3  Using Arduino / Programming Questions / array of servo objects on: February 22, 2014, 09:39:51 am
Hello,

          I am trying to make an array of servo objects. Here is the sketch:
Code:
/*
              Record and Playback program
 Cappy Jack 10/10/13
 
 editing for turn switch fred
 This program takes the manuel manipulation of 4 potentiometers and moves 4 servo motors.
 Recording each of them individually while playing back SD files for the other three.
 The record button saves the selected pot values to a SD memory card, reads the SD card for the other three and drives all servos.
 The playback button reads the values from the SD card and drives the servos.
 The stop_ button halts the record or playback function.
 There are 4 pots selected by 2 switches in a state diagram. one at a  time
 */

#include <SD.h>
#include <Servo.h>

const int pin6 = 6;      //attaches servos to pins 6 & 8
const int pin7 = 7;
const int pin8 = 8;
const int pin9 = 9;

Servo servo0;      // declare objects for servos
Servo servo1;
Servo servo2;
Servo servo3;
const int halt = 0;
const int rec = 2;
const int play = 3;
const int switch1 = 4;  // switches to select which pot to record
const int switch2 = 5;
int pot;
int move_val;
File routine0;          // creates a file object for the SD card
File routine1;
File routine2;
File routine3;

File routine[]= {
  routine0, routine1,routine2,routine3};
int pots[] = {
  0,1,2,3};
Servo servo[]= {
 servo0, servo1,servo2,servo3};

void setup() {
  Serial.begin(9600);      // initialize serial communication
  Serial.print("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());
  servo0.attach(pin6);     // attach pins to the servos
  servo1.attach(pin7);
  servo2.attach(pin8);
  servo3.attach(pin9);
  pinMode(halt, INPUT_PULLUP);   // the halt button
  pinMode(rec, INPUT_PULLUP);    // the record button
  pinMode(play, INPUT_PULLUP);   // the playback button
  pinMode(switch1, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(switch2, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(10, OUTPUT);           // SD card on
  SD.begin();                    //Initializes the SD library and card
}   // end of set up

int select_pot () {
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 1))
    pot = pots[0];
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 1))
    pot = pots[1];
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 0))
    pot =  pots[2];
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 0))
    pot = pots[3];
  return pot;
}

int readpot(int pot) {
  move_val = analogRead(pot);  // reads the value of the potentiometer (value between 0 and 1023)
  move_val = map(move_val, 0, 1023, 0, 89);  // sets the servo position according to the scaled value
  return move_val;
}

void move_servo(int pot,int move_val) {  // function to move the servo   
  servo[pot].write(move_val);
  delay(40);   // waits for the servo to get there


void halt_loop(int pot) {
  if (digitalRead(halt)== 0){
    routine[pot].close();
 //   break;
  }
}

void writesd(int pot){
  routine[pot] = SD.open(routine[pot],FILE_WRITE);
  routine[pot].write(move_val);
  delay(40);
  }

  void record(int pot){      // function to read the pot, move the servos and write to the SD card
    //  SD.remove("routine.txt");
    open routine[pot];
    readpot(pot);
    moveservo(pot);
    writesd(pot);
    halt_loop(pot);
  }

void playback(int pot){
  int value;
  open routine[pot];
  value = routine[pot].read();
  moveservo(pot);
  halt_loop(pot);
}

void switch_pot(){
  switch (pot){
  case 0:
    record(pot);
    playback(1);
    playback(2);
    playback(3);
  case 1:
    record(pot);
    playback(0);
    playback(2);
    playback(3);
  case 2:
    record(pot);
    playback(0);
    playback(1);
    playback(3);   
  case 3:
    record(pot);
    playback(0);
    playback(1);
    playback(2);
  case 4:
    playback(0);
    playback(1);
    playback(2);
    playback(3);
  }
}

void loop() {    // main loop

    if ((digitalRead(rec)) == 0){
    select_pot();
    switch_pot();
    Serial.print(digitalRead(rec));
    Serial.println("  rec   ");
  }

  if ((digitalRead(playback)) == 0){
    pot = 4;
    switch_pot();
    Serial.print(digitalRead(playback));
    Serial.println("  play   ");
  }
  Serial.print("  looping ");
  delay(100);
}  // end void loop

and here is the compiler error log:
rec_1_play_3k.ino: In function 'void writesd(int)':
rec_1_play_3k:93: error: no matching function for call to 'SDClass::open(File&, int)'
C:\Program Files (x86)\Arduino\libraries\SD/SD.h:74: note: candidates are: File SDClass::open(const char*, uint8_t)
rec_1_play_3k.ino: In function 'void record(int)':
rec_1_play_3k:100: error: 'open' was not declared in this scope
rec_1_play_3k:100: error: expected `;' before 'routine'
rec_1_play_3k:102: error: 'moveservo' was not declared in this scope
rec_1_play_3k.ino: In function 'void playback(int)':
rec_1_play_3k:109: error: 'open' was not declared in this scope
rec_1_play_3k:109: error: expected `;' before 'routine'
rec_1_play_3k:111: error: 'moveservo' was not declared in this scope
rec_1_play_3k.ino: In function 'void loop()':
rec_1_play_3k:154: error: invalid conversion from 'void (*)(int)' to 'uint8_t'
rec_1_play_3k:154: error: initializing argument 1 of 'int digitalRead(uint8_t)'
rec_1_play_3k:157: error: invalid conversion from 'void (*)(int)' to 'uint8_t'
rec_1_play_3k:157: error: initializing argument 1 of 'int digitalRead(uint8_t)'

Could anyone suggest a fix?  Thank you!
4  Using Arduino / Programming Questions / using long constants in PROGMEM on: December 18, 2013, 07:08:19 am
Code:
Hello,

          I am trying to read long constants stored by PROGMEM.  pgm_read_word only reads a 16bit int and returns~1/4 the value  I am trying to read.  avr/pgmspace.h  says there is a #define pgm_read_word_far which will read a 32bit long. But my compiler errors with " error: 'pgm_read_word_far' was not declared in this scope". Here is the code-
[code
]uint32_t long_time[] PROGMEM = {
 224000,260000,257000,251000,259000,212000,244000,274000,213000,259000,246000,245000,281000,210000,229000};

uint32_t ptime;

ptime = pgm_read_word_far (&long_time[song]);
 

Thank you for any help you may offer.
5  Using Arduino / Programming Questions / Arduino Uno and Adafruit SD card datalogger shield problem with analogWrite com on: December 12, 2013, 09:31:04 am
Hello,

           I have an Arduino Uno and a Adafruit SD data logger shield. I wrote a sketch that records the manuel movements of a potentiometer on an SD card and drives servo motors as well. I have a playback function that reads the SD card and moves the servo motors. It works well.

            Now I want to substitute an LED for the servo motor. I used the same sketch changing the LED for the servo motor. The record function works fine. The potentiometer dims and brightens the LED ok. The playback function does not work. It acts like there isn't anything on the SD card (there is).  But when I hold the playback button in the LED blinks furiously for several cycles and quits. And it won't do it again until I record again.

I will paste both sketchs below since they are so small. Any suggestions to make the playback function work with the LED sketch would be highly appreciated!

Code:
/* The original code for the servo motors (works)
              Record and Playback program
 Cappy Jack 10/10/13
 
 This program takes the manuel manipulation of potentiometers and moves servo motors.
 The record button saves the pot values to a SD memory card and drives the servos.
 The playback button reads the values from the SD card and drives the servos.
 The stop button halts the record or playback function.
 */

#include <SD.h>
#include <Servo.h>
// #include <WaveHC.h>  //something to do with free ram in setup
// #include <WaveUtil.h>

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)

// The analog pins that connect to the pots
#define pot1 0           // analog 0
#define pot2 1           // analog 1

const int pin6 = 6;      //attaches servos to pins 6 & 8
const int pin8 = 8;

Servo servo1;      // declare objects for servos
Servo servo2;

const int halt = 0;
const int rec = 2;
const int play = 3;

/*  // see if the card is present and can be initialized:
 if (!SD.begin()) {
 Serial.println("Card failed, or not present");
 // don't do anything more:
 return;
 }
 else
 Serial.println("card initialized.");
 }
 */
File routine;          // creates a file object for the SD card

void setup() {

  Serial.begin(9600);      // initialize serial communication
  //  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  // Serial.println(FreeRam());
  servo1.attach(pin6);     // attach pins to the servos
  servo2.attach(pin8);
  pinMode(halt, INPUT_PULLUP);   // the halt button
  pinMode(rec, INPUT_PULLUP);    // the record button
  pinMode(play, INPUT_PULLUP);   // the playback button
  pinMode(10, OUTPUT);           // SD card on
  SD.begin();                    //Initializes the SD library and card
}

void move_servo(int read1,int read2) {  // function to move the servos 
  servo1.write(read1);                 
  servo2.write(read2);
  delay(15);   // waits for the servo to get there
  /* Serial.print(value1);
   Serial.print("  val1   ");
   Serial.println();
   Serial.print(value2);
   Serial.print(" val2  ");
   Serial.println();
   Serial.print(read1);
   Serial.print("  read1  ");
   Serial.print(read2);
   Serial.print("  read2  ");
   Serial.println();
   */
}
void record(){      // function to read the pots, move the servos and write to the SD card
  int readpot1, readpot2;
  routine = SD.open("routine.txt", FILE_WRITE);
  while(1) {       // end less loop
    readpot1 = analogRead(pot1);  // reads the value of the potentiometer (value between 0 and 1023)
    readpot1 = map(readpot1, 0, 1023, 0, 89);  // sets the servo position according to the scaled value
    routine.write(readpot1);
    readpot2 = analogRead(pot2);
    readpot2 = map(readpot2, 0, 1023, 0, 89);
    routine.write(readpot2);
    move_servo(readpot1,readpot2);
    if (digitalRead(halt)== 0){
      routine.close();
      return;
    }
  } 
}

void playback() {      // function to read Sd card and move the servos
  int value1, value2;
  routine = SD.open("routine.txt");
  while (routine.available()) {
    value1 = routine.read();
    value2 = routine.read();
    value1 = int(value1);
    value2 = int(value2);
    move_servo(value1,value2);
    halt_loop();
  } 
}

void halt_loop() {
  if (digitalRead(halt)== 0){
    routine.close();
    move_servo(45,45); 
    return;
  }
}

void loop() {    // main loop

    if ((digitalRead(rec)) == 0){
    record();
  }

  if ((digitalRead(play)) == 0){
    playback();
  }
  Serial.print("  looping ");
  delay(100);
}

And the LED sketch where record works but playback does not.
Code:
/*
              Record and Playback program
 Cappy Jack 10/10/13
 
 This program takes the manuel manipulation of potentiometers and dims and brightens an LED.
 The record button saves the pot values to a SD memory card and drives the LEDs.
 The playback button reads the values from the SD card and drives the LEDs.
 The stop button halts the record or playback function.
 */

#include <SD.h>

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)

const int POT_1 = 0;  // Analog input pin that the potentiometer is attached to
const int LED_1 = 9; // Analog output pin that the LED is attached to
const int POT_2 = 1;  // Analog input pin that the potentiometer is attached to
const int LED_2 = 6;

const int halt = 0;
const int rec = 2;
const int play = 3;
 const int chipSelect = 10;
File routine;          // creates a file object for the SD card

void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(halt, INPUT_PULLUP);   // the halt button
  pinMode(rec, INPUT_PULLUP);    // the record button
  pinMode(play, INPUT_PULLUP);   // the playback button
  pinMode(10, OUTPUT);           // SD card on
//  SD.begin();                    //Initializes the SD library and card
 
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
 
  // Open up the file we're going to log to!
  routine = SD.open("routine.txt", FILE_WRITE);
  if (! routine) {
    Serial.println("error opening routine.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}

 // end of setup

void record(){      // function to read the pots, move the servos and write to the SD card
  int readpot1, readpot2;
  routine = SD.open("routine.txt", FILE_WRITE);
  while(1) {       // end less loop
    readpot1 = analogRead(POT_1);           
    readpot1 = map(readpot1, 0, 1023, 0, 100);
    routine.write(readpot1);
    analogWrite(LED_1, readpot1);
    readpot2 = analogRead(POT_2);           
    readpot2 = map(readpot2, 0, 1023, 0, 100);
    routine.write(readpot2);
    analogWrite(LED_2, readpot2);           
    // print the results to the serial monitor:
    Serial.print("pot1 = " );                       
    Serial.print(readpot1);     
    Serial.print("\t play = ");     
    Serial.println(digitalRead(play));   
    Serial.print("pot2 = " );                       
    Serial.print(readpot2);     
    Serial.print("\t led_2 = ");     
    Serial.println(LED_2);   
    if (digitalRead(halt)== 0){
      routine.close();
      return;
    }
  } 
}
void playback() {      // function to read Sd card and move the servos
  int value1, value2;
  routine = SD.open("routine.txt");
  while (routine.available()) {
    value1 = routine.read();
    value2 = routine.read();
    value1 = int(value1);
    value2 = int(value2);
    analogWrite(LED_1, value1);
    analogWrite(LED_2, value2);
    halt_loop();
  } 
}

void halt_loop() {
  if (digitalRead(halt)== 0){
    routine.close();
    return;
  }
}

void loop() {    // main loop

    if ((digitalRead(rec)) == 0){
    record();
  }

  if ((digitalRead(play)) == 0){
    Serial.print("\t play = ");     
    Serial.println((digitalRead)(play));   
    playback();
  }
  Serial.print("  looping ");
  delay(100);
}

Thank you!
6  Using Arduino / Storage / Re: Read and write integers to a SD card. on: October 20, 2013, 11:04:16 am
Hello, Thank you for the suggestion. I found a simple way to deal with it and it works. Here are the record and playback functions:

Code:
void move_servo(int read1,int read2) {  // function to move the servos 
  servo1.write(read1);                 
  servo2.write(read2);
  delay(15);   // waits for the servo to get there
 }

void record(){      // function to read the pots, move the servos and write to the SD card
  int readpot1, readpot2;
  routine = SD.open("routine.txt", FILE_WRITE);
  while(1) {       // end less loop
    readpot1 = analogRead(pot1);  // reads the value of the potentiometer (value between 0 and 1023)
    readpot1 = map(readpot1, 0, 1023, 0, 89);  // sets the servo position according to the scaled value
    routine.write(readpot1);
    readpot2 = analogRead(pot2);
    readpot2 = map(readpot2, 0, 1023, 0, 89);
    routine.write(readpot2);
    move_servo(readpot1,readpot2);
    if (digitalRead(halt)== 0){
      routine.close();
      return;
    }
  } 
}

void playback() {      // function to read Sd card and move the servos
  int value1, value2;
  routine = SD.open("routine.txt");
  while (routine.available()) {
    value1 = routine.read();
    value2 = routine.read();
    value1 = int(value1);
    value2 = int(value2);
    move_servo(value1,value2);
    halt_loop();
  } 
}

I changed the value of the integer I am writing from the value of the potentiometer (0 - 1023) to the value scaled for the servo (0 -180). This way I could use the one byte read to get the true value. (one byte = 0 - 255 chars) Then I cast it as an integer with int(). Voila! It works except for one thing. When I open the file with notepad, I see only gibberish...no numbers! I was hoping to be able to tweak the numbers to improve on the motions a manually operated pot would produce. Have you any suggestions to change the gibberish back to numbers?

Thank you again for your help!

Regards,
Cappy Jack
7  Using Arduino / Storage / Read and write integers to a SD card. on: October 19, 2013, 05:53:09 am
Hello! I am using an Arduino Uno and an Adafruit SD card shield to read 2 potentiometers, write the values (0- 1023) to the SD card and drive 2 servos. This I can do. I also want to read the values from the SD card and drive the servos. The write works but the read does not. Here are the record and playback functions:

Code:
void record(){      // function to read the pots, move the servos and write to the SD card
  int readpot1, readpot2;
  routine = SD.open("routine.txt", FILE_WRITE);
  while(1) {       // end less loop
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // delay for the amount of time we want between readings
    readpot1 = analogRead(pot1);  // reads the value of the potentiometer (value between 0 and 1023)
    routine.print(readpot1);      // write to the SD card
    readpot2 = analogRead(pot2);
    routine.print(readpot2);
    move_servo(readpot1,readpot2);   //  move the servos
    delay(10);
    if (digitalRead(halt)== 0){       //  halt button push escapes the loop
      routine.close();               //  write to and close the SD card
      return;
    }
  } 
}

void playback() {      // function to read SD card and move the servos
  int value1, value2;
  routine = SD.open("routine.txt");
  while (routine.available()) {    // read until the end of the file
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // delay for the amount of time we want between readings
    value1 = routine.read();       // read the SD card file
    value2 = routine.read();
    move_servo(value1,value2);     // move the servos
    halt_loop();                   // halt function to escape the loop
  } 
}

The values that the routine.read() returns are not correct. They  do not drive the servos either. I would appreciate any suggestions to write and read integers to the SD card. Thanking you in advance!
Regards,
                  Cappy Jack
8  Using Arduino / Storage / Adafruit SD Data logger shield tutorial on: October 06, 2013, 08:27:15 am
I am trying to use the  Adafruit SD data logger shield. I can find a file with keywords- no explanation, but I cannot find any info on the command structure or descriptions to help me understand the "wrapper". There are two code examples which are confusing to me. I have searched this forum and Adafruits to no avail. Please, does anyone know of a programming tutorial for this shield? Thanks a lot!
9  Using Arduino / Sensors / Re: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.4 on: July 30, 2012, 07:54:06 am
Well, I found the source of the false short ping readings. I had placed the 2 sensors 5 & 10 cm away from the Arduino Uno (with wav shield) on my pedestal. Something on the board emits a directional noise that the sensors heard and misreported the distance as short. When I moved the sensors 20 cm away and put a piece of wood between them and the computer, the noise no longer spoofed the Ping sensors.  Thanks, everyone, for helping with suggestions. Tim - I am going to implement your new ping library next. I will post my results after testing.
10  Using Arduino / Sensors / Re: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.4 on: July 29, 2012, 09:03:00 am
Thanks, Tim, for the reply.
      I am trying to get around short false readings from the Parallax ping sensor. I have the same thing with v 1.4 of NewPing as I do with the std. ping library. Around the middle of the servo sweep I get short readings, typically 15 cm, for a couple of servo moves. Curiously, the ping will read below 15 cm during this dead band. My space has no influence on the position of servo. I have moved it all around to see if I am getting bad readings from my environment. NewPing works fine on both sides of the dead band.  I have removed the servo from the project electrically (disconnect power and signal) and I still get low readings around the center of the sweep when I move it by hand. I have tried it on both servos and ping sensors. I have no idea how the servo position is effecting the ping sensor even when it is not moving and disconnected.
      My project is to sweep 360 deg around my sculpture pedestal with 2 servos and 2 pings. When someone comes close the wav shield plays a short file depending on distance and position. Any distance reading will trigger another wav file and if you walk the "right" way, the whole song will play. I got everything working and started to test when I discovered the short bad distances. I reduced the sketch to just the std. libraries and the short readings persist. I can manipulate them with delays and code but never get rid of them all. I hope that this description will aid you in suggesting how best to use NewPing. And ANY thoughts on the dead band of the servos would be greatly appreciated! Many Thanks!!
11  Using Arduino / Programming Questions / Re: Servo2Timer and Parallax ultrasonic sensors on: July 27, 2012, 07:10:44 am
Hello again. I rewrote my sketch using the servo and ping librarys that come with the IDE. I am still getting low distance values in the middle of the sweep. I can take the servo out of the sketch and the same thing happens. For 9 readings in the middle of the sweep, the distance is 15 cm instead of maximum. I can hold my hand in front of the sensor and it will read shorter distances so the sensor is working properly. I moved the sensor around to eliminate the effect of what I am pinging and can turn the servo by hand but still get low readings in the middle of the sweep. I have 2 servos and 2 ping sensors from Parallax and it happens to both of them. I am attaching the ping sensors to pins 6 & 8 and the servos to pins 7 & 9.

Code:
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
const int ping1 = 7;
const int ping2 = 9;
int cm;
Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup() {
  // initialize serial communication:
  Serial.begin(115200);
  myservo.attach(8);  // attaches the servo on pin 9 to the servo object
  }

int ping(int pingPin){
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);

  return cm;
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

void loop()
{
  for(pos = 0; pos < 180; pos += 10)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(50);
    ping(ping1);
    Serial.print(cm);
    Serial.println("  cm up");
    delay(50);
    ping(ping2);
  }
  for(pos = 180; pos>=1; pos-=10)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(50);    // waits 15ms for the servo to reach the position 
    ping(ping1);
    Serial.print(cm);
    Serial.println("  cm down");
    delay(50);
    ping(ping2);
  }
}


I have put 470mf capacitors across the servos and 33 mf caps across the ping sensors. I drive the servos with a separate 5vDC power supply. These measures had no effect. Does anyone have a suggestion for me?  Thanks!

--Cappy Jack
12  Using Arduino / Sensors / Re: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.4 on: July 24, 2012, 04:11:03 am
I am interested in using the new ping library but am concerned about the interrupt driven version. I am using an arduino uno, 2 parallax ping sensors mounted on servo motors (from Parallax). I want to drive each servo 180 deg to make a full 360 sweep as fast as I can. After each servo move, I want to ping a distance and use that value to play a small wav file. I put an Adafruit wav shield on my arduino and found that it uses the only 16 bit timer. The servo library from arduino uses it too. So I found another servo library that uses an 8 bit timer on the AT328 and I can play wavs and mover servos. I cannot get good pings all the time with the standard ping code.

It appears that the new ping libary uses the 16 bit timer, too. But, in the revision history, it appears that an earlier version does not. Is that correct? I would appreciate any suggestions. Thank you.
13  Using Arduino / Sensors / unreliable measurment using an uno with parallax ping sensor and servo on: July 23, 2012, 08:30:23 am
Hello! I am using a ping sensor with an arduino uno board. I bought a kit with ping sensor, servo motor and mount. I am sweeping the servo back and forth ~180 deg. and pinging between moves. I have tried two approaches with differing unreliable output. I describe the unreliable output below.  The sketch is very small so I will post the whole thing.


Code:
        //   sketch to move servo and ping a distance//   two different ways with different results


        #include <ServoTimer2.h> 
        const int pin6 = 6;   //attaches servo to pin 6
        const int ping1 = 7;  //attaches ping sensor to pin 7


        long duration1;      // used in ping function
        long cm1;             // distance value
        int num1;             // direction value out of servo1
        int dir;                // var for switch to sweep servo back & forth in move


        ServoTimer2 servo1;    // declare objects for servos


        void setup() {
          Serial.begin(9600);      // initialize serial communication:
          servo1.attach(pin6);    // attach pins to the servos
          dir = 0;                     // set initial direction of switch statement
          int num1 = 0;            // initialize servo movement var
          duration1 = 0;           // initialize ping var
        } 


        long ping()   // function to drive the ping sensors
        {
          pinMode(ping1, OUTPUT);   
          digitalWrite(ping1, LOW);
          delayMicroseconds(4);  //was 2
          digitalWrite(ping1, HIGH);
          delayMicroseconds(10);   //was 5
          digitalWrite(ping1, LOW);
          pinMode(ping1, INPUT);
          duration1 = pulseIn(ping1, HIGH);
          cm1 = microsecondsToCentimeters(duration1);
          return cm1;
        } 


        long microsecondsToCentimeters(long microseconds)
        {
          // The speed of sound is 340 m/s or 29 microseconds per centimeter.
          // The ping travels out and back, so to find the distance of the
          // object we take half of the distance travelled.
          return microseconds / 29 / 2;
        }  // end of ping


        long out()           
        {
          Serial.print(cm1);
          Serial.println("  cm1  ");
        }


        int move_another(){     // this gets good distance values from i = 45 down to i = 12 then bad distance
          int count = 0;
          int i;
          for ( i = 1; i != 60; i++){     // servo starts in the middle so for the first loop it hangs from i = 30  until i = 60
            if ( i <= 30 ){                  // then it sweeps back and forth ~180 deg correctly
              num1 = (servo1.read() - 50);
              delay(5);     
              servo1.write(num1);
              delay(400);
              ping();
              out();
              count = count + 1;
              Serial.print(i);
              Serial.println("  i < 15  ");
            }
            else{
              num1 = (servo1.read() + 50);
              delay(5);
              servo1.write(num1);
              delay(400);
              ping();
              out();
              count = count + 1;
              Serial.print(i);
              Serial.println("  i > 15  ");
            }
          }
        }


        int move(){        // This one gets 29 good distances  then 29 bad ones ( always 15 cm)
          switch (dir) {
          case 0:
            ping();
            out();
            num1 = (servo1.read() + 50);
            delay(5);     
            servo1.write(num1);
            delay(250);     
            if (num1 >= 2250){
              dir = 1;
            }
            break;
          case 1:
            ping();
            out();
            num1 = (servo1.read() - 50);
            delay(5);
            servo1.write(num1);
            delay(250);
            if (num1 <= 800){
              dir = 0;
            }
            break; 
          }
        }


        void loop()
        {
          move();
        //  move_another(); 
        }

  /code]

   
        The servo moves as expected, back and forth for ~180 deg. The ping sensor works when the servo is off.

        the function  move_another gets good distance values from i = 45 down to i = 12 then bad distance
        Now a good distance is the maximum range- 350 cm and the bad distance is 15 cm. Oddly enough, if I hold
        my hand in front of the bad reading, it gives me a good reading under 15 cm.

        The function move gets 29 good distances then 29 bad ones ( always 15 cm)
        It measures good values, i.e 350 cm, which I can influence with my hand to give different accurate values. The bad values start
        in the middle of a sweep, i.e. num1 < 1550 go down to 750 and back up to 1650 then get good again.

        I wrote another function which has good and bad readings and a few dodgy reading, i.e. 120 cm when it should be 350.
        I am trying to keep the sketch as simple as possible in order to understand why ping works sometimes and not others.
        I thought perhaps the servo motor gives an interference sound and I tested another servo motor with the same result.
        I also tested another ping sensor with the same results.

        Thanks for having a look. Any suggestions are welcome!

        Regards,
        Cappy

 
14  Using Arduino / Programming Questions / Servo2Timer and Parallax ultrasonic sensors on: July 22, 2012, 01:43:10 pm
Hello. I am trying to drive a servo from Adafruit 180 deg. in increments back and forth. Between moves of the servo I am pinging a parallax ultrasonic sensor to determine distance. I am using Servo2Timer because ultimately I will use the wav shield from Lady Ada which has an interrupt conflict with the standard servo library. The ping sensor does not give a valid distance all the time. The whole sketch is so small I will post it.

Code:
//   sketch to move servo and ping a distance
        //   two different ways with different results

#include <ServoTimer2.h>  
const int pin6 = 6;   //attaches servo to pin 6
const int ping1 = 7;  //attaches ping sensor to pin 7

long duration1;      // used in ping function
long cm1;             // distance value
int num1;             // direction value out of servo1
int dir;                 // var for switch to sweep servo back & forth in move

ServoTimer2 servo1;    // declare object for servo

void setup() {
  Serial.begin(9600);      // initialize serial communication:
  servo1.attach(pin6);    // attach pin to the servo
  dir = 0;                      // set direction of servo to zero
  int num1 = 0;             // initialize direction of servo movement
  duration1 = 0;            // used in the ping function
}  

long ping()   //function to drive the Parallax ultrasonic sensors
{
  pinMode(ping1, OUTPUT);    
  digitalWrite(ping1, LOW);
  delayMicroseconds(2);  
  digitalWrite(ping1, HIGH);
  delayMicroseconds(5);  
  digitalWrite(ping1, LOW);
  pinMode(ping1, INPUT);
  duration1 = pulseIn(ping1, HIGH);
  cm1 = microsecondsToCentimeters(duration1);
  return cm1;
}  

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}  // end of ping

long out()           //function to print distances
{
  Serial.print(cm1);
  Serial.println("  cm1  ");
}

int move_another(){     // this code gets good distance values from i = 45 down to i = 12
                                  // then bad distance- always ~ 15 cm
  int count = 0;
  int i;
  for ( i = 1; i != 60; i++){     // servo starts in the middle so for the first loop it
                                         // hangs from i = 15 until i = 60...doesn't matter...
    if ( i <= 30 ){                   // then it sweeps ~180 deg correctly
      num1 = (servo1.read() - 50);
      delay(5);      
      servo1.write(num1);
      delay(400);
      ping();
      out();
      count = count + 1;
      Serial.print(i);
      Serial.println("  i < 15  ");
    }
    else {
      num1 = (servo1.read() + 50);
      delay(5);
      servo1.write(num1);
      delay(400);
      ping();
      out();
      count = count + 1;
      Serial.print(i);
      Serial.println("  i > 15  ");
    }
  }
}

int move(){        // This one gets 29 good distances  then 29 bad ones ( always 15 cm)
  switch (dir) {    // ad infinitum
  case 0:
    ping();
    out();
    num1 = (servo1.read() + 50);
    delay(5);      
    servo1.write(num1);
    delay(250);    
    if (num1 >= 2250){
      dir = 1;
    }
    break;
  case 1:
    ping();
    out();
    num1 = (servo1.read() - 50);
    delay(5);
    servo1.write(num1);
    delay(250);
    if (num1 <= 800){
      dir = 0;
    }
    break;  
  }
}

void loop()
{
  move();
//  move_another();  
}

I have tried delays in a variety of  values and places in the code because I determined the ping sensors are sensitive to timing. I would appreciate any help anyone can give me. Thanking you in advance.
15  Using Arduino / Programming Questions / Re: Errors Installing External Libraries (MegaServo, ServoTimer2 , SoftwareServo) on: May 14, 2012, 08:21:16 am
Hello....I am using an arduino uno with a adafruit wav shield. I got a conflict with the serv.ccp and waveHC.ccp over using Timer1. I am trying to work around by using ServoTimer2 but with the simplest sketch I get these errors:

sketch_may14b.cpp.o: In function `__static_initialization_and_destruction_0':
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:7: undefined reference to `ServoTimer2::ServoTimer2()'
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:8: undefined reference to `ServoTimer2::ServoTimer2()'
sketch_may14b.cpp.o: In function `loop':
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:19: undefined reference to `ServoTimer2::write(int)'
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:21: undefined reference to `ServoTimer2::write(int)'
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:24: undefined reference to `ServoTimer2::write(int)'
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:26: undefined reference to `ServoTimer2::write(int)'
sketch_may14b.cpp.o: In function `setup':
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:12: undefined reference to `ServoTimer2::attach(int)'
C:\Users\cappy\AppData\Local\Temp\build987738042993949936.tmp/sketch_may14b.cpp:13: undefined reference to `ServoTimer2::attach(int)'

The code is as follows:
Code:
#include <ServoTimer2.h>  // the servo library

ServoTimer2 a;
ServoTimer2 b;

void setup()
{
   a.attach(5);
   b.attach(6);
}

void loop()
{
  a.write(2000);
  delay(500);
  a.write(1000);
  delay(500);
 
  b.write(2000);
  delay(5000);
  b.write(1000);
  delay(500);
}


I have found several versions of ServoTimer2 here and at the Adafruit forum. None of them are working for me. Please advise!

Thank you!!

Cappy Jack
Pages: [1]