Heey everyone,
I am currently working on a project that makes a plateua move back and forth using a stepper motor. On this plateau I have attached LEDs that create patterns. However, the plateau should be moving whilst the pattern is changing. With my current code this is not happening. I know that I have to work with millis, but when I am completely lost on how to do it. Could someone help me?
Here is my code:
#include <AccelStepper.h>
AccelStepper stepper(1, 42, 38);
int i;
int microStep = 46;
int currentAnimation = 0;
int count = 0;
int button = 13;
int state = LOW;
int lastState = LOW;
//bytes for patterns
byte a = B11110000;
byte b = B00010000;
byte c = B01000000;
byte e = B10100000;
//--------------------------------------------------------------------------------------------------------------------------------
void setup()
{
DDRH = B1111000; // set PORT 6-9 of arduino mega high
DDRB = B11110000; // set PORT 10-13 high
stepper.setMaxSpeed(900.0);
stepper.setAcceleration(400.0);
pinMode(button, INPUT);
state = digitalRead(button);
pinMode(microStep, OUTPUT);
digitalWrite(microStep, HIGH);
}
//--------------------------------------------------------------------------------------------------------------------------------
void loop()
{
//This should happen constantly
stepper.runToNewPosition(2000);
delay(500);
stepper.runToNewPosition(0);
// These are the patterns, that should be played whilst the stepper is running. The patterns chaneg when you push the button
if ( state == HIGH & lastState == LOW )
{
currentAnimation++;
}
if (currentAnimation % 3 == 0)
{
same();
}
if (currentAnimation % 3 == 1)
{
left();
}
if (currentAnimation % 3 == 2) {
faster();
}
lastState = state;
state = digitalRead(button);
}
//--------------------------------------------------------------------------------------------------------------------------------
//PATTERNS
void same() {
for (int k = 0; k < 10; k++) //plays 10 times
{
PORTH = a;
PORTB = a;
delay(100);
PORTH = 0;
PORTB = 0;
delay(100);
}
}
void left() {
for (int z = 0; z < 10; z++)
{
PORTH = e;
PORTB = e;
delay(100);
PORTH = ~e;
PORTB = ~e;
delay(100);
}
}
void faster() {
for (int z = 0; z < 256; z++)
{
PORTH = z;
PORTB = z;
delay(100);
}
PORTH = 0;
PORTB = 0;
}