mirror of
https://gitlab.waag.org/make/fablab/interns/2025/sam.git
synced 2025-08-03 11:54:58 +00:00
docs about normalising potpin
This commit is contained in:
@@ -333,4 +333,71 @@ After flashing it onto the microcontroller I could test it.
|
||||
I've noticed there are some ghost values when it needs to be 0 so I need a small area where it's always 0. The upper side of the Potentiometer is always 4095 thats the max value so that's good.
|
||||
|
||||
##### Fixing the ghost measurements
|
||||
Im going to map everything lower than 80 to 80 using software. The reason im doing 80 is because then it will keep on counting up from 80 instead of jumping from 0 to 81 and I didn't see any ghost readings above 80.
|
||||
Im going to map everything lower than 80 to 80 using software. The reason im doing 80 is because then it will keep on counting up from 80 instead of jumping from 0 to 81 and I didn't see any ghost readings above 80.
|
||||
|
||||
Luckily there is an arduino function called `map` that can do this. [Source](https://docs.arduino.cc/language-reference/en/functions/math/map/). The syntax is like this `map(value, fromLow, fromHigh, toLow, toHigh)` so it's really easy to use. This is how I implemented it.
|
||||
|
||||
During coding I realized this could be done way easier and simpler. So this is my result
|
||||
|
||||
```cpp
|
||||
#include <Arduino.h>
|
||||
|
||||
int pot1 = 0;
|
||||
int Mpot1 = 0; //mapped pot
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
pinMode(0, INPUT);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
pot1 = analogRead(0); //store potmeter value in pot1
|
||||
|
||||
if (pot1 <= 80){ //check if pot1 is lower than 80, if yes make it 80
|
||||
Mpot1 = 80;
|
||||
}
|
||||
else {
|
||||
Mpot1 = pot1; //if pot1 is above 80 just copy over pot1 to Mpot1
|
||||
}
|
||||
Serial.println(Mpot1);
|
||||
}
|
||||
```
|
||||
This code works and makes any value under 80 automatically 80. And keeps all values above the original value.
|
||||
|
||||
I've cleaned it up a bit and turned it into a function so I don't have to copy paste code later on and so it stays clean.
|
||||
Result:
|
||||
??? example
|
||||
```cpp
|
||||
#include <Arduino.h>
|
||||
// declarations
|
||||
int normalizePot(int pin, int minValue);
|
||||
|
||||
// constants
|
||||
const int potPin1 = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(potPin1, INPUT);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
Serial.println(normalizePot(potPin1, 80));
|
||||
}
|
||||
|
||||
int normalizePot(int pin, int minValue)
|
||||
{
|
||||
int pot = analogRead(pin);
|
||||
|
||||
if (pot <= minValue)
|
||||
{
|
||||
return 80;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pot;
|
||||
}
|
||||
}
|
||||
```
|
@@ -1,13 +1,32 @@
|
||||
#include <Arduino.h>
|
||||
// declarations
|
||||
int normalizePot(int pin, int minValue);
|
||||
|
||||
// constants
|
||||
const int potPin1 = 0;
|
||||
|
||||
void setup(){
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(0, INPUT);
|
||||
pinMode(potPin1, INPUT);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
Serial.println(analogRead(0));
|
||||
void loop()
|
||||
{
|
||||
|
||||
Serial.println(normalizePot(potPin1, 80));
|
||||
}
|
||||
|
||||
int normalizePot(int pin, int minValue)
|
||||
{
|
||||
int pot = analogRead(pin);
|
||||
|
||||
if (pot <= minValue)
|
||||
{
|
||||
return 80;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pot;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user