Go Down

Topic: Help with a Voltage Indication System (Read 2767 times) previous topic - next topic

jreemygear

I'm working on a setup that can detect the voltage from a muscle as it flexes. I have the signal amplification circuit built and working for obtaining the signal from the muscle. The problem I am having is getting my arduino to show, with an array of LEDs, the general voltage level. I have a series of LEDs and resistors wired up to the arduino and , if everything worked properly (it doesn't so far) more LEDs would light up the higher the voltage. I am fairly confident the circuit is solid and believe the programming to be in my code which I have included below. Anyone have any ideas on a solution?

Thanks

Code: [Select]

int ledPin[] = {
  2,3,4,5,6,7,8,9};
void setup()
{
  Serial.begin(9600);
  for(int i = 0; i < 8; i++){         //this is a loop and will repeat eight times
    pinMode(ledPin[i],OUTPUT); //we use this to set each LED pin to output
  }                                   

}
void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue, DEC);
  if (sensorValue = 0) {
    digitalWrite (ledPin[0], LOW);
    digitalWrite (ledPin[1], LOW);
    digitalWrite (ledPin[2], LOW);
    digitalWrite (ledPin[3], LOW);
    digitalWrite (ledPin[4], LOW);
    digitalWrite (ledPin[5], LOW);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 29) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], LOW);
    digitalWrite (ledPin[2], LOW);
    digitalWrite (ledPin[3], LOW);
    digitalWrite (ledPin[4], LOW);
    digitalWrite (ledPin[5], LOW);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 58) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], LOW);
    digitalWrite (ledPin[3], LOW);
    digitalWrite (ledPin[4], LOW);
    digitalWrite (ledPin[5], LOW);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 87) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], HIGH);
    digitalWrite (ledPin[3], LOW);
    digitalWrite (ledPin[4], LOW);
    digitalWrite (ledPin[5], LOW);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 116) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], HIGH);
    digitalWrite (ledPin[3], HIGH);
    digitalWrite (ledPin[4], LOW);
    digitalWrite (ledPin[5], LOW);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 145) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], HIGH);
    digitalWrite (ledPin[3], HIGH);
    digitalWrite (ledPin[4], HIGH);
    digitalWrite (ledPin[5], LOW);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 174) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], HIGH);
    digitalWrite (ledPin[3], HIGH);
    digitalWrite (ledPin[4], HIGH);
    digitalWrite (ledPin[5], HIGH);
    digitalWrite (ledPin[6], LOW);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 203) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], HIGH);
    digitalWrite (ledPin[3], HIGH);
    digitalWrite (ledPin[4], HIGH);
    digitalWrite (ledPin[5], HIGH);
    digitalWrite (ledPin[6], HIGH);
    digitalWrite (ledPin[7], LOW);
} else if (sensorValue >= 232) {
    digitalWrite (ledPin[0], HIGH);
    digitalWrite (ledPin[1], HIGH);
    digitalWrite (ledPin[2], HIGH);
    digitalWrite (ledPin[3], HIGH);
    digitalWrite (ledPin[4], HIGH);
    digitalWrite (ledPin[5], HIGH);
    digitalWrite (ledPin[6], HIGH);
    digitalWrite (ledPin[7], HIGH);
}
}

PaulS

Code: [Select]
  if (sensorValue = 0) {
This assigns the value 0 to sensorValue. If you are going to do this, why are you bothering to read from the sensor. (== is more likely what you want).
The art of getting good answers lies in asking good questions.

graynomad

What is the result of the Serial.print() ?

if (sensorValue = 0) {

Should be ==

What happens for values of 1 to 28?
______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Grumpy_Mike

#3
Nov 30, 2011, 04:25 pm Last Edit: Nov 30, 2011, 04:33 pm by Grumpy_Mike Reason: 1
What is actually happening with the LEDs?

It is a bit rubbish code anyway. you need to learn how to use arrays, with a for loop see this page:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
You need to map the analogue reading to a range of 0 to 8 and then just write out n LOWs and 8-n HIGHs. It will be much shorter.

dxw00d

Anything over 28 is going to satisfy the

Code: [Select]
} else if (sensorValue >= 29) {

So it will never light more than 1 led.

graynomad

I didn't say over 28, 1 to 28.

What happens if you have 5 LEDs on and then get a value of 7?

You've made a good start by having the pin numbers in an array, now you need to clean up the mess and reduce that huge if/else block to something smaller.

If the values aren't critical then something like this

Code: [Select]
val = map (sensorValue, 0, 255, 1, 8);

for (int i = 0; i < 8; i++) {
     if (i < val)
        digitalWrite (ledPin[i], HIGH);
     else
        digitalWrite (ledPin[i], LOW);
}
 



______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

jreemygear

Thanks for the responses so far. Let me go more in depth as to what I am trying to do. When connected to an oscilloscope, the board used for amplification of the muscle's electrical signal returns a value roughly between 0 and 1.2 volts. 1.2 volts is achieved with considerable effort, so that is being considered as the upper boundary with 0 as the lower boundary. The purpose of the LEDs is to serve as a proof of concept and show that muscle movements can be translated into microcontroller commands. The goal of this particular project is to reflect the voltage being registered by lighting more and more LEDs in response to certain voltage thresholds being crossed.

Grumpy_Mike


jreemygear

Haha! That fixed it. It has been a while since I've actively programmed, so the suggestion about the arrays as well as the code snippet are much appreciated.

Go Up