3

I am extremely new to Arduino coding and have downloaded a sketch for the PCA9685 to use with model railroading and turnout control. When I verify the sketch I get several errors ... I don't even know how to resolve and could use some assistance. There are two sketches:

1:

    /***************************************************
  This sketch can be used to operate any number of turnouts on a
  Model Railroad. Code blocks can be added to accomodate any number
  of servos. This sketch uses only two servos for this demonstration.

  The pushbutton can be replaced with a SPST switch
  commonly used on a control panel. Three pin Bicolor LED can be used
  in place of two seperate LEDs in each circuit.

  10K Ohm pulldown resistor used on switch
  330 Ohm resistor used on LEDs. One per pair of LEDs

  Created 03/02/2019 by Tom Kvichak
  https://tomstrainsandthings.com/
  Additional Arduino Projects can be found at the above web address

 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int buttonPin0 = 2;   // Can add as many buttons as needed
int ledPin0C = 3;     // C designates Closed position of turnout
int ledPin0T = 4;     // T designates Thrown position of turnout
int buttonState0 = 0;

int buttonPin1 = 5;
int ledPin1C = 6;
int ledPin1T = 7;
int buttonState1 = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("pca9685_TurnoutFinal!");
  pwm.begin();
  pwm.setPWMFreq(60);
  delay(30);

  pinMode(ledPin0C, OUTPUT);
  pinMode(ledPin0T, OUTPUT);
  pinMode(buttonPin0, INPUT);
  pinMode(ledPin1C, OUTPUT);
  pinMode(ledPin1T, OUTPUT);
  pinMode(buttonPin1, INPUT);
}

void loop() {
  ///////////
  buttonState0 = digitalRead(buttonPin0);
  if (buttonState0 == HIGH) {
    pwm.setPWM(0, 0, 370);
    digitalWrite(ledPin0T, LOW);
    digitalWrite(ledPin0C, HIGH);
  }
  else  {
    pwm.setPWM(0, 0, 285);
    digitalWrite(ledPin0C, LOW);
    digitalWrite(ledPin0T, HIGH);
  }
  //////////
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
    pwm.setPWM(1, 0, 370);
    digitalWrite(ledPin1T, LOW);
    digitalWrite(ledPin1C, HIGH);
  }
  else  {
    pwm.setPWM(1, 0, 285);
    digitalWrite(ledPin1C, LOW);
    digitalWrite(ledPin1T, HIGH);
  }
  //////////
}

2:

    /*************************************************** 
  This is an example for our Adafruit 16-channel PWM & Servo driver
  Servo test - this will drive 8 servos, one after the other on the
  first 8 pins of the PCA9685

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/815

  These drivers use I2C to communicate, 2 pins are required to  
  interface.

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

#define SERVOMIN  110 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  510 // this is the 'maximum' pulse length count (out of 4096)

// May have to adjust values to match your servos
// Test your servo with the procedures below

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("pca9685_TurnoutTest!");
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  delay(50);
}
///////////////////Functions//////////////////////

void driveMin() {
  pwm.setPWM(servonum, 0, SERVOMIN);    //Use to test low range
}
void driveMax() {
  pwm.setPWM(servonum, 0, SERVOMAX);    //Use to test high range
}
///////////////End Of Functions//////////////////

void loop() {

  for( int angle =0; angle<181; angle +=15){  // testing only right now
    delay(500);
    pwm.setPWM(1, 0, convert2angle(angle) );   // May only need this for sweeping the servo
  }

}
int convert2angle(int myang){
   int pulse = map(myang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max 
   Serial.print("Angle/Pulse: ");Serial.print(myang);
   Serial.print(" / "); Serial.println(pulse);
   return pulse;
}

ERROR MESSAGES:

pca9685_TurnoutTest:23:25: error: redefinition of 'Adafruit_PWMServoDriver pwm'

 Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

                         ^

C:\Users\wdhen\Documents\Arduino\libraries\Adafruit-PWM-Servo-Driver-Library-master\examples\pca9685_TurnoutFinal\pca9685_TurnoutFinal.ino:21:25: note: 'Adafruit_PWMServoDriver pwm' previously declared here

 Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

                         ^

C:\Users\wdhen\Documents\Arduino\libraries\Adafruit-PWM-Servo-Driver-Library-master\examples\pca9685_TurnoutFinal\pca9685_TurnoutTest.ino: In function 'void setup()':

pca9685_TurnoutTest:35:6: error: redefinition of 'void setup()'

 void setup() {

      ^

C:\Users\wdhen\Documents\Arduino\libraries\Adafruit-PWM-Servo-Driver-Library-master\examples\pca9685_TurnoutFinal\pca9685_TurnoutFinal.ino:33:6: note: 'void setup()' previously defined here

 void setup() {

      ^

C:\Users\wdhen\Documents\Arduino\libraries\Adafruit-PWM-Servo-Driver-Library-master\examples\pca9685_TurnoutFinal\pca9685_TurnoutTest.ino: In function 'void loop()':

pca9685_TurnoutTest:52:6: error: redefinition of 'void loop()'

 void loop() {

      ^

C:\Users\wdhen\Documents\Arduino\libraries\Adafruit-PWM-Servo-Driver-Library-master\examples\pca9685_TurnoutFinal\pca9685_TurnoutFinal.ino:48:6: note: 'void loop()' previously defined here

 void loop() {

      ^

Using library Wire at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire 
Using library Adafruit-PWM-Servo-Driver-Library-master at version 2.0.0 in folder: C:\Users\wdhen\Documents\Arduino\libraries\Adafruit-PWM-Servo-Driver-Library-master 
exit status 1
redefinition of 'Adafruit_PWMServoDriver pwm'
  • The errors are shown in the Arduino IDE under the sketch. Mark the corresponding text, press Control+C, click "edit" on this site, click at the end of the text in the appearing text editor. Press Control+V to paste the text into the question. Then select the whole text of the error message and click on the {} button, that you can see above the text in the text editor. – chrisl Sep 21 at 18:56
1

You seem to compile a sketch from a folder, where more than 1 .ino file is placed. I guess this from the paths, that are mentioned in the error messages.

The Arduino IDE supports you to organize your code into multiple .ino files, so that big code can be read way easier. For this it put's together all .ino files in the folder of the main sketch, so that they get compiled together.

Since you have at least 2 full sketches in the same folder, this leads to a redefinition error, since these resources (the variable pwm and the functions setup() and loop()) are already defined by the first sketch, when the compiler gets the second sketch.

You have to put all your individual sketches into own folders. Then the error will not appear. I tried to compile your two sketches individually and that worked fine.

  • Thanks so much ... I will work on creating individual sketches! – Wayne Hensley Sep 21 at 21:30
  • Great! Every sketch in an individual file in an individual folder – chrisl Sep 21 at 21:31
  • @WayneHensley If this answer solved your question, you can accept it as correct :) – chrisl Sep 22 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.