Go Down

Topic: Speed Control without delay !  (Read 1 time) previous topic - next topic

veliusta

Feb 04, 2015, 03:59 am Last Edit: Feb 04, 2015, 11:24 am by veliusta


With delay Code;
Code: [Select]
const int LEDPin[10] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
byte y=0;
long wait=1000;
byte SPEED_MOD=0;

void setup(){
 for(y=0; y<10; y++){
  pinMode(LEDPin[y], OUTPUT);
  }
 pinMode(2, INPUT_PULLUP);
 pinMode(13, OUTPUT);
 attachInterrupt(0, KESME, LOW);
}

void loop(){
 for(y=0; y<10; y++){
   digitalWrite(LEDPin[y], HIGH);
   delay(wait);
   digitalWrite(LEDPin[y], LOW);
  }
 }

void KESME(){
if(SPEED_MOD == 5){
  SPEED_MOD=0;
  }
  else{
   SPEED_MOD++;
   }
  if(SPEED_MOD == 0){
  wait=1000;
  }
  if(SPEED_MOD == 1){
  wait=700;
  }
  if(SPEED_MOD == 2){
  wait=500;
  }
  if(SPEED_MOD == 3){
  wait=200;
  }
  if(SPEED_MOD == 4){
  wait=100;
  }
  if(SPEED_MOD == 5){
  wait=50;
  }
}


I want without delay code. How do I make?  :'(
The end time does not pass to the other mode. :'(

nilton61

#1
Feb 04, 2015, 06:15 am Last Edit: Feb 04, 2015, 06:18 am by nilton61
Code: [Select]

const int noLeds = 10;
const int LEDPin[noLeds] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
unsigned long mark;

void loop(){
  if(millis()>=mark){//check time mark
    mark += wait;//increase time mark
    digitalWrite(LEDPin[y++], 0);//turn previous led off
    if(y==noLeds) y = 0;//y=y%noLeds
    digitalWrite(LEDPin[y], 1);//turn next led on
  }//if(millis()>=mark)
}//loop()

veliusta

#2
Feb 04, 2015, 08:47 am Last Edit: Feb 04, 2015, 09:31 am by veliusta
Not Worked
Code: [Select]

const int noLeds = 10;
const int LEDPin[noLeds] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
unsigned long mark;

byte y=0;
long wait=100;
byte SPEED_MOD=0;

void setup(){
for(y=0; y<10; y++){
 pinMode(LEDPin[y], OUTPUT);
 }
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
attachInterrupt(0, KESME, LOW);
}

void loop(){
if(millis()>=mark){//check time mark
mark += wait;//increase time mark
   digitalWrite(LEDPin[y++], 0);//turn previous led off
   if(y==noLeds) y = 0;//y=y%noLeds
   digitalWrite(LEDPin[y], 1);//turn next led on
 }
}

void KESME(){
if(SPEED_MOD == 5){
 SPEED_MOD=0;
 }
 else{
  SPEED_MOD++;
  }
 if(SPEED_MOD == 0){
 wait=10000;
 }
 if(SPEED_MOD == 1){
 wait=700;
 }
 if(SPEED_MOD == 2){
 wait=500;
 }
 if(SPEED_MOD == 3){
 wait=200;
 }
 if(SPEED_MOD == 4){
 wait=100;
 }
 if(SPEED_MOD == 5){
 wait=50;
 }
}


AWOL

Quote
NOT Work
OK, define "work".

Please use code tags when posting code.

nilton61

Since you are using the global y variable in the for() loop in the setup() function y will start at 10 in you code.

Robin2

Not Worked
So what do you think might be the problem.
Thinking is 99% of successful programming.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

nilton61

#6
Feb 04, 2015, 11:01 am Last Edit: Feb 04, 2015, 11:01 am by nilton61
I suggest you make y a local variable in the setup() function:
Code: [Select]
for(int y=0; y<noLeds; y++)pinMode(LEDPin[y], OUTPUT);

veliusta

I did it. No Delay, No millis() :D

Code: [Select]
const int LEDPin[10] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
byte y=0;
long BEKLE=150000;
byte SPEED_MOD=0;
unsigned long DOGRU_ZAMAN;
unsigned long DONGU_ZAMAN;

void setup(){
 for(y=0; y<10; y++){
  pinMode(LEDPin[y], OUTPUT);
  }
 pinMode(2, INPUT_PULLUP);
 pinMode(13, OUTPUT);
 attachInterrupt(0, KESME, LOW);
 y=0;
}

void loop(){
    DOGRU_ZAMAN = DOGRU_ZAMAN+1;
  if(DOGRU_ZAMAN >= (DONGU_ZAMAN + BEKLE)){
     if(y==0){digitalWrite(LEDPin[9], LOW);}
     digitalWrite(LEDPin[y], HIGH);
     digitalWrite(LEDPin[y]-1, LOW);
  if(y == 9){
   y=0;
  }else{
   y++;
  }
    DONGU_ZAMAN = DOGRU_ZAMAN;
  }
 }

void KESME(){
if(SPEED_MOD == 5){
  SPEED_MOD=0;
  }
  else{
   SPEED_MOD++;
   }
  if(SPEED_MOD == 0){
  BEKLE=150000;
  }
  if(SPEED_MOD == 1){
  BEKLE=75000;
  }
  if(SPEED_MOD == 2){
  BEKLE=50000;
  }
  if(SPEED_MOD == 3){
  BEKLE=25000;
  }
  if(SPEED_MOD == 4){
  BEKLE=10000;
  }
  if(SPEED_MOD == 5){
  BEKLE=5000;
  }
}

nilton61

You should use millis() instead, what you have done is writing your own delay which blocks the cpu and does nothing useful except that is has far worse accuracy.

Go Up