Arduino按钮不工作 - 始终显示为高

时间:2017-05-29 16:59:02

标签: c++ arduino

我不知道我的接线是否错误或我的代码是错误的。我尝试过各种各样的变化。发生了什么事情是名为“重置”的按钮始终显示为“高”并且从不“低”,即使我按下它并按住它。请参阅下面草图的代码和图像。

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define NUMNUCLEARPIXELS 3
#define NUMWINDSOLARPIXELS 3
#define NUMSUBSTATIONPIXELS 3
#define NUMFACTORYPIXELS 3
#define NUMEVPIXELS 3
#define NUMHOMEPIXELS 3


Adafruit_NeoPixel nuclearpixels = Adafruit_NeoPixel(NUMNUCLEARPIXELS, 3 /*pin*/, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel windsolarpixels = Adafruit_NeoPixel(NUMWINDSOLARPIXELS, 5 /*pin*/, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel substationpixels = Adafruit_NeoPixel(NUMSUBSTATIONPIXELS, 6 /*pin*/, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel factorypixels = Adafruit_NeoPixel(NUMFACTORYPIXELS, 10 /*pin*/, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel evpixels = Adafruit_NeoPixel(NUMEVPIXELS, 11 /*pin*/, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel homepixels = Adafruit_NeoPixel(NUMHOMEPIXELS, 9 /*pin*/, NEO_GRB + NEO_KHZ800);


int delayval = 50; // delay in milliseconds
int delaybutton = 100; // delay in milliseconds

const int windsolarbutton = 8;
const int factorybutton = 2;
const int evbutton = 4;
const int homebutton = 7;
const int reset = 12;
int resetval = 0;
int resetcounter = 0;

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);
  // initialize digital pins as input or output

  pinMode(windsolarbutton, INPUT);
  pinMode(factorybutton, INPUT);
  pinMode(evbutton, INPUT);
  pinMode(homebutton, INPUT);
  pinMode(reset, INPUT);

  nuclearpixels.begin(); // This initializes the NeoPixel library.
  windsolarpixels.begin();
  substationpixels.begin();
  factorypixels.begin();
  evpixels.begin();
  homepixels.begin();

}

// the loop routine runs over and over again forever:
void loop() {
  Serial.print(resetcounter);
  Serial.print('\n');
  resetval = digitalRead(reset);
  if (resetval == HIGH) {
    resetcounter = resetcounter + 1;
    Serial.print(resetcounter);
    delay(delaybutton);
    }
  if (resetcounter % 2) {
     // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
     for(int i=0;i<NUMNUCLEARPIXELS;i++){
     // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    nuclearpixels.setPixelColor(i, nuclearpixels.Color(0,150,0)); // Moderately bright green color.\
    nuclearpixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
     for(int i=0;i<NUMWINDSOLARPIXELS;i++){
    windsolarpixels.setPixelColor(i, windsolarpixels.Color(0,150,0));
    windsolarpixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds). 
  }
   for(int i=0;i<NUMSUBSTATIONPIXELS;i++){
    substationpixels.setPixelColor(i, substationpixels.Color(0,150,0));
    substationpixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
   for(int i=0;i<NUMFACTORYPIXELS;i++){
    factorypixels.setPixelColor(i, factorypixels.Color(0,150,0));
    factorypixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
   for(int i=0;i<NUMEVPIXELS;i++){
    evpixels.setPixelColor(i, evpixels.Color(0,150,0));
    evpixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
   for(int i=0;i<NUMHOMEPIXELS;i++){
    homepixels.setPixelColor(i, homepixels.Color(0,150,0));
    homepixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
  }
  else {
      for(int i=0;i<NUMNUCLEARPIXELS;i++){
     // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    nuclearpixels.setPixelColor(i, nuclearpixels.Color(0,0,0)); // Moderately bright green color.\
    nuclearpixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
     for(int i=0;i<NUMWINDSOLARPIXELS;i++){
    windsolarpixels.setPixelColor(i, windsolarpixels.Color(0,0,0));
    windsolarpixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds). 
  }
   for(int i=0;i<NUMSUBSTATIONPIXELS;i++){
    substationpixels.setPixelColor(i, substationpixels.Color(0,0,0));
    substationpixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
   for(int i=0;i<NUMFACTORYPIXELS;i++){
    factorypixels.setPixelColor(i, factorypixels.Color(0,0,0));
    factorypixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
   for(int i=0;i<NUMEVPIXELS;i++){
    evpixels.setPixelColor(i, evpixels.Color(0,0,0));
    evpixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
   for(int i=0;i<NUMHOMEPIXELS;i++){
    homepixels.setPixelColor(i, homepixels.Color(0,0,0));
    homepixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }  
  }
 }

Sketch

1 个答案:

答案 0 :(得分:0)

缺少接地连接。谢谢gre_gor!

相关问题