code werkt niet

Arduino specifieke Software
Gebruikers-avatar
Berichten: 13
Geregistreerd: 10 Aug 2017, 20:58

code werkt niet

Berichtdoor AnHacker » 16 Okt 2017, 19:22

hallo,
ik ben aan het probeen om met een remote een stepper motor aan te sturen. alleen als ik 1 keer op een knop druk stopt de hele code. Hoe kan dat? code:
Code: Alles selecteren
#include <IRremote.h>
int input_pin = 12; //set D12 as input signal pin
IRrecv irrecv(input_pin);
decode_results signals;
//________motor_______
#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=1000;
long time;
boolean turning = 0;
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // enable input from IR receiver
  //_________motor_________
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);
}
void loop()
{
  //Steps = 0;
  //________self________
  while (turning == 1) {while(steps_left>0){
  currentMillis = micros();
  if(currentMillis-last_time>=1000){
  stepper(1);
  time=time+micros()-last_time;
  last_time=micros();
  steps_left--;
  }
  }
  }
  if (signals.value == 16734885 || signals.value == 16716015) {turning = true;}
  else {turning = false;}
  if (signals.value == 16734885 && turning == true) {Direction = 1;}
  if (signals.value == 16716015 && turning == true) {Direction = 0;}
  //______________remote___________
  if (irrecv.decode(&signals)) {
    Serial.println(signals.value);
    irrecv.resume(); // get the next signal
  }
  //__________________motor_________________
     steps_left=1000;
 
  /*while(steps_left>0){
  currentMillis = micros();
  if(currentMillis-last_time>=1000){
  stepper(1);
  time=time+micros()-last_time;
  last_time=micros();
  steps_left--;
  }
  }*/
   Serial.println(time);
  Serial.println("Wait...!");
 // delay(2000);

}

void stepper(int xw){
  for (int x=0;x<xw;x++){
switch(Steps){
   case 0:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break;
   case 1:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break;
   case 2:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break;
   case 3:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break;
   case 4:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
   case 5:
     digitalWrite(IN1, HIGH);
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
     case 6:
     digitalWrite(IN1, HIGH);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
   case 7:
     digitalWrite(IN1, HIGH);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break;
   default:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
}
SetDirection();
}
}
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7;}
}
Code: Alles selecteren
void loop()
digitalWrite (Eat, HIGH)
digitalWrite (Sleep, HIGH)
digitalWrite (Program, HIGH)

Advertisement

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Bing [Bot] en 4 gasten