按下按钮旋转图像

时间:2019-12-18 22:26:34

标签: arduino

因此,我正在使用Arduino编写脚本,并且应该按下向上按钮时使箭头变成90度。我真的不明白当按下向上按钮时应该如何旋转它。如果您想测试它,我提供了完整的代码。我正在用德语进行操作,因此“ unten”位于底部,“ oben”位于顶部

#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

const int IO_BUTTON_UP = 4;
void setup() {
  Serial.begin(115200);

  Serial.println("Matrix8x8");

  Serial.println("<Autor>");

  matrix.begin(0x70);

  matrix.setBrightness(1);

  pinMode(IO_BUTTON_UP, INPUT);
}
int loops = 0;

void loop() {

  Serial.println("loop");

  int ON = 1;

  int OFF = 0;

  int unten = 0;

  int oben = 7;

  int rotation = 0;

  matrix.setRotation(rotation);

  if (digitalRead(IO_BUTTON_UP) == 1){

  rotation ++;

}

  if (loops == 0){

    unten = 3;

    oben = 4; 

  } else if (loops == 1){

    unten = 3;

    oben = 4;

  } else if (loops == 2){

    unten = 3;

    oben = 4;

  } else if (loops == 3){

    unten = 3;

    oben = 4;

  } else if (loops == 4){

    unten = 0;

    oben = 7;

  } else if (loops == 5){

    unten = 1;

    oben = 6;

  } else if (loops == 6){

    unten = 2;

    oben = 5;

  } else if (loops == 7){

    unten = 3;

    oben = 4;

  }

  for (int i = unten; i <= oben; i++){

    matrix.drawPixel(loops, i, ON);

  }

  matrix.writeDisplay();

  delay(200);



  if (loops == 7){

    delay (200);

    matrix.clear();

    matrix.writeDisplay();

    delay (200);

  }



  loops = (loops = loops + 1) % 8;

  Serial.print ("loops: ");

  Serial.println (loops);

}

1 个答案:

答案 0 :(得分:0)

我得到了答案。我只需要在“无效循环”之外定义“旋转”

#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

int IO_BUTTON_UP = 4;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Matrix8x8");
  Serial.println("<Autor>");
  matrix.begin(0x70);
  matrix.setBrightness(1);
  pinMode(IO_BUTTON_UP, INPUT);
}
int loops = 0;
int rotation = 0;
void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("loop");
  int ON = 1;
  int OFF = 0;
  int unten = 0;
  int oben = 7;
  Serial.println(IO_BUTTON_UP);
  if (digitalRead(IO_BUTTON_UP) == 1){
    rotation++;
  }
  matrix.setRotation(rotation);

  if (loops == 0){
    unten = 3;
    oben = 4; 
  } else if (loops == 1){
    unten = 3;
    oben = 4;
  } else if (loops == 2){
    unten = 3;
    oben = 4;
  } else if (loops == 3){
    unten = 3;
    oben = 4;
  } else if (loops == 4){
    unten = 0;
    oben = 7;
  } else if (loops == 5){
    unten = 1;
    oben = 6;
  } else if (loops == 6){
    unten = 2;
    oben = 5;
  } else if (loops == 7){
    unten = 3;
    oben = 4;
  }
  for (int i = unten; i <= oben; i++){
    matrix.drawPixel(loops, i, ON);
  }
  matrix.writeDisplay();
  delay(200);

  if (loops == 7){
    delay (200);
    matrix.clear();
    matrix.writeDisplay();
    delay (200);
  }

  loops = (loops = loops + 1) % 8;
  Serial.print ("loops: ");
  Serial.println (loops);
}