Programming next level

Hallo iedereen,

Na een tijdje proberen en nog eens proberen, kom je steeds een stukje verder en dan is je code ook zo steeds gecompliceerder. hieronder heb ik mij code tot nu toe gepost.

alles werkt totdat ik de ****programming section ****** toevoeg ik blijf "Start programming" krijgen op de seriele monitor? de bedoeling is dat als de stop button en de stop button ingedrukt blijven voor 3 seconde de Arduino in "programming state" komt nu zou ik de stepper up en down moeten kunnen laten draaien en terwijl dat gebeurt de stappen geteld worden. Ik heb volensmij te lang gekeken naar de code want ik zie het niet meer ::slight_smile: Iemand?

// define Step and Dir pin on the arduino
const int stepPin = 3;
const int dirPin = 4;
const int EnablePin = 8;
const int SleepPin = 9;

// define Up and Down on the arduino
const int UpPin = 5;     // the number of the pushbutton pin
const int StopPin = 6;     // the number of the pushbutton pin
const int DownPin = 7;     // the number of the pushbutton pin

// variables of the burtoon state
int UpState = 0;         // variable for reading the pushbutton
int StopState = 0;         // variable for reading the pushbutton status
int DownState = 0;         // variable for reading the pushbutton status


int stepCount = 0;         // number of steps the motor has taken

int Time = 0;
int ProgramState = 0;

void setup() {
  // initialize the serial port:
  Serial.begin(9600);

  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);


  pinMode(UpPin, INPUT_PULLUP);
  pinMode(StopPin, INPUT_PULLUP);
  pinMode(DownPin, INPUT_PULLUP);

  pinMode(EnablePin, OUTPUT);
  pinMode(SleepPin, OUTPUT);


}


void loop() {
  // read the state of the pushbutton value:
  UpState = digitalRead(UpPin);
  StopState = digitalRead(StopPin);
  DownState = digitalRead(DownPin);

  // ************* START PROGRAMMING SECTION *************
  if (StopState == HIGH && UpState == HIGH) {
    Time = millis();

  }

  if (Time < 3000) {
    Serial.println ("Start programming");
    ProgramState = 1;
  }

  if (ProgramState == 1 && UpState == HIGH) {
    // turn Up:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

    // X = zero; X = X + 1; Increase X + 1
    for (int x = 0; x = + 1 ; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount++;
    delay(1000); // One second delay
  }

  if (ProgramState == 1 && DownState == HIGH) {
    // turn Up:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, LOW); // Enables the motor to move in a particular direction

    // X = zero; X = X - 1; Increase X - 1
    for (int x = 0; x = - 1 ; x--) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount++;
    delay(1000); // One second delay
  }


  if (ProgramState == 1 && StopState == HIGH) {
    Serial.print("Programming Done");
    ProgramState = 0;
  }

  // ************* START PROGRAMMING SECTION *************



  // check if the upbutton is pressed. if it is, the upState is HIGH:
  if (UpState == HIGH) {
    // turn Up:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

    // Makes 200 pulses for making one full cycle rotation
    for (int x = 0; x < 200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount++;
    delay(1000); // One second delay
  }





  // check if the downbutton is pressed. if it is, the downState is HIGH:
  if (StopState == HIGH) {
    // turn Down:
    digitalWrite(SleepPin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(EnablePin, LOW); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    delay(1000); // One second delay
  }












  // check if the downbutton is pressed. if it is, the downState is HIGH:
  if (DownState == HIGH) {
    // turn Down:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, LOW); //Changes the rotations direction

    // Makes 200 pulses for making two full cycle rotation
    for (int x = 0; x < 200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }

    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount--;
    delay(1000); // One second delay
  }

  else {

  }

}

Time is een int terwijl millis een unsigned long is. Dat gaat niet goed.
Verder is millis een 32 bit counter die eindeloos doortelt en dan weer door 0 gaat. Dus dat klopt ook niet. Dit viel mij gelijk op... op de aifoon.....

je vult time met millis(), maar die millis loopt op dus dan gaat het niet goed meer, naast uiteraard de opmerking van nico.

Je moet dus inderdaad met en starttijd en een stoptijd werken
dus bij het indrukken van de toetsen starttijd instellen, en daarna kijken wanneer er 3000ms voorbij zijn
vb if millis>time+3000 of zoiets

en je schrijft ok nog if time< 3000. dus niet langer dan 3 sec

Ben ook maar beginneling, dus don't shoot me

vadeveni:
vb if millis>time+3000 of zoiets

Alllllltijd millis() - time >= 3000. Op die manier heb je geen probleem met de roll over van millis() :slight_smile:

Hoe?