Hee Mark,
Gefeliciteerd!
Je werkt nu nog met een oude versie van de sketch. Sinds de inbouw van de draaischijf heb ik wat verbeteringen toegevoegd.
Zo was de het uitlezen van de knoppen niet helemaal betrouwbaar.
En was het uitrekenen van de motorpositie ingewikkeld (en niet helemaal juist).
Verder heb ik de sketch wat opgeschoond en geprobeerd alles in definities onder te brengen, zoals de snelheden, afstanden (ik denk dat je met een spindelmotor eerder duizenden dan honderden stappen moet maken) en het aantal sporen instellen.
Verder branden bij het begin nu alleen led 1 en 4.
Voor meer sporen zul je ook meer knopjes en meer leds nodig hebben.
Voor de knoppen kun je de spanningsdeler uitbreiden: gewoon meer weerstanden op een rijtje. Je moet dan wel even uitvogelen wat de analoge waarden worden.je kunt daar eventueel even een korte sketch voor schrijven die de waarde op print (serial print).
Precies tussen deze waarden zet je de grenzen op in (zie de nieuwe versie van de sketch hier onder, werkt niet in de oude versie):
int buttonBoundary[] {900, 750, 600, 300}; // voltage divider boundary values between buttons on buttonsPin
Let op: dit moet van hoog naar laag.
Verder kun je het aantal ledjes uitbreiden. Dat doe je met de pindefinities en in deze array:
int ledPins[] {trackled1 ,trackled2, trackled3, trackled4}; // array for light track moving to or pointing at
Het aantal sporen en de stappen naar elk spoor zet je dan hier op:
#define numberOfTracks 4 // number of tracks on turntable
// arrays below must correspond with number of tracks:
int track[] {-9, -185, -454, -624}; // positions for 4 turntable tracks (motor steps from 0)
Ik heb het meer sporen niet uitgeprobeerd, maar dat kun jij dan mooi testen...
Ook als je de ledjes niet hebt aangesloten, moet je toch net zovel leds definiëren als er sporen zijn. Je kunt natuurlijk ook de bestaande Leds hergebruiken, led 1 = spoor 1 en 5, led 2 = spoor 2 en 6, etc. Dat doe je bijvoorbeeld zo voor 8 sporen:
int ledPins[] {trackled1 ,trackled2, trackled3, trackled4, trackled1 ,trackled2, trackled3, trackled4}; // array for light track moving to or pointing at
Affijn, hier is de nieuwe versie:
/* Turntable control
* =================
* For a partial turntable. 4 tracks at angles of 15, 20 and 15 degrees
*
* Beschrijving (NL)
* Besturing voor eeen modelspoor segmentdraaischijf met 4 sporen.
* De draaischijf wordt aangedreven met een stappenmotor.
* Bediening met een '0-positie' sensor en 4 drukknoppen op een spanningsdeler
* Terugmelding over 4 LEDs: een LED voor elk spoor.
*/
/***** Import needed libraries ******/
#include <AccelStepper.h>
#define FULLSTEP 4
#define HALFSTEP 8
/***** Declare Pin Numbers *******/
// motor pins
#define motorPin1 12 // Blue - 28BYJ48 pin 1
#define motorPin2 11 // Pink - 28BYJ48 pin 2
#define motorPin3 10 // Yellow - 28BYJ48 pin 3
#define motorPin4 9 // Orange - 28BYJ48 pin 4
// sensor pins (analog)
#define startPin A0 // pin to detect turntable start position
#define buttonsPin A1 // analog pin to read buttons by Voltage divider
// led indicators pins
# define trackled1 5 // led for track 1
# define trackled2 4 // led for track 2
# define trackled3 3 // led for track 3
# define trackled4 2 // led for track 4
#define ledpin 13 // LED on Arduino
/******* Definitions for turntable control ******/
// ---- Turntable ----
// Physical definitions
#define slack 9 // steps of slack in stepper motor axel from middle
#define initMove -30 // steps to move at initialisation before sensor read
#define numberOfTracks 4 // number of tracks on turntable
// arrays below must correspond with number of tracks:
int track[] {-9, -185, -454, -624}; // positions for 4 turntable tracks (motor steps from 0)
int buttonBoundary[] {900, 750, 600, 300}; // voltage divider boundary values between buttons on buttonsPin
int ledPins[] {trackled1 ,trackled2, trackled3, trackled4}; // array for light track moving to or pointing at
// Operational definitions
#define startTrack 1 // initial track to move to (after detecting 0-position sensor)
#define maxTurnSpeed 55.0 // maximum speed of stepper motor normal operation
#define startTurnSpeed 20.0 // start speed of stepper motor
#define turnAccelleration 46.0 // acceleration of stepper motor
// ---- Led display ----
unsigned long prevms = 0; // blinking led milisecs last on/off
unsigned long curms = millis(); // blinking led milisecs now
const long blnkint = 250; // blink interval in milliisec.
/******* Global variables ******/
// ---- Turntable variables ----
int currentTrack = 0; // position after initialisation
int newTrack = 0; // 1st position to move to after initialisation
int button = -1; // button pressed (0, 1 2 or 3, -1 is no button)
// ---- Led display variables ----
int blnk = HIGH;
/****** Objects *******/
// ---- Stepper motor object ----
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
/****** Functions ******/
// ---- initialiation: 0-position detection ----
// Turntable initialisation after starting/resetting Arduino.
// It first moves the stepper motor counterclockwise (defined by initMove),
// and then moves the turntable clockwise slowly untill 0-positions is sensed
// on "startPin".
// Motor position "0"is set to the sensor position.
// Function returns "true" if sensor has been detected.
//
// Maximum steps is to find sensor: last track position + initial move steps.
// When 0 position is not sensed, function returns "false".
bool initTurntable () {
bool succesfull = true; // return parameter
int x = 0; // step counter
digitalWrite (ledpin,LOW); // show initialisation ongoing on Arduino board
// calculate total maximum number of steps (maxSteps) to find sensor:
// the number of steps to position of last track, including slack and initial move
int maxSteps = abs(track[numberOfTracks - 1]) + abs(initMove) +2*slack; // sensor must be found in less steps
// move turntable into start / reset position
stepper1.setMaxSpeed(1);
stepper1.setAcceleration(500.0);
stepper1.setSpeed(1);
// start with a move counterclockwise in case just beyond sensor...
x = initMove;
// now keep turning the table slowly until start point sensed
do {
stepper1.runToNewPosition(x);
x++;
} while ( (digitalRead (startPin) == HIGH) && (x < maxSteps) ); //run until sensor active, or until max steps done (error)
if (x < maxSteps) { // start position sensed. Intialise stepper position
stepper1.setCurrentPosition(0); // sensor position = 0
digitalWrite (ledpin,HIGH); // end of initialisation
} else { //initialisation error
succesfull = false;
}
stepper1.disableOutputs(); // Turn off stepper motor power
return succesfull;
}
// ---- read button sensor ----
// function to get button from analog pin
// returns button 0-3 pressed.
// returns -1 if no button pressed
int getButtonPressed () {
int r = analogRead (buttonsPin);
int b = -1;
if (r < buttonBoundary[0]) { // a button was pressed
delay(200); // wait for analog pin to stabalize
r = analogRead (buttonsPin);
// check which boundary value has been read on analog buttonPin
if (r < buttonBoundary[0]) {
b = numberOfTracks -1; // check lat button to first
while (r > buttonBoundary[b]) {
b--;
}
}
}
return b;
}
// ---- turntable movement calculation ----
// function to calculate new position for turntable to move to
// input: new track number
// output: new stepper position with corrections mechanical inaccuracies
int calcNewPosition (int trk) {
int pos = 0; // new stepper motor position
int curTurnDirection = 0; // current direction in Turntable = calculate for slack in movement
// keep values last move before calculating new move
currentTrack = newTrack;
newTrack = trk;
// Init new move
// check direction for additional slack movement.
// calculate new stepper position
if ( newTrack > currentTrack ) {
curTurnDirection = -1; // counter clockwise
} else {
curTurnDirection = 1; // clockwise
}
// calculate position to move to
pos = track[newTrack]; // bare track position
pos = pos + (curTurnDirection*2*slack); // correction for slack
return pos;
}
// ---- LED indiator function ----
// led on / of / blink function
// input: led number
// func: 0 = off, 1=blink, 2 = on
void setLed (int led, int func) {
if (func == 0) {
digitalWrite(ledPins[led], LOW);
}
if (func == 2) {
digitalWrite(ledPins[led], HIGH);
}
if (func == 1) {
curms = millis();
if (curms - prevms >= blnkint) {
prevms = curms;
if (blnk == HIGH) {
blnk = LOW;
} else {
blnk = HIGH;
}
digitalWrite(ledPins[led], blnk);
}
}
}
/****** SETUP: RUNS ONCE ******/
void setup()
{
bool init = false; // turntable intialisation status
int x = 0;
// set the digital led pins as output and initialise values
while (x < numberOfTracks) {
pinMode(ledPins[x], OUTPUT);
analogWrite (ledPins[x], 255);
digitalWrite (ledPins[x], LOW); // show initializing: all track leds off
x++;
}
digitalWrite (ledPins[0], HIGH); // show: initializing: outermost track leds on
digitalWrite (ledPins[3], HIGH);
pinMode (ledpin, OUTPUT);
digitalWrite (startPin,HIGH); // initialise startpin high
digitalWrite (buttonsPin,HIGH); // initialise button read
// turntable initialisation: search for 0-position sensor
init = initTurntable() ; // initialise turntable.
while (not init) { // Find 0-position (sensor), false = 0-pos. not found
digitalWrite(ledpin,HIGH); // Blink led indefenitely
delay(100);
digitalWrite(ledpin,LOW);
delay(100);
}
// no error: setup turntable and led indicators for normal operation
x=0;
while (x < numberOfTracks) { // switch of track leds
digitalWrite (ledPins[x], LOW);
x++;
}
// Setings for speed and accelaration during normal operation
stepper1.setMaxSpeed(maxTurnSpeed);
stepper1.setAcceleration(turnAccelleration);
stepper1.setSpeed(startTurnSpeed);
// move to initial track
stepper1.moveTo(calcNewPosition(startTrack)); // move to new position in normal operation
stepper1.runSpeed();
}//--(end setup )---
/****** LOOP: RUNS CONSTANTLY ******/
void loop()
{
stepper1.run(); // operate the stepper motor
// turntable stop
if (stepper1.distanceToGo() == 0) { // turntable at desired position
stepper1.disableOutputs(); // turn stepper motor off (all pins low)
setLed (newTrack,2); //turn on led for track positioned to
button = getButtonPressed (); // check if a button was pressed
if (button > -1 && button != newTrack) { // button pressed for another track than positioned on
setLed (newTrack,0); //turn off led for this track, we're moving away from it.
stepper1.moveTo(calcNewPosition(button)); // calculate new stepper position and move to it.
}
}
// turntable moving
else { // turntable turning (moving to desired position)
setLed (newTrack,1) ; // blink the led for the track where turntable is moving to
}
}//--(end main loop )---
//*********( THE END )***********
Succes!
Rob