Skip to content

Instantly share code, notes, and snippets.

@goran-mahovlic
Last active September 16, 2024 11:12
Show Gist options
  • Save goran-mahovlic/a1edf66aa3860e67ebb322ece97c953e to your computer and use it in GitHub Desktop.
Save goran-mahovlic/a1edf66aa3860e67ebb322ece97c953e to your computer and use it in GitHub Desktop.
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on the value obtained
by analogRead().
The circuit:
- potentiometer
center pin of the potentiometer to the analog input 0
one side pin (either one) to ground
the other side pin to +5V
- LED
anode (long leg) attached to digital output 13 through 220 ohm resistor
cathode (short leg) attached to ground
- Note: because most Arduinos have a built-in LED attached to pin 13 on the
board, the LED is optional.
created by David Cuartielles ¸
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/
#include "QuickMedianLib.h"
int sensorPinA0 = A0; // select the input pin for the potentiometer
int sensorPinA1 = A1; // select the input pin for the potentiometer
int sensorPinA2 = A2; // select the input pin for the potentiometer
int sensorPinA3 = A3; // select the input pin for the potentiometer
int sensorPinD2 = 2; // select the input pin for the potentiometer
int sensorPinD3 = 3; // select the input pin for the potentiometer
int sensorPinD4 = 4; // select the input pin for the potentiometer
int sensorPinD5 = 5; // select the input pin for the potentiometer
int sensorPinD6 = 6; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorPinA0Value = 0; // variable to store the value coming from the sensor
int sensorPinA1Value = 0; // variable to store the value coming from the sensor
int sensorPinA2Value = 0; // variable to store the value coming from the sensor
int sensorPinA3Value = 0; // variable to store the value coming from the sensor
int values100A0[200] = { 0 };
int values100A1[200] = { 0 };
int values100A2[200] = { 0 };
int values100A3[200] = { 0 };
int values100A0Length = sizeof(values100A0) / sizeof(int);
int values100A1Length = sizeof(values100A1) / sizeof(int);
int values100A2Length = sizeof(values100A2) / sizeof(int);
int values100A3Length = sizeof(values100A3) / sizeof(int);
int mapedSensorPinA0Value;
int mapedSensorPinA1Value;
int mapedSensorPinA2Value;
int mapedSensorPinA3Value;
bool calibrated = false;
int minValA0;
int minValA1;
int minValA2;
int minValA3;
int maxValA0;
int maxValA1;
int maxValA2;
int maxValA3;
int oldFoilNumber = 0;
int trasholdValue = 50;
bool samplingFoilDone = false;
int foilChanged = 0;
int maxFoilCount = 3;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(sensorPinD2, OUTPUT);
pinMode(sensorPinD3, OUTPUT);
pinMode(sensorPinD4, OUTPUT);
pinMode(sensorPinD5, OUTPUT);
pinMode(sensorPinD5, OUTPUT);
digitalWrite(sensorPinD2, LOW);
digitalWrite(sensorPinD3, LOW);
digitalWrite(sensorPinD4, LOW);
digitalWrite(sensorPinD5, LOW);
digitalWrite(sensorPinD6, LOW);
Serial.begin(115200);
}
void mapSensors(){
mapedSensorPinA0Value = map(sensorPinA0Value, minValA0-100, 1023, 0, 1000);
mapedSensorPinA1Value = map(sensorPinA1Value, minValA1-100, 1023, 0, 1000);
mapedSensorPinA2Value = map(sensorPinA2Value, minValA2-100, 1023, 0, 1000);
mapedSensorPinA3Value = map(sensorPinA3Value, minValA3-100, 1023, 0, 1000);
}
void calibrate(){
for (int i = 0; i < (sizeof(values100A0) / sizeof(values100A0[0])); i++) {
maxValA0 = max(values100A0[i],maxValA0);
minValA0 = min(values100A0[i],minValA0);
}
for (int i = 0; i < (sizeof(values100A1) / sizeof(values100A1[0])); i++) {
maxValA1 = max(values100A1[i],maxValA1);
minValA1 = min(values100A1[i],minValA1);
}
for (int i = 0; i < (sizeof(values100A2) / sizeof(values100A2[0])); i++) {
maxValA2 = max(values100A2[i],maxValA2);
minValA2 = min(values100A2[i],minValA2);
}
for (int i = 0; i < (sizeof(values100A3) / sizeof(values100A3[0])); i++) {
maxValA3 = max(values100A3[i],maxValA3);
minValA3 = min(values100A3[i],minValA3);
}
calibrated = true;
}
void getValues(){
for (int i=0;i<200;i++){
values100A0[i] = analogRead(sensorPinA0);
delay(1);
}
for (int i=0;i<200;i++){
values100A1[i] = analogRead(sensorPinA1);
delay(1);
}
for (int i=0;i<200;i++){
values100A2[i] = analogRead(sensorPinA2);
delay(1);
}
for (int i=0;i<200;i++){
values100A3[i] = analogRead(sensorPinA3);
delay(1);
}
sensorPinA0Value = QuickMedian<int>::GetMedian(values100A0, values100A0Length);
sensorPinA1Value = QuickMedian<int>::GetMedian(values100A1, values100A1Length);
sensorPinA2Value = QuickMedian<int>::GetMedian(values100A2, values100A2Length);
sensorPinA3Value = QuickMedian<int>::GetMedian(values100A3, values100A3Length);
// print the results to the Serial Monitor:
}
void loop() {
getValues();
if(!calibrated){
calibrate();
}
//mapSensors();
/*
Serial.print("sensorA0 = ");
Serial.println(sensorPinA0Value);
Serial.print("sensorA1 = ");
Serial.println(sensorPinA1Value);
Serial.print("sensorA2 = ");
Serial.println(sensorPinA2Value);
Serial.print("sensorA3 = ");
Serial.println(sensorPinA3Value);
*/
// stop the program for for <sensorValue> milliseconds:
int foilNumber = 0;
if(sensorPinA0Value > (maxValA0 + trasholdValue)){
foilNumber = foilNumber + 1;
}
if(sensorPinA1Value > (maxValA1 + trasholdValue)){
foilNumber = foilNumber + 2;
}
if(sensorPinA2Value > (maxValA2 + trasholdValue)){
foilNumber = foilNumber + 4;
}
if(sensorPinA3Value > (maxValA3 + trasholdValue)){
foilNumber = foilNumber + 8;
}
if (oldFoilNumber != foilNumber){
foilChanged++;
Serial.println(20);
}
if (foilChanged > maxFoilCount){
samplingFoilDone = true;
foilChanged = 0;
}
if(samplingFoilDone){
Serial.println(foilNumber);
oldFoilNumber = foilNumber;
samplingFoilDone = false;
}
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(200);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment