A Variable within a Variable

Aim: To scan a number of inputs and take action according to the Input State ie: High or Low

Arduino UNO Input Pins 2 to 12 as Inputs. Driving a series of Servo Motors via a 16 servo module, the narrow version.

I am attempting to keep the code to a minimum by using increment loops, have succeeded with the Input Pins but am having trouble with the Variables OldDir and NewDir.

These need to be incremented to the same increment value as the Input Pin...eg:

for (int i = 2 ; i < 13 ; i++) {
      Direction = digitalRead(i);
   OldDir(i) = NewDir(i);
  if (Direction == HIGH) {

****     if (OldDir(i) == 0) {     *****  this is the part that does not work

ERROR CODES

Arduino: 1.6.8 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Program Files\arduino-1.6.8\Sketches\RailwayPointsControl\RailwayPointsControl.ino: In function 'void loop()':

RailwayPointsControl:50: error: 'OldDir' cannot be used as a function

    OldDir(i) = NewDir(i);

            ^

RailwayPointsControl:50: error: 'NewDir' cannot be used as a function

    OldDir(i) = NewDir(i);

                        ^

RailwayPointsControl:52: error: 'OldDir' cannot be used as a function

     if (OldDir(i) == 0) {

                 ^

RailwayPointsControl:59: error: 'NewDir' cannot be used as a function

   NewDir(i) = 1;

           ^

RailwayPointsControl:63: error: 'OldDir' cannot be used as a function

     if (OldDir(i) == 1) {

                 ^

RailwayPointsControl:70: error: 'NewDir' cannot be used as a function

   NewDir(i) = 0;

           ^

exit status 1
'OldDir' cannot be used as a function

Am I on the right track or is this not possible, is there another way to do this??

Thanks in advance
Doug :slight_smile:

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

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

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

int Point = 0;                                                                   // our servo # counter
int SwitchNumber = 0;
int Direction = 0;
int OldDir = 0;
int NewDir = 0;

void setup() {
  Serial.begin(9600);
  
   for (int i = 2 ; i < 13 ; i++) {                                                 //sets all pins 2 -12 as INPUT
      pinMode(i,INPUT);
      }
   for (int i = 2 ; i < 13 ; i++) {                                                 //sets all pins 2 -12 to LOW
      digitalWrite(i,LOW);
      }  

  pwm.begin();
  pwm.setPWMFreq(60);                                                               // Analog servos run at ~60 Hz updates
}
void loop() {
  
    for (int i = 2 ; i < 13 ; i++) {
        Direction = digitalRead(i);
     OldDir(i) = NewDir(i);
    if (Direction == HIGH) {
      if (OldDir(i) == 0) {
        for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
            pwm.setPWM(Point, 0, pulselen);
            Serial.print(Point);
            Serial.print("    HIGH    ");
            Serial.println(pulselen);
    }
    NewDir(i) = 1;
  }
}
  else
      if (OldDir(i) == 1) {
        for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
            pwm.setPWM(Point, 0, pulselen);
            Serial.print(Point);
            Serial.print("    LOW    ");
            Serial.println(pulselen);
    }
    NewDir(i) = 0;
  }
delay(25);
}
}

OldDir and NewDir need to be arrays

int OldDir[11];
int NewDir[11];

use square brackets, not parenthesis

https://www.arduino.cc/en/Reference/Array

Thanks for the direction Guys, will look into it and see where that leads.
Doug :slight_smile:

Thanks Guys :stuck_out_tongue_closed_eyes:

That got it.
I wont say that i have Arrays down pat, but I now have a workable idea of how to use them.
Full working code attached.
I now have a points controller for my Model Railway.

Doug

/*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 WL888 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 < PinCount ; thisPin++) {
      digitalWrite(InPin[thisPin],0);                                         //sets all pins 2 -12 to LOW

    Serial.print("   InPin  ");                                               //displays the STATE of the PIN
    Serial.print(InPin[thisPin]);
      int p = digitalRead(InPin[thisPin]);
    Serial.print("   State  ");
    Serial.println(p);
      delay(1000);
      }                                  
}
void loop() {
    unsigned long currentMillis = millis();                                           // get current time stamp, only need one for all if-statements
    if ((unsigned long)(currentMillis - previousMillisSWITCH) >= 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 < SERVOMAX[thisPin]; pulselen++) {
            pwm.setPWM(thisPin, 0, pulselen);
            Serial.print(InPin[thisPin]);
            Serial.print("    HIGH    ");
            Serial.println(pulselen);
      }
         NewDir[thisPin] = 1;
    }
    else
    if ((Dir == 0 ) && (OldDir[thisPin] == 1)) {
    
        for (uint16_t pulselen = SERVOMAX[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;
}