简体   繁体   English

Arduino for 循环,每个循环之间有延迟,无需停止程序

[英]Arduino for loop with delay between each cycle without stopping program

I am a third year student, the first arduino classes started this year - I'm having trouble executing a for loop with delay and without stopping the program.我是一名三年级学生,今年开始的第一个 arduino 课程 - 我在执行 for 循环时遇到问题,延迟且不停止程序。 My task is to create a game similar to a dinosaur in chrome, and the problem is when I want to separate each cycle for 100ms of time for the cacti to move sideways.我的任务是在 chrome 中创建一个类似于恐龙的游戏,问题是当我想将每个周期分开 100 毫秒的时间让仙人掌侧向移动时。 Can someone help me?有人能帮我吗? :) :)

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// punktacja
int score = 0;
int lastScore = 0;

// czy gracz wygral?
bool won = false;

// zmienna do lcd clear po zwycięstwie
bool blocker = true;

int buttonPin = 6;
int buttonState = 0;

// Obliczanie czasu do spawnu obstacli
//unsigned long aktualnyCzas = millis();
//unsigned long zapamietanyCzas = 0;
//unsigned long roznicaCzasu = 0;

byte dino[8] = {
  0b00000,
  0b00111,
  0b00111,
  0b10110,
  0b11111,
  0b01010,
  0b01010,
  0b00000
};

byte cactus[8] = {
  0b00100,
  0b00101,
  0b10101,
  0b10101,
  0b10111,
  0b11100,
  0b00100,
  0b00000
};

byte stone[8] = {
  0b00000,
  0b00100,
  0b01110,
  0b01110,
  0b01110,
  0b11111,
  0b11111,
  0b11111
};

byte clear[8] = {
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};


void setup() {
  
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
  lcd.setCursor(11, 0);
  lcd.print("s:");
  
  lcd.createChar(0, dino);
  lcd.createChar(1, cactus);
  lcd.createChar(2, stone);
  lcd.createChar(9, clear);
}

void loop() {
  
  //Obliczanie czasu
  //aktualnyCzas = millis();
  
  // Odczyt z guzika
  buttonState = digitalRead(buttonPin);

  //Główna pętla gry
  if(won == false)  
  {
    // Spawn kamieni
    if(lastScore < score - 5)
    {
      lastScore = score;
      for(int i = 15; i >= 0; i--)
        {
            SpawnStone(i);
            wait(100);
        }
    }
      
    // Punktacja
    score = millis() / 100;
    lcd.setCursor(13, 0);
    lcd.print(score);
    
    if(buttonState == HIGH)
    {
      JumpDino();

    }
    else if (buttonState == LOW)
    {
      SpawnDino();
    }
  }
  
  //LCD Clear po wygranej
  
  if(score == 1000 && blocker == true)
  {
    blocker = false;
    lcd.clear();
  }
  
  //Informacja o zwycięstwie
  
  if(score > 999)
  {
    won = true;
    lcd.setCursor(0, 0);
    lcd.print("YOU WON!");
    return;
  }
}

void SpawnDino()
{
    lcd.setCursor(0,1);
    lcd.write(byte(0));
    lcd.setCursor(0,0);
    lcd.write(byte(9));
}

void JumpDino()
{
    lcd.setCursor(0,0);
    lcd.write(byte(0));
    lcd.setCursor(0,1);
    lcd.write(byte(9));
}

int SpawnStone(int i)
{
    lcd.setCursor(i,1);
    lcd.write(byte(2));
    lcd.setCursor(i+1,1);
    lcd.write(byte(9));
}

void wait(long duration)
{
    int wait = 0;
    long last = millis();
    while (wait == 0)
    {
        long now = millis();

        if (now - last >= duration) 
        { wait = 1;}
        else {}
    }
}
 

@Edit @编辑

I tried this:我试过这个:

int SpawnStone(int i)
{
    if(actualTime - rememberTime > differenceTime)
    {
        rememberTime = actualTime;
        lcd.setCursor(i,1);
        lcd.write(byte(2));
        lcd.setCursor(i+1,1);
        lcd.write(byte(9));
        SpawnStone(i-1);
    }
}

but it just made my stones appear at starting frame like this :(但它只是让我的石头像这样出现在起始帧:(

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// punktacja
int score = 0;
int lastScore = 0;

// czy gracz jeszcze gra
bool isPlaying = false;

// zmienna do lcd clear po zwycięstwie
bool blocker = true;

int buttonPin = 6;
int buttonState = 0;

// Obliczanie czasu do spawnu obstacli
long actualTime = 0;
long rememberTime = 0;
long differenceTime = 100;

int checkScore;

int stonePosition;

byte dino[8] = {
  0b01111,
  0b01111,
  0b00111,
  0b10110,
  0b11111,
  0b01010,
  0b01010,
  0b01011
};

byte stoneDown[8] = {
  0b00000,
  0b00100,
  0b01110,
  0b01110,
  0b01110,
  0b11111,
  0b11111,
  0b11111
};

byte clear[8] = {
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};


void setup() {
  
  Serial.begin(9600);
  
  checkScore = 0;
  
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
  lcd.setCursor(11, 0);
  lcd.print("s:");
  
  rememberTime = millis();
  
  differenceTime = 100;
  
  lcd.createChar(0, dino);
  lcd.createChar(2, stoneDown);
  lcd.createChar(9, clear);
  
  stonePosition = 15;
}

void loop() {

  
  //Obliczanie czasu
  
  actualTime = millis();
  
  
  
  // Odczyt z guzika
  
  buttonState = digitalRead(buttonPin);

  
  
  //Główna pętla gry
  
  if(isPlaying == false)    
  {
      
    
    // Dodatkowy poziom trudności
  
    if(score >= 1 && score <= 99)
    {
        differenceTime = 100;
    }
  
    if(score >= 100 && score <= 199)
    {
        differenceTime = 50;
        lcd.setCursor(2, 0);
        lcd.print("Faster!");
    }
  
    if(score >= 200 && score <= 299)
    {
        differenceTime = 30;
        lcd.setCursor(2, 0);
        lcd.print("FASTER!");
    }
    
    
    
    // Obsługa guzika
    
    if(buttonState == HIGH)
    {
      JumpDino();
    }
    
    else if (buttonState == LOW)
    {
      SpawnDino();
    }
    
    
    
    // Spawn kamieni
    
    if(actualTime - rememberTime >= differenceTime)
    {
        rememberTime = actualTime;
        stonePosition--;
        SpawnStone(stonePosition);
        if(stonePosition < 0)
        {
           stonePosition = 15;
        }
    }
      
    
    
    // Punktacja
    
    score = millis() / 100;
    lcd.setCursor(13, 0);
    lcd.print(score);
  }
  
  
  //LCD Clear po wygranej
  
  if(score == 301 && blocker == true)
  {
    blocker = false;
    lcd.clear();
  }
  
  
  
  //Informacja o zwycięstwie lub porażce
  
  if(stonePosition == 0 && buttonState == LOW)
  {
    isPlaying = true;
    lcd.setCursor(0, 0);
    lcd.print("YOU LOST!");
    return;
  }
  
  if(score > 300)
  {
    isPlaying = true;
    lcd.setCursor(0, 0);
    lcd.print("YOU WON!");
    return;
  }
}


// Dinozaur na dole lub u góry

void SpawnDino()
{
    lcd.setCursor(0,1);
    lcd.write(byte(0));
    lcd.setCursor(0,0);
    lcd.write(byte(9));
}

void JumpDino()
{
    lcd.setCursor(0,0);
    lcd.write(byte(0));
    if(stonePosition != 0)
    {
        lcd.setCursor(0,1);
        lcd.write(byte(9));
    }
}


// Spawn kamieni - funkcja

int SpawnStone(int i)
{
    lcd.setCursor(i,1);
    lcd.write(byte(2));
    lcd.setCursor(i+1,1);
    lcd.write(byte(9));
}

I did this that way for all time travellers.我对所有时间旅行者都是这样做的。 Hope you enjoy:)希望你喜欢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM