Skip to content

Commit 6e873d1

Browse files
committed
tidy up folder, add library properties
1 parent 5535555 commit 6e873d1

24 files changed

+961
-32
lines changed

.gitignore

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# MODSPI_Modules
22
Arduino Library to interface with MODSPI Modules
3+
4+
5+
See https://www.modspi.com/modules for information on all of the modules we offer.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <MODSPI.h>
2+
3+
// Create an instance of the MODSPI_AnalogInputs class named "analogInputs1".
4+
// If you have multiple Analog Input modules, you'll need to create multiple instances.
5+
MODSPI_AnalogInputs analogInputs1;
6+
7+
void setup() {
8+
// Initialize the analogInputs1 instance with D5 as the chip select pin.
9+
analogInputs1.begin(D5); // D5 is the chip select pin.
10+
}
11+
12+
void loop() {
13+
// Loop through analog inputs 0 to 1 and read their values and voltages.
14+
for (int a = 0; a <= 1; a++) {
15+
int readADCValue = analogInputs1.readValue(a); // Read the raw ADC value of input 'a'.
16+
float readADCVoltage = analogInputs1.readVoltage(a); // Read the voltage of input 'a'.
17+
18+
// Print the ADC value and voltage to the serial monitor.
19+
Serial.print("Analog Input ");
20+
Serial.print(a);
21+
Serial.print(": ADC Value = ");
22+
Serial.print(readADCValue);
23+
Serial.print(", Voltage = ");
24+
Serial.println(readADCVoltage);
25+
}
26+
27+
delay(500); // Wait for 500 milliseconds before repeating the loop.
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <MODSPI.h>
2+
3+
// Create an instance of the MODSPI_AnalogOutputs class named "analogOutputs1".
4+
// If you have multiple Analog Output modules, you'll need to create multiple instances.
5+
MODSPI_AnalogOutputs analogOutputs1;
6+
7+
void setup() {
8+
// Initialize the analogOutputs1 instance with D5 as the chip select pin.
9+
analogOutputs1.begin(D5); // D5 is the chip select pin.
10+
}
11+
12+
void loop() {
13+
// Ramp up from 0.0V to 10.0V on channel 0 by setting raw values (0 to 4095).
14+
for (int a = 0; a <= 4095; a++) {
15+
analogOutputs1.writeValue(0, a); // Set channel 0 to value 'a'.
16+
delay(1); // Wait for 1 millisecond before setting the next value.
17+
}
18+
19+
delay(2000); // Wait for 1 second before continuing.
20+
21+
// Ramp up from 0.0V to 10.0V on channel 0 by setting a voltage.
22+
for (float a = 0; a <= 10; a += 0.2) {
23+
analogOutputs1.writeVoltage(0, a); // Set channel 0 to voltage 'a'.
24+
delay(100); // Wait for 100 milliseconds before setting the next voltage.
25+
}
26+
27+
delay(2000); // Wait for 2 seconds before repeating the loop.
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <MODSPI.h>
2+
3+
// Create an instance of the MODSPI_DigitalInputs class named "inputs1".
4+
// If you have multiple Digital Input modules, you'll need to create multiple instances.
5+
MODSPI_DigitalInputs inputs1;
6+
7+
void setup() {
8+
Serial.begin(115200); // Initialize serial communication at a baud rate of 115200.
9+
inputs1.begin(D5); // Initialize the inputs1 instance with D5 as the chip select pin.
10+
}
11+
12+
void loop() {
13+
// Loop through digital inputs 0 to 7 and read their values.
14+
for (int a = 0; a <= 7; a++) {
15+
bool readInput = inputs1.read(a); // Read the value of input 'a'.
16+
Serial.print("Input");
17+
Serial.print(a); // Print the input number.
18+
Serial.print(": ");
19+
Serial.println(readInput); // Print the value of the input (HIGH or LOW).
20+
}
21+
22+
// Read the values of all inputs simultaneously.
23+
int8_t readAllInputs = inputs1.readAll(); // Read all inputs and return a byte with each bit representing an input.
24+
Serial.print("readAll: ");
25+
Serial.println(readAllInputs); // Print the combined value of all inputs.
26+
27+
delay(500); // Wait for 500 milliseconds before repeating the loop.
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <MODSPI.h>
2+
3+
// Create an instance of the MODSPI_DigitalOutputs class named "outputs1".
4+
// If you have multiple Digital Output modules, you'll need to create multiple instances.
5+
MODSPI_DigitalOutputs outputs1;
6+
7+
void setup() {
8+
// Initialize the outputs1 instance with D5 as the chip select pin.
9+
outputs1.begin(D5); // D5 is the chip select pin.
10+
11+
outputs1.writeAll(255); // Set all of the output channels to HIGH.
12+
delay(300); // Wait for 300 milliseconds.
13+
outputs1.writeAll(0); // Set all of the output channels to LOW.
14+
}
15+
16+
void loop() {
17+
// Loop through digital outputs 0 to 7, turning each on and off in sequence.
18+
for (int a = 0; a <= 7; a++) {
19+
outputs1.write(a, HIGH); // Set the output 'a' to HIGH.
20+
delay(100); // Wait for 100 milliseconds.
21+
outputs1.write(a, LOW); // Set the output 'a' to LOW.
22+
}
23+
}

examples/QuadRelays/QuadRelays.ino

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <MODSPI.h>
2+
3+
// Create an instance of the MODSPI_QuadRelays class named "relays1".
4+
// If you have multiple relay modules, you'll need to create multiple instances.
5+
MODSPI_QuadRelays relays1;
6+
7+
void setup() {
8+
relays1.begin(D5); // D5 is the chip select pin.
9+
}
10+
11+
void loop() {
12+
for (int a = 1; a <= 4; a++) {
13+
relays1.write(a,HIGH); // Set the relay 'a' to HIGH.
14+
delay(100); // Wait for 100 milliseconds.
15+
relays1.write(a,LOW); // Set the relay 'a' to LOW.
16+
delay(100); // Wait for 100 milliseconds.
17+
}
18+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include <MODSPI.h>
2+
3+
//MODSPI - Homing Routine Example
4+
// This is an example of how todo advanced homing with the MODSPI system
5+
// The routine looks for a rising edge of a homing switch, moving the motor towards it
6+
// Once a rising edge is found, it backs away at a slower speed to find the falling edge
7+
// The axis then moves towards a park position. Once parked it moves to a far away position and then
8+
// back towards the homing switch where it repeats the loop.
9+
//
10+
// Note: this code is also in MM and converts to Steps.
11+
// This routine requires a stepper motor module & a digital inputs module
12+
// It is only intended as a guide to help you test and structure your code.
13+
// ---------------------------------------------------------------------------------------------
14+
15+
// Create an instance of the MODSPI_StepperController class named "motor1".
16+
MODSPI_StepperController motor1;
17+
//Create an instance of the MODSPI_DigitalInputs class named "inputs1"
18+
MODSPI_DigitalInputs inputs1;
19+
20+
const float motorRatedCurrent = 1.0; //The maximum current of the motor specified.
21+
const float runCurrent = 0.8; //The current used when the motor is moving
22+
const float idleCurrent = 0.2; //The current used when the motor is idle.
23+
24+
float stepsPerRotation = 200; //Most motors have 200 steps per revolution
25+
float mmPerRotation = 40; //20tooth x 2mm pitch
26+
float stepsPerMM = stepsPerRotation / mmPerRotation;
27+
28+
float speedMM = 200; //Move at 200mm per second
29+
float accelMM = 200; //Accelerate at 200mm per second
30+
float homeSpeedMM = 40; //Home at 80mm per second
31+
float homeAccelMM = 200; //Home accel at 200mm per second
32+
33+
float travelLimitMM = 300; // max travel of 300mm
34+
float parkPositionMM = 5; // parked poisition of 5mm
35+
float OutPositionMM = 250; // move motor to out position
36+
float MidPositionMM = 50; // Position near the homing
37+
38+
float speedSteps = (speedMM / mmPerRotation) * stepsPerRotation;
39+
float accelSteps = (accelMM / mmPerRotation) * stepsPerRotation;
40+
float homeSpeedSteps = (homeSpeedMM / mmPerRotation) * stepsPerRotation;
41+
float homeAccelSteps = (accelMM / mmPerRotation) * stepsPerRotation;
42+
float travelLimitSteps = (travelLimitMM / mmPerRotation) * stepsPerRotation;
43+
float parkPositionSteps = (parkPositionMM / mmPerRotation) * stepsPerRotation;
44+
float OutPositionSteps = (OutPositionMM / mmPerRotation) * stepsPerRotation;
45+
float MidPositionSteps = (MidPositionMM / mmPerRotation) * stepsPerRotation;
46+
47+
enum HomingState {
48+
UNKNOWN,
49+
LOOKING_FOR_RISING_EDGE,
50+
LOOKING_FOR_FALLING_EDGE,
51+
PARKING,
52+
HOMED,
53+
FAILED
54+
};
55+
HomingState homingState1;
56+
57+
58+
enum MovingState {
59+
NONE,
60+
MovingOut,
61+
MovingIn
62+
};
63+
MovingState movingState1;
64+
65+
66+
void setup() {
67+
motor1.begin(bay1); // Stepper Motor module is inserted into bay1 (chip select D5)
68+
inputs1.begin(bay2); // Digital Input module is inserted into bay2 (chip select D6)
69+
70+
// Optional settings
71+
motor1.setMaxSpeed(speedSteps); //Set speed to 2 revolutions per second.
72+
motor1.setAcceleration(accelSteps); // set acceleration to 4 revolutions per second per second.
73+
motor1.setCurrent(motorRatedCurrent, runCurrent, idleCurrent); //Set the currents of the motor
74+
motor1.setCurrentPosition(0); // reset the current position to 0.
75+
}
76+
77+
void loop() {
78+
delay(1); //Wait 1ms
79+
80+
if (homingState1 != HOMED) {
81+
homeMotor1(); //Home Motor routine
82+
} else {
83+
if (motor1.isTargetPositionReached()) {
84+
homingState1 = UNKNOWN;
85+
}
86+
}
87+
}
88+
89+
void homeMotor1() {
90+
bool motor1HomeSwitchState = debounceSwitch(7);
91+
92+
switch (homingState1) {
93+
case UNKNOWN:
94+
motor1.fastStop(); //Quickly stops the motor
95+
motor1.setCurrentPosition(0); //reset the current position
96+
Serial.println("Starting homing.");
97+
motor1.setMaxSpeed(homeSpeedSteps); //Set speed to homing speed
98+
motor1.setAcceleration(homeAccelSteps); // set acceleration to homing accleration
99+
motor1.setTargetPosition(-travelLimitSteps); //move backwards towards homing switch
100+
homingState1 = LOOKING_FOR_RISING_EDGE;
101+
break;
102+
103+
case LOOKING_FOR_RISING_EDGE:
104+
if (motor1HomeSwitchState == HIGH) {
105+
Serial.println("Rising Edge found! Now looking for falling edge.");
106+
motor1.fastStop(); //quickly stop the motor
107+
motor1.setCurrentPosition(0); //reset the current position
108+
motor1.setMaxSpeed(homeSpeedSteps / 5); //set speed to 1/5th speed of homing
109+
motor1.setAcceleration(homeAccelSteps / 5); //set acceleration to 1/5th the homing accel
110+
motor1.setTargetPosition(travelLimitSteps); //move forwards slowly away from homing switch
111+
homingState1 = LOOKING_FOR_FALLING_EDGE;
112+
} else if (motor1.isTargetPositionReached()) {
113+
Serial.println("Failed to find homing switch rising edge within travel limit");
114+
homingState1 = FAILED;
115+
}
116+
break;
117+
118+
case LOOKING_FOR_FALLING_EDGE:
119+
if (motor1HomeSwitchState == LOW) {
120+
Serial.println("Falling Edge found! Now parking.");
121+
motor1.fastStop(); //Quickly stop the motor
122+
motor1.setCurrentPosition(0); //reset the position
123+
motor1.setMaxSpeed(speedSteps); //set speed to normal speed
124+
motor1.setAcceleration(accelSteps); //set acceleration to normal acceleration
125+
motor1.setTargetPosition(parkPositionSteps); //move forwards to park position away from homing switch
126+
homingState1 = PARKING;
127+
} else if (motor1.isTargetPositionReached()) {
128+
Serial.println("Failed to find homing switch falling edge within travel limit");
129+
homingState1 = FAILED;
130+
}
131+
break;
132+
133+
case PARKING:
134+
if (motor1.isTargetPositionReached()) {
135+
Serial.println("Homing Finished");
136+
homingState1 = HOMED;
137+
}
138+
break;
139+
}
140+
}
141+
142+
void moveMotor1() {
143+
if (motor1.isTargetPositionReached()) {
144+
switch (movingState1) {
145+
case NONE:
146+
motor1.setTargetPosition(OutPositionSteps); //move to out position
147+
movingState1 = MovingOut;
148+
break;
149+
case MovingOut:
150+
motor1.setTargetPosition(MidPositionSteps); //move to mid position near homing
151+
movingState1 = MovingIn;
152+
break;
153+
case MovingIn:
154+
movingState1 = NONE; //stop moving and start homing
155+
homingState1 = UNKNOWN; //reset routine to start homing again
156+
break;
157+
}
158+
}
159+
}
160+
161+
bool debounceSwitch(int Din) {
162+
//Sometimes the digital input is a little noisy. This function just adds a delay before reading again.
163+
//Optional, but helps with reliability of homing.
164+
bool startState = inputs1.read(Din);
165+
delay(5);
166+
return inputs1.read(Din);
167+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <MODSPI.h>
2+
3+
// Create an instance of the MODSPI_StepperController class named "motor1".
4+
MODSPI_StepperController motor1;
5+
// Create a second instance for motor2
6+
MODSPI_StepperController motor2;
7+
8+
const float motorRatedCurrent = 1.0; //The maximum current of the motor specified.
9+
const float runCurrent = 0.8; //The current used when the motor is moving
10+
const float idleCurrent = 0.2; //The current used when the motor is idle.
11+
const int maxSpeed = 2 * 200; //Max speed to 2 revs per second
12+
const int maxAccel = 8 * 200; //Max acceleration to 8 revs per second
13+
14+
bool motor1_clockwiseRotation = true;
15+
bool motor2_clockwiseRotation = true;
16+
17+
void setup() {
18+
motor1.begin(bay1); // Module is inserted into bay1 (chip select D5)
19+
motor2.begin(bay2); // Module is inserted into bay2 (chip select D6)
20+
21+
// Motor1 Setup
22+
motor1.setMaxSpeed(maxSpeed); //Set speed to 2 revolutions per second.
23+
motor1.setAcceleration(maxAccel); // set acceleration to 4 revolutions per second per second.
24+
motor1.setCurrent(motorRatedCurrent,runCurrent,idleCurrent); //Set the currents of the motor
25+
motor1.setCurrentPosition(0); // reset the current position to 0.
26+
27+
// Motor2 Setup
28+
motor2.setMaxSpeed(maxSpeed); //Set speed to 2 revolutions per second.
29+
motor2.setAcceleration(maxAccel); // set acceleration to 4 revolutions per second per second.
30+
motor2.setCurrent(motorRatedCurrent,runCurrent,idleCurrent); //Set the currents of the motor
31+
motor2.setCurrentPosition(0); // reset the current position to 0.
32+
}
33+
34+
void loop() {
35+
// Check if motor1 has finished traveling to the target position.
36+
if (motor1.isTargetPositionReached()) {
37+
if (clockwiseRotation) {
38+
motor1.setTargetPosition(200); //start the move clockwise position
39+
clockwiseRotation = false;
40+
} else {
41+
motor1.setTargetPosition(-200); //start the move to counter-clockwise position
42+
clockwiseRotation = true;
43+
}
44+
}
45+
// Check if motor2 has finished traveling to the target position.
46+
if (motor2.isTargetPositionReached()) {
47+
if (clockwiseRotation) {
48+
motor2.setTargetPosition(300); //start the move clockwise position
49+
clockwiseRotation = false;
50+
} else {
51+
motor2.setTargetPosition(-300); //start the move to counter-clockwise position
52+
clockwiseRotation = true;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)