Compiling error on this Model Railway sketch

I have a sketch for Model Railway turnouts which I did copy from another site and I get a stray\342 error
which from looking at other posts it maybe a character error but not sure as my programming skills are limited, I also notice there is no Void Loop() on this sketch.
Would be grateful if anyone could help inentify problem...

/*This sketch is designed to operate Model Railway Points that are driven by Servo Motors
*A SPDT switch connected to + and – 5vDC with the centre tap going to an Input Pin.
*
*This will scan a predefined set if Pins and allocate as INPUT then set them LOW.
*Dependant on the PIN STATE and whether or not it has changed it will rotate a SERVO on the active pin
*to a pre-determined location as set out in the SERVOMIN/SERVOMAX arrays.
*Millis() function has been used to streamline the operation.
*Serial.print can be removed, they are just for checking.
*
*Machine:- UNO R3 and 16 Servo Controller
*
*Created by Doug Reed 10/05/2016
*
*My thanks to the Forum Members who pointed me in the right direction.
*/

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

unsigned long previousMillisSWITCH=0; //Button Millis() setup
int intervalSWITCH = 20; //intervals for millis()

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // called this way, it uses the default address 0x40

unsigned int SERVOMIN[11] = {172,172,172,246,246,172,246,200,200,150}; //servo lower setting to suit individual servo
unsigned int SERVOMAX[11] = {565,530,500,492,492,565,492,550,550,525}; //servo upper setting to suit individual servo
int inPin[11] = {2,3,4,5,6,7,8,9,10,11,12}; //array of input pins
int pinCount = 11; //the number of pins used, lenght of array
int Dir = 0;
int oldDir[11];
int newDir[11];
int thisPin;
int degrees;

void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates

for (int thisPin = 0 ; thisPin < pinCount ; thisPin++) { //"thisPin" is array position
pinMode(inPin[thisPin],INPUT); //sets all pins 2 -12 as INPUT
}
for (int thisPin = 0 ; thisPin = intervalSWITCH) { // time between button presses

for (int thisPin = 0 ; thisPin < PinCount ; thisPin++) { //assess the direction settings
Dir = digitalRead(thisPin+2);
oldDir[thisPin] = newDir[thisPin];
Serial.print(inPin[thisPin]);
Serial.print(" Dir ");
Serial.println(Dir);

if ((Dir == 1) && (oldDir[thisPin] == 0)){
for (uint16_t pulselen = SERVOMIN[thisPin]; pulselen SERVOMIN[thisPin]; pulselen–) {
pwm.setPWM(thisPin, 0, pulselen);
Serial.print(inPin[thisPin]);
Serial.print(” LOW “);
Serial.println(pulselen);
}
NewDir[thisPin] = 0;
}
}
}
previousMillisSWITCH = currentMillis;
}

Please post a link to where you got the code.

It is possible but very unusual to compile and run a program on the Arduino that has no loop() function, but as posted the code certainly won't work whether it has stray characters in it or not

That is a common error when copying code from a website. Google "stray\342 error" for solutions.

Please read the how to use this forum-please read stickies.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please use code tags when you post code or warning/error messages. To do this, click the </> button on the forum toolbar, then paste the text you want to be in the code tags. Finally, move the cursor out of the code tags before adding any additional text you don't want to be in the code tags. If your browser doesn't show the posting toolbar, then you can manually add the code tags like this:

[code]

[color=blue]// your code is here[/color]

[/code]

The reason for doing this is that, without code tags, the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier for us to read your code and to copy it to the IDE or editor.

Using code tags and other important information is explained in the "How to use this forum" post. Please read it.

John40131:
I get a stray\342 error
which from looking at other posts it maybe a character error but not sure as my programming skills are limited

This is a common sort of problem when you copy code from websites that were not designed to host code and replace some of the text with "prettified" characters. I don't get that error when I copy and paste your code from the forum so I think the forum automatically removed the bad character. So just copy the code from your own post here on the forum and replace your sketch with it.

John40131:
I also notice there is no Void Loop() on this sketch.

Every Arduino sketch must have a setup and a loop function. If you don't need to have any code in the function, then just leave it empty, but still define the function. Note that void and loop are capitalized exactly like I wrote them. Arduino code is case-sensitive!

There are quite a few other errors in that code. Did you copy it exactly from the site? If so, I would be very skeptical about the quality of the work the author of the code does and also I would be distrustful of any other information they have published on their site. There's no shame in having some errors in your first draft of code, it happens even to the professionals, but for the author to publish untested code with obvious errors on a website is not a very nice thing to do to all the people who will find that code and then have a lot of struggles fixing all the problems.

I got the Code from this site, it was in the comments and seem genuine, I did a sketch using inputs to A0 to A10 using a voltage divider which does work to switch 9 servos but I would rather use the Data input instead of a voltage divider also using the PCA9685 to turn the servo's through 60 deg,

http://thenscaler.com/?page_id=174&unapproved=4851&moderation-hash=ef8066b93d746dc7f227ed001f21af8b#comment-4851

Sorry if was in incorrect section or incorrect format..

John

Hi Pert,

I did copy and paste has you suggested and totally different error, but still cant work out problem error on Line 47..

for (int thisPin = 0 ; thisPin = intervalSWITCH) { // time between button presses

and error is expected " ; " before " ) " token..

John

for (int thisPin = 0 ; thisPin = intervalSWITCH)

Look at the reference for the for statement. There are 3 parameters delimited by semicolons. The for statement will run as long as the condition is met. And thisPin = intervalSWITCH is not really a conditional statement, it is an assignment (= for assign == for compare).

The problem I have is very little Sketch knowledge as I only use occasionally and forget what i did last time, also looking at sketches that other peolpe have put up they seem to vary for similar operation which makes it more confusing.

Looking at the last comment desnt really help me as although I have seen the difference between "=" and "==" not sure how would help me solve this problem.

Its Okay for people who write sketch programs for a living or on a very frequent basis, I dont its only for a particular project ike at the moment building a Model Railway which my Grandson can use.

Thanks for all help.

John

for (int thisPin = 0 ; thisPin = intervalSWITCH)
for (int thisPin = 0 ; thisPin < PinCount ; thisPin++)

Compare these 2 for loops from your program.
Do you notice a significant difference between them ?

Sorry but there are no difference to what I have but are you trying to say one is contradicting the other or is it anything to do with 2 FOR loops one after the other .

I am sorry but like I said before my sketch knowledge at this level is not good...

Never been very good at logic, electronics and wiring thats a different matter..LOL

John

for (int thisPin = 0 ; thisPin < PinCount ; thisPin++)

This statement has all of the elements of a proper for loop. It has initialization, condition and increment.

for (int thisPin = 0 ; thisPin = intervalSWITCH)

This line of code is not a proper [for loop](http://for (int thisPin = 0 ;). It has initialization, but no proper comparison* and the increment is missing. *As I said before, thisPin = intervalSWITCH is not a comparison, it is an assignment.

I linked the reference to the [for loop](http://for (int thisPin = 0 ;), have you read the page?

The problem is that every for loop needs 3 parameters and your first one only has 2 parameters so it won't work.

Try to write out in English what you are trying to make happen with

for (int thisPin = 0 ; thisPin = intervalSWITCH)

That's good practice for many coding problems. If you can't say exactly and in detail what you're expecting to happen then you're very likely to have trouble coding it.

Steve

Believe it or not I did get it to compile after a bit of trial and error not sure if it will work but will try prob over weekend, as I said I have a sketch to control the 9 serv's which may be a bit long winded using Analog inputs but it does work and I havnt got the time to spend hours and hours running round in circles.

There is one line which I had to add something between "pulselen SERVOMIN(thisPin) to make work doesnt seem correct to me but unsure what to add so an "=" I tried obviously wrong but like I said it did compile so will try..

for (uint16_t pulselen = SERVOMIN[thisPin]; pulselen = SERVOMIN[thisPin]; pulselen -- ) {

John

pulselen = SERVOMIN[thisPin]

That is obviously not right because, once again, it is an assignment, not a comparison. Without more context I can't say what might need to be. When you modify code from what was previously posted, please post the latest version so that we can keep up.

John40131:
There is one line which I had to add something between "pulselen SERVOMIN(thisPin) to make work doesnt seem correct to me but unsure what to add so an "=" I tried obviously wrong but like I said it did compile so will try..

for (uint16_t pulselen = SERVOMIN[thisPin]; pulselen = SERVOMIN[thisPin]; pulselen -- ) {

You need to take the time to learn how for loops work. This is an essential concept that you will need to have success with Arduino. The way to do this is to write a very simple sketch that contains a for loop and a Serial.println() call. Then you can open Serial Monitor and see how your for loop is working. You can modify the code and then verify that it works as you expect it to. This is much better than trying to do a trial and error process to get the for loop working in your more complex sketch for your project. Between that and the reference page groundFungus linked to (here), you should be able to easily become proficient with for loops.

(X = Y) will assign the value of Y to the variable X and evaluate to whatever that value is. This is not a test for equality. If you use a single equals in a situation where the code wants a boolean for some sort of test, what it actually does is set the variable on left side to value on right side, and then will test if that value is 'true' (any non-zero value is true).

(X==Y) will be true if X and Y have the same value, and will not change either of the variables.

if(x==5) {doSomething();}

Will call doSomething() if x is 5.

if(x=5) {doSomething();}

Will set x to 5, and (because 5 is true) will always call doSomething().

I have included the latest sketch which I will check out prob over weekend, and like I said previously to get to grips with a program such as this which to be honest I cant work out, simple is great.

And I cant spend hours and hours getting know were even though I am 7 years into retiring I have other things that I need to do i'm afraid life is to short...

When I try it, if it works, it works if not will try serial Monitor to see if I can find error if not I have another program that works..

/*This sketch is designed to operate Model Railway Points that are driven by Servo Motors
A SPDT switch connected to + and - 5vDC with the centre tap going to an Input Pin.

This will scan a predefined set if Pins and allocate as INPUT then set them LOW.
Dependant on the PIN STATE and whether or not it has changed it will rotate a SERVO on the active pin
to a pre-determined location as set out in the SERVOMIN/SERVOMAX arrays.
Millis() function has been used to streamline the operation.
Serial.print can be removed, they are just for checking.

Machine:- UNO R3 and 16 Servo Controller

Created by Doug Reed 10/05/2016

My thanks to the Forum Members who pointed me in the right direction.
*/

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

unsigned long previousMillisSWITCH = 0; //Button Millis() setup
int intervalSWITCH = 20; //intervals for millis()

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // called this way, it uses the default address 0x40

unsigned int SERVOMIN[11] = {172, 172, 172, 246, 246, 172, 246, 200, 200, 150}; //servo lower setting to suit individual servo
unsigned int SERVOMAX[11] = {565, 530, 500, 492, 492, 565, 492, 550, 550, 525}; //servo upper setting to suit individual servo
int inPin[11] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //array of input pins
int pinCount = 11; //the number of pins used, lenght of array
int Dir = 0;
int oldDir[11];
int newDir[11];
int thisPin;
int degrees;
int currentMillis;

void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates

for (int thisPin = 0 ; thisPin < pinCount ; thisPin++) { //"thisPin" is array position
pinMode(inPin[thisPin], INPUT); //sets all pins 2 -12 as INPUT
}
for (int thisPin = 0 ; thisPin = intervalSWITCH ; thisPin++) { // time between button presses

for (int thisPin = 0 ; thisPin < pinCount ; thisPin++) { //assess the direction settings
Dir = digitalRead(thisPin + 2);
oldDir[thisPin] = newDir[thisPin];
Serial.print(inPin[thisPin]);
Serial.print(" Dir ");
Serial.println(Dir);

if ((Dir == 1) && (oldDir[thisPin] == 0)) {
for (uint16_t pulselen = SERVOMIN[thisPin]; pulselen = SERVOMIN[thisPin]; pulselen -- ) {
pwm.setPWM(thisPin, 0, pulselen);
Serial.print(inPin[thisPin]);
Serial.print(" LOW ");
Serial.println(pulselen);
}
newDir[thisPin] = 0;
}
}
}
previousMillisSWITCH = currentMillis;
}

for (int thisPin = 0 ; thisPin = intervalSWITCH ; thisPin++)

I see that you are still assigning the value of intervalSWITCH to the thisPin variable in the for loop rather than comparing the two of them. I doubt if that is what you meant to do.

Please explain in plain words what you want that for loop to do.

Incidentally, look at this portion of code

  for (int thisPin = 0 ; thisPin = intervalSWITCH ; thisPin++)   // time between button presses
  {
    for (int thisPin = 0 ; thisPin < pinCount ; thisPin++)   //assess the direction settings
    {
      Dir = digitalRead(thisPin + 2);
      oldDir[thisPin] = newDir[thisPin];

Using the same name for the two nested for loop variables is a recipe for confusion, if not worse. For instance, are you absolutely certain which of the two thisPin variables is being used in the last two lines of code above ?

The object of the exercise is to control 9 servos using a PCA9685 with 2 pairs of servo's being operated at the same time and using toggle switches on a Mimic panel to turn Model Railway points, I have searched the Model Railway and other channels for servo operation but not one has the operation I require apart from Dronebot Workshop but they used Analog Potentiometers so like I said I adapted that sketch which does work, this is the original sketch which I just added more inputs and outputs...
/*
PCA9685 PWM Servo Driver Example
pca9685-servomotor-demo.ino
Demonstrates use of 16 channel I2C PWM driver board with 4 servo motors
Uses Adafruit PWM library
Uses 4 potentiometers for input

DroneBot Workshop 2018

*/

// Include Wire Library for I2C Communications
#include <Wire.h>

// Include Adafruit PWM Library
#include <Adafruit_PWMServoDriver.h>

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define FREQUENCY 50

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Define Potentiometer Inputs

int controlA = A0;
int controlB = A1;
int controlC = A2;
int controlD = A3;

// Define Motor Outputs on PCA9685 board

int motorA = 0;
int motorB = 4;
int motorC = 8;
int motorD = 12;

void setup()
{
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
}

void moveMotor(int controlIn, int motorOut)
{
int pulse_wide, pulse_width, potVal;

// Read values from potentiometer
potVal = analogRead(controlIn);

// Convert to pulse width
pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);

//Control Motor
pwm.setPWM(motorOut, 0, pulse_width);

}

void loop() {

//Control Motor A
moveMotor(controlA, motorA);

//Control Motor B
moveMotor(controlB, motorB);

//Control Motor C
moveMotor(controlC, motorC);

//Control Motor D
moveMotor(controlD, motorD);

}
Also like I said on a number of occasions I am not a programmer and I just tried this sketch from another site which obviously doesnt work so if no one can help me solve the sketch then I think I will call it quits and use the one I have ....

John

The object of the exercise is to control 9 servos using a PCA9685 with 2 pairs of servo's being operated at the same time and using toggle switches on a Mimic panel to turn Model Railway points,

I interpret that as you having 9 single pole/double throw switches that when each is operated will move an associated pair of servos to predefined positions. Is that correct or does "2 pairs of servos" mean something else ?