Skip to content

Instantly share code, notes, and snippets.

@arduinoboard
Created October 21, 2013 11:28
Show Gist options
  • Save arduinoboard/7082360 to your computer and use it in GitHub Desktop.
Save arduinoboard/7082360 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Uno with a serial number of 6493633313735181C1A2
/*
Code created for reading shake switch sensor and turning on led based on threshold
This sensor has a default digital read
*/
const int numReadings = 3;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int upperReading=1010; // sensor reading from testing sensor. get from serial read that is commented out.
int buttonPin=7; // reading either on or off set with threshold on potentiometer
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 30; // interval at which to blink (milliseconds)
int Thresholdstate = 0; // current state of the button
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int val=0;
void setup()
{
// Serial.begin(9600);
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// read the pushbutton input pin:
Thresholdstate = digitalRead(buttonPin);
// read the value from the sensor:
//the higher the vibration the lover the sensor value
//Readings high 1010 reading low 600
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(sensorPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
// Serial.println(average);
//map(value, fromLow, fromHigh, toLow, toHigh)
val = map(average, upperReading, 700, 0, 255);
// turn the ledPin on the higher the vibration the lower the average.
//digital 0 is when is over threshold set by potentiometer on sensor
while( Thresholdstate == LOW ){ // buttoState=LOW means tha reading is above the threshold so activate led by analogWrite
analogWrite(ledPin,val);
//Serial.println("Val"); // for testing
//Serial.println(val);
Thresholdstate = digitalRead(buttonPin);
}
digitalWrite(ledPin,LOW);// Turn led LOW, turn it off.
Thresholdstate = digitalRead(buttonPin);// read
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment