well, for now my code looks like that, and I can't figure out how to assign addresses with pins to the number of motor
#include <Keypad.h>
#include "led.h"
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm_A = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwm_B = Adafruit_PWMServoDriver(0x41);
Adafruit_PWMServoDriver pwm_C = Adafruit_PWMServoDriver(0x42);
Adafruit_PWMServoDriver pwm_D = Adafruit_PWMServoDriver(0x44);
const byte ROWS = 6; //four rows
const byte COLS = 9; //three columns
unsigned char keys[ROWS][COLS] = {
{'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}
};
byte rowPins[ROWS] = {34, 32, 30, 28, 26, 24}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {53, 51, 49, 47, 45, 43, 41, 39, 37}; //connect to the column pinouts of the kpd
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// In led.h we have defined four variables for each motor.
// They are: motorPin, startTime, interval, and onOff.
const unsigned int numMOTORs = 54;
MOTOR motor[numMOTORs]; // so let's make an array of 56 of them.
unsigned int num;
int incomingNum;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pwm_A.begin();
pwm_A.setPWMFreq(1600); // This is the maximum PWM frequency
pwm_B.begin();
pwm_B.setPWMFreq(1600); // This is the maximum PWM frequency
pwm_C.begin();
pwm_C.setPWMFreq(1600); // This is the maximum PWM frequency
pwm_D.begin();
pwm_D.setPWMFreq(1600); // This is the maximum PWM frequency
uint8_t twbrbackup = TWBR;
// must be changed after calling Wire.begin() (inside pwm.begin())
TWBR = 12; // upgrade to 400KHz!
for (uint8_t pwmnum = 0; pwmnum < 16; pwmnum++) {
pwm_A.setPWM(pwmnum, 0, 0);
pwm_B.setPWM(pwmnum, 0, 0);
pwm_C.setPWM(pwmnum, 0, 0);
pwm_D.setPWM(pwmnum, 0, 0);
}
}
void loop()
{
if (Serial1.available() > 0) {
// read the incoming byte:
incomingNum = Serial1.read();
Serial.print(Serial1.read());
// say what you got:
Serial.print("I received: ");
Serial.println(incomingNum);
}
/* Set the brightness to a medium values */
char key = keypad.getKey();
// In this block we will turn on a selected motor by pressing a key.
if (key)
{
num = keypad.key[0].kcode; // Map key code to motor num.
motor[num].onOff = OFF; // Update the LED onOff status.
// Send your motor 'on command' from here.
// Turn the motor OFF.
Serial.print(num);
// pwm_A.setPWM(num, 0, OFF);
key != keypad.getKey();
}
// In this block we will scan through the entire motors array and
// decide if there are any motors that need to be turned off.
if (incomingNum == 2) {
motor[num].startTime = millis();
for (num = 0; num < numMOTORs; num++)
{
if ( (motor[num].onOff == ON) && ( (millis() - motor[num].startTime) > motor[num].interval) )
{
motor[num].onOff = ON; // Update the LED onOff status.
// pwm_A.setPWM(num, 0, ON);
Serial.print(motor[num].pin);
}
}
}
switch (incomingNum) {
case 1: //figure is touched
pwm_A.setPWM(1, 0, ON);
pwm_B.setPWM(2, 0, ON);
pwm_C.setPWM(4, 0, ON);
pwm_B.setPWM(10, 0, ON);
pwm_A.setPWM(12, 0, ON);
pwm_B.setPWM(7, 0, ON);
pwm_D.setPWM(2, 0, ON);
break;
case 2: //figure is released
pwm_A.setPWM(1, 0, ON);
pwm_B.setPWM(2, 0, ON);
pwm_C.setPWM(4, 0, ON);
pwm_B.setPWM(10, 0, ON);
pwm_A.setPWM(12, 0, ON);
pwm_B.setPWM(7, 0, ON);
pwm_D.setPWM(2, 0, ON);
}
}
#include "motor.h"
// default constructor intializes timer.
MOTOR::MOTOR() {
interval = 350; // milliseconds
}
#include "Arduino.h" // for digitalRead, digitalWrite, etc.
#ifndef _MOTOR_H_
#define _MOTOR_H_
#define ON 500
#define OFF 0
class MOTOR {
public:
// members
unsigned int pin; // Assigned pin number
unsigned long startTime; // Starting milliseconds for the interval.
unsigned long interval; // motor is on for xNumber of milliseconds.
boolean onOff; // Keep track when motor is ON or OFF.
// methods
MOTOR();
private:
};
#endif