我想为我的项目使用 ACS712 库或默认计算(ACS712-30A),首先,我得到 45-60 范围内的数据,没有任何连接,为了显示设备当前使用情况,我想更改 Neopixels 颜色,但是当我想这样做时,我的数据起点在没有任何连接的情况下变为 200。
连接引脚:
- ACS712 数据:A0
- NeoPixel LED RGB 环形数据:D6
我的代码:
#include <Adafruit_NeoPixel.h>
#include "ACS712.h"
#define PIN 6 // On Trinket or Gemma, suggest changing this to 1
#define NUMPIXELS 16 // Popular NeoPixel ring size
#define DELAYVAL 300 // Time (in milliseconds) to pause between pixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
ACS712 ACS(A0, 5.0, 1023, 66);
const int analogIn = A0;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue = 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
long randNum = 2600;
unsigned long startMillis;
void setup() {
Serial.begin(9600);
pinMode(A0,INPUT);
ACS.autoMidPoint();
startMillis = millis();
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.clear(); // Set all pixel colors to 'off'
pinMode(2, OUTPUT);
cyan();
}
void loop() {
int mA = ACS.mA_AC();
if ((millis() - startMillis) >= 5000) {
if(mA < 480){
Serial.println(0);
}else{
float mW = (mA *0.22);
Serial.println(mW);
}
startMillis = millis();
//LED RGB
if(mA <480){
cyan(); // cyan
}else if(mA <= 1000 && mA >= 480){
green(); // Green
}else if (mA >1000 && mA <=6000){
yellow(); // Yellow
}
}
delay(100);
}
void cyan(){
pixels.setPixelColor(0, pixels.Color( 0, 250, 150));
pixels.setPixelColor(1, pixels.Color( 0, 250, 150));
pixels.setPixelColor(2, pixels.Color( 0, 250, 150));
pixels.setPixelColor(3, pixels.Color( 0, 250, 150));
pixels.setPixelColor(4, pixels.Color( 0, 250, 150));
pixels.setPixelColor(5, pixels.Color( 0, 250, 150));
pixels.setPixelColor(6, pixels.Color( 0, 250, 150));
pixels.setPixelColor(7, pixels.Color( 0, 250, 150));
pixels.setPixelColor(8, pixels.Color( 0, 250, 150));
pixels.setPixelColor(9, pixels.Color( 0, 250, 150));
pixels.setPixelColor(10, pixels.Color( 0, 250, 150));
pixels.setPixelColor(11, pixels.Color( 0, 250, 150));
pixels.setPixelColor(12, pixels.Color( 0, 250, 150));
pixels.setPixelColor(13, pixels.Color( 0, 250, 150));
pixels.setPixelColor(14, pixels.Color( 0, 250, 150));
pixels.setPixelColor(15, pixels.Color( 0, 250, 150));
pixels.show();
}
void green(){
pixels.setPixelColor(0, pixels.Color( 0, 155, 0));
pixels.setPixelColor(1, pixels.Color( 0, 155, 0));
pixels.setPixelColor(2, pixels.Color( 0, 155, 0));
pixels.setPixelColor(3,pixels.Color( 0, 155, 0));
pixels.setPixelColor(4, pixels.Color( 0, 155, 0));
pixels.setPixelColor(5, pixels.Color( 0, 155, 0));
pixels.setPixelColor(6, pixels.Color( 0, 155, 0));
pixels.setPixelColor(7, pixels.Color( 0, 155, 0));
pixels.setPixelColor(8, pixels.Color( 0, 155, 0));
pixels.setPixelColor(9, pixels.Color( 0, 155, 0));
pixels.setPixelColor(10, pixels.Color( 0, 155, 0));
pixels.setPixelColor(11, pixels.Color( 0, 155, 0));
pixels.setPixelColor(12, pixels.Color( 0, 155, 0));
pixels.setPixelColor(13, pixels.Color( 0, 155, 0));
pixels.setPixelColor(14, pixels.Color( 0, 155, 0));
pixels.setPixelColor(15, pixels.Color( 0, 155, 0));
pixels.show();
}
void yellow(){
pixels.setPixelColor(0, pixels.Color( 255, 155, 0));
pixels.setPixelColor(1, pixels.Color( 255, 155, 0));
pixels.setPixelColor(2, pixels.Color( 255, 155, 0));
pixels.setPixelColor(3,pixels.Color( 255, 155, 0));
pixels.setPixelColor(4, pixels.Color( 255, 155, 0));
pixels.setPixelColor(5, pixels.Color( 255, 155, 0));
pixels.setPixelColor(6, pixels.Color( 255, 155, 0));
pixels.setPixelColor(7, pixels.Color( 255, 155, 0));
pixels.setPixelColor(8, pixels.Color( 255, 155, 0));
pixels.setPixelColor(9, pixels.Color( 255, 155, 0));
pixels.setPixelColor(10, pixels.Color( 255, 155, 0));
pixels.setPixelColor(11, pixels.Color( 255, 155, 0));
pixels.setPixelColor(12, pixels.Color( 255, 155, 0));
pixels.setPixelColor(13, pixels.Color( 255, 155, 0));
pixels.setPixelColor(14, pixels.Color( 255, 155, 0));
pixels.setPixelColor(15, pixels.Color( 255, 155, 0));
pixels.show();
}
//void colorWipe(uint32_t color, int wait) {
//
// for(int i=0; i<16; i++) { // For each pixel in strip...
// pixels.setPixelColor(i, color); // Set pixel's color (in RAM)
// pixels.show(); // Update strip to match
// delay(wait); // Pause for a moment
// }
//}
我知道有些方法在代码重用方面非常糟糕,但我们只是想测试一下什么时候只打开一个 LED,它实际上是正确的。当我们只使用一个 LED 时,我们的数据范围在没有任何连接的情况下达到 90-100。