Skip to content

Instantly share code, notes, and snippets.

@smanning29
Created October 9, 2019 01:45
Show Gist options
  • Save smanning29/a6a3028fce882c267cd37966bceac46d to your computer and use it in GitHub Desktop.
Save smanning29/a6a3028fce882c267cd37966bceac46d to your computer and use it in GitHub Desktop.
Analog Sensor Box
void setup() {
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int potentiometer = analogRead(A0);
// sensor range : 0 to 1023
int photoresistor = analogRead(A1);
// sensor range: 150 to 990
Serial.print(potentiometer);
Serial.print(" ");
Serial.println(photoresistor);
potentiometer = map(potentiometer, 0, 1023, 0, 255);
analogWrite(9, potentiometer);
photoresistor = map(photoresistor, 150, 990, 0, 255);
analogWrite(10, photoresistor);
}
int sensorValue = 0;
float freq = 0;
void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(A0);
Serial.println(sensorValue);
//50 to 1000
int cMaj[] = { 261, 293, 329, 349, 392, 440, 493, 523, 587, 659, 698 };
freq = map(sensorValue, 50, 1000, 100, 1000);
if(sensorValue >= 100 && sensorValue <= 200){
tone(8, cMaj[0], 10);
}
else if(sensorValue >= 201 && sensorValue <= 300){
tone(8, cMaj[1], 10);
}
else if(sensorValue >= 301 && sensorValue <= 400){
tone(8, cMaj[2], 10);
}
else if(sensorValue >= 201 && sensorValue <= 300){
tone(8, cMaj[3], 10);
}
else if(sensorValue >= 301 && sensorValue <= 400){
tone(8, cMaj[4], 10);
}
else if(sensorValue >= 401 && sensorValue <= 500){
tone(8, cMaj[5], 10);
}
else if(sensorValue >= 501 && sensorValue <= 600){
tone(8, cMaj[6], 10);
}
else if(sensorValue >= 601 && sensorValue <= 700){
tone(8, cMaj[7], 10);
}
else if(sensorValue >= 701 && sensorValue <= 800){
tone(8, cMaj[8], 10);
}
else if(sensorValue >= 801 && sensorValue <= 900){
tone(8, cMaj[9], 10);
}
else{
tone(8, cMaj[10], 10);
}
}
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 8
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 14
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int potVal = 0;
int fsVal = 0;
int colorR = 255;
int colorG = 0;
int colorB = 0;
void setup() {
pinMode(6, OUTPUT); //speaker pin
Serial.begin(9600);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop() {
//get input from force sensor
fsVal = analogRead(A0);
Serial.print(fsVal);
Serial.print(" "); //for reading serial monitor
//get input from potentiometer
potVal=analogRead(A1);
Serial.print(potVal);
Serial.print(" "); //for reading serial monitor
//map values to the number of pixels
int m = map(fsVal, 0, 950, 0, strip.numPixels());
Serial.println(m);
//set colors based on potentiometer value
if(potVal >= 0 && potVal <= 170){ //red
colorR = 255;
colorG = 0;
colorB = 0;
}
else if(potVal >= 171 && potVal <= 341){ //orange
colorR = 243;
colorG = 113;
colorB = 37;
}
else if(potVal >= 342 && potVal <= 512){ //yellow
colorR = 255;
colorG = 255;
colorB = 0;
}
else if(potVal >= 513 && potVal <= 683){ //green
colorR = 0;
colorG = 255;
colorB = 0;
}
else if(potVal >= 684 && potVal <= 854){ //blue
colorR = 0;
colorG = 0;
colorB = 255;
}
else{ //purple
colorR = 255;
colorG = 0;
colorB = 255;
}
highStriker(strip.Color(colorR, colorG, colorB), m);
}
void highStriker(uint32_t color, int max){
//function to light up number of LEDS based on a max value
for(int i=0;i<max;i++){
strip.setPixelColor(i, color);
strip.show();
delay(50);
}
if(max==14){
tone(6, 44, 1000);
}
delay(100);
for(int i=max;i>=0;i--){
strip.setPixelColor(i, strip.Color(0,0,0));
strip.show();
delay(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment