Hello all
First off I would like to use a Arduino to take a voltage reading on one pin and separate the output to 3 different pins. So by different input voltage to get different output pins low at the same time but no matter what i do the cycle works only the first time and i can not get it to loop.
I have a steering wheel remote that outputs different voltage from 0 to 5 volts depending on the button pressed and i need to transfer that to press some buttons on another board which has buttons short to ground when pressed. i haven't used C in around 10 years so please help me i am at a loss here.
Thank you.
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
for (int pinNumber = 4; pinNumber < 9; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, HIGH);
}
}
void loop() {
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0) * 5.0;
Serial.print("Volts: ");
Serial.println(voltage);
delay(1);
//if (voltage == 2.65) {
if ((voltage > 2.32) && (voltage < 2.54)) {
// UP on:
digitalWrite(4, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
else if ((voltage > 2.94) && (voltage < 3.16)) {
// DOWN on:
digitalWrite(4, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
}
else if ((voltage > 3.55) && (voltage < 3.76)) {
// MODE on:
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
}
delay(100);
}
What is this supposed to do if the voltage is lower than 2.32 or higher than 3.76?
You could put a print statement in the loop just to prove it is looping.
It is supposed to set all outputs HIGH.
What voltage are you seeing reported in your serial output?
The voltage is correct all the time.
I don't immediately see anything wrong with your program. How do you know it is only cycling once?
Try putting in a byte variable the increments on every iteration of loop and print its value.
Maybe try a longer delay() for initial testing?
What happens if you remove the ELSEs ?
When i hook it up to a 0-5 volt source i increment the voltage to 5 volt everethyng works as it should and then back to 0 volt only the middle output works the thirds stays LOW if input is less than 3.55 and also first one stays always LOW
Tested on Duemilanove, Pro mini and Mega.
if i remove the last else if
it works
All the outputs go HIGH in setup. Once any of them go LOW there is nothing that will set them back high if the voltage is below 2.32 or above 3.76. It will just loop continuously with no output. You also gave gaps where the same thing happens at a voltage of 2.55-2.93 and 3.56-3.74.
In each of those cases, the loop will continue with no output and the signals will remain in the last state they were in when the voltage was between one of the sets you check for.
you could add another else if to bring them all high if that is what you want. You could also put a print statement inside that else if that says something like "Not a checked Value" or something similar. With that, you would then have something on the serial monitor guaranteed for every loop.
Robin2:
I have no experience with a DUE. What happens when you try the code on your Mega?
By the way the lines like this
else if ((voltage > 2.32) && (voltage < 2.54)) {
can be simplified to
else if (voltage < 2.54) {
because volts up to 2.32 has already been taken care of by the previous IF
...R
On the Mega is doing OK for 2 outputs but for the third when its HIGH its HIGH and LOW is always between 0 and 3 volts no matter if it is a pwm pin or not
tunebg:
On the Mega is doing OK for 2 outputs but for the third when its HIGH its HIGH and LOW is always between 0 and 3 volts no matter if it is a pwm pin or not
You need to explain that more clearly. When you say "third" do you mean the 3rd IF test
else if ((voltage > 2.54) && (voltage < 2.94)) {
// DOWN on:
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
An Arduino works very very fast by human standards. Just watching the output may not tell you enough to debug the problem. That's why I suggest putting a counter in loop() so you can see whether it is iterating as expected, or whether it is stuck.
"LIGHT BULB MOMENT" ... I suspect your Serial.print() statements are happening too fast and blocking the Mega. Try adding delay(200); at the bottom of loop() and see what happens.
Might want to kick up the Speed of the Serial output. I would think any Arduino can handle much higher rates. I regularly us 115200 on Arduino UNO and Leonardo.
Robin2:
You need to explain that more clearly. When you say "third" do you mean the 3rd IF test
An Arduino works very very fast by human standards. Just watching the output may not tell you enough to debug the problem. That's why I suggest putting a counter in loop() so you can see whether it is iterating as expected, or whether it is stuck.
"LIGHT BULB MOMENT" ... I suspect your Serial.print() statements are happening too fast and blocking the Mega. Try adding delay(200); at the bottom of loop() and see what happens.
...R
I mean if i set 1 output, 2nd output, 3rd output - pins 2, 3, 4 pin 2 goes nuts if i set 4, 7, 8, pin 8 goes nuts if 2, 4, 8 pin 4 goes nuts
i tested even without serial output cause i actualy dont need it and same thing happens. every time i connect a oscilloscope to the outputs cause i need a steady 0v (short to ground) output.
Tomorrow i'll make a new board with a 328p to see if it works or not.
And also i will be very appreciative if you show me how to add a counter in loop(), cause i have no idea.
tunebg:
I mean if i set 1 output, 2nd output, 3rd output - pins 2, 3, 4 pin 2 goes nuts if i set 4, 7, 8, pin 8 goes nuts if 2, 4, 8 pin 4 goes nuts
That probably makes complete sense to you because you are very familiar with your project. But, I'm afraid it is meaningless to me. You need to provide a lot more detail and relate all those things to parts of the program in the style that I did in Reply #12
Adding a counter in loop() is a simple as
void loop() {
static byte loopCounter;
loopCounter ++;
Serial.print("Loop Counter ");
Serial.println(loopCounter);
// other code in loop()
delay(300); // for testing so things don't go too fast to see
}