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 ![]()
#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);
}
}