Arduino Programming-Neopixels with Sound Sensor Trigger

Thread Starter

Joel Ong

Joined Nov 23, 2019
8
I am new to Arduino and currently been assigned to do light installation. The led strip should change it colour when the applied pressure varies. Below is the code I have done, please do have a look and give me some advice on whether it will work or not (I have not yet try on the circuit due to some limitations) ?

C:
#include <Adafruit_NeoPixel.h>
#define LED_COUNT 30
#define LED_PIN  6

int sensorPin = A2;
int sensorValue = 0;
int ledValue;

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
pinMode (sensorPin, INPUT);
pinMode (LED_PIN, OUTPUT);
Serial.begin(9600);
strip.begin();
strip.show();
}

void loop() {
sensorValue = analogRead(sensorPin);
int ledValue = map(analogRead(sensorPin), 0, 1023, 0, 255);
Serial.print("sensorPin: ");
Serial.println(sensorValue);
Serial.print("\t ledValue: ");
Serial.println(ledValue);

if (ledValue<60){
strip.fill((255, 255, 255));
strip.show();
}

if (ledValue>=60 && ledValue<=80){
strip.fill((255, 0, 0));
strip.show();
}

else{
strip.fill((0, 0, 255));
strip.show();
}

delay(1000);
}
Moderators note : used code tags
 

djsfantasi

Joined Apr 11, 2010
9,180
A quick glance and I cannot see any glaring problems. I’m not familiar with the neopixel library, so there is an assumption on my part that it’s usage is correct.

In this simple program, the fact that you have two variables sensorvalue and ledvalue is irrelevant. But I don’t see any need to have two separate values. In a different scenario, it might waste memory and time. I would have calculated the constants in your if statements manually and tested only sensorvalue.
 
Top