Trying to use a nano every to provide a 5v output depending on a voltage threshold, that i need to adjust many times to fine tune things.
Some helpful people on another forum helped me with the code, but it didnt quite work how i thought it would
const int analogPin = A0;
const int outputPin = 2;
const int thresholdMin = 0; // Minimum value of the analog input
const int threshold = 1.5; // Trigger voltage
const int thresholdMax = 3.3; // Maximum voltage (adjust as needed)
const int outputMin = LOW; // Output LOW when below threshold
const int outputMax = HIGH; // Output HIGH when above threshold
void setup() {
pinMode(outputPin, OUTPUT);
digitalWrite(outputPin, outputMin); // Set initial output state
}
void loop() {
int analogValue = analogRead(analogPin);
// Map the analog value to the desired threshold range
int actualVoltage = map(analogValue, 0, 1023, thresholdMin, thresholdMax);
if (actualVoltage > threshold) {
digitalWrite(outputPin, outputMax);
} else {
digitalWrite(outputPin, outputMin);
}
}
The trigger voltage didnt seem to do anything, the output would turn on above the ThresholdMax voltage instead,
So i tried changing that to make it do what i wanted, but then the output wouldnt trigger at all,
So i changed it back to 3.3v, and then again it would work, but only above the thresholdmax.
So anytime i try and change the values and upload the sketch, it doesnt do anything, until i change it back to the very first values i used and re-upload.
I did find a few threads on here about nano every's freezing up and needing to reupload the firmware for the "SAMD11"?
as you can tell, i am very much a beginner!!!
It's not clear to me if you have an issue with loading code on the NanoEvery or if there is an error in your use of map( ) with the analogRead values which is confusing you.
The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.
However, map( ) is easily modified to work with floats using this function which you will need to add to your code.
Interesting question but I cannot read thr gibberish you posted. Rather then me trying to figure out what you posted I will help another user. Meanwhile read the forum guidelines especially the part about code tags. I will check later.
That is brilliant thanks, you are spot on,
I started printing actualvoltage and the threshold to the serial monitor and could see that it was only reading as whole numbers, so the code is doing what i want it to, but just not with the resolution/accuracy that i need,
I will try your Floats code tomorrow, that looks like just the ticket!
thankyou very much for helping an absolute beginner!!