Offline
Newbie
Karma: 0
Posts: 27
|
 |
« on: November 27, 2012, 11:15:00 pm » |
I just started playing with microcontrollers and I am learning. I'm working on a water tank level indicator and I'm having some trouble figuring it out whats wrong with the sketch. What I am trying to do is have a pot activate pins that correspond to a 7 seg common anode display. Hopefully someone has some time to look at this that would be a big help thanks. int aPin = 13; // Red LED connected to digital pin 13 int bPin = 12; int cPin = 11; int dPin = 10; int ePin = 9; int fPin = 8; int gPin = 7; int hPin = 6; //BI Blanking input LOW turns off segments HIGH normal op.
int sensePin = 0; // select the input pin for the Map
void setup() // run once, when the sketch starts { Serial.begin(9600); analogReference (DEFAULT); pinMode(aPin, OUTPUT); // sets the digital pin as output pinMode(bPin, OUTPUT); pinMode(cPin, OUTPUT); pinMode(dPin, OUTPUT); pinMode(ePin, OUTPUT); pinMode(fPin, OUTPUT); pinMode(gPin, OUTPUT); pinMode(hPin, OUTPUT); //BI Blanking input LOW turns off segments HIGH normal op if used on 7447 driver. }
void loop() { Serial.println(analogRead(sensePin)); delay (200); int val = analogRead(sensePin); if (val >= 1 && val <= 100) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, LOW); if (val >= 101 && val <= 200) digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); digitalWrite(gPin, LOW); if (val >= 201 && val <= 300) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, LOW); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, LOW); digitalWrite(gPin, HIGH); if (val >= 301 && val <= 400) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); digitalWrite(gPin, HIGH); if (val >= 401 && val <= 500) digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, HIGH); digitalWrite(gPin, HIGH); if (val >= 501 && val <= 600) digitalWrite(aPin, HIGH); digitalWrite(bPin, LOW); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, LOW); digitalWrite(fPin, HIGH); digitalWrite(gPin, HIGH); if (val >= 601 && val <= 700) digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, HIGH); if (val >= 701 && val <= 800) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); digitalWrite(gPin, LOW); if (val >= 801 && val <= 900) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, HIGH); if (val >= 901 && val <= 1023) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, HIGH); digitalWrite(gPin, HIGH); }
|
|
|
|
« Last Edit: November 27, 2012, 11:23:34 pm by SAButter »
|
Logged
|
|
|
|
|
Indiana, US
Offline
Full Member
Karma: 12
Posts: 161
|
 |
« Reply #1 on: November 27, 2012, 11:21:24 pm » |
Hi SAButter, and welcome to the forums.
Before we can help, you'll need to explain what the sketch does that is incorrect. What's it doing wrong?
|
|
|
|
|
Logged
|
There are 10 types of people in the world, those that understand binary, and those that don't.
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #2 on: November 27, 2012, 11:21:46 pm » |
if (val >= 1 && val <= 100); digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(cPin, LOW); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); } {
Search for how to use of IF statement
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 623
Scientia potentia est.
|
 |
« Reply #3 on: November 27, 2012, 11:22:34 pm » |
Hello, there are some mistakes in your code, I will correct a few and you do the rest: void loop() { int val = analogRead(sensePin);
Serial.println( val ); delay (200); if (val >= 1 && val <= 100) { digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, LOW); }
else if (val >= 101 && val <= 200) { digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); digitalWrite(gPin, LOW); } //etc etc }
Study the changes, and apply them to the rest of your code  Also you will have to restore the pins to HIGH, or they will stay LOW You fixed it already 
|
|
|
|
« Last Edit: November 27, 2012, 11:30:48 pm by guix »
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #4 on: November 27, 2012, 11:23:53 pm » |
if (val >= 1 && val <= 100); digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(cPin, LOW); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); } {
If you want to do action on val >= 1 && val <= 100 if (val >= 1 && val <= 100) { digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(cPin, LOW); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); }
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #5 on: November 27, 2012, 11:26:47 pm » |
OOOooo Sorry guys I didn't think that I would have a response that fast. I noticed that I posted a newer sketch of what I am working on so I edited my original post and changed the code. Sorry
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #6 on: November 27, 2012, 11:28:30 pm » |
if (val >= 1 && val <= 100) digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, LOW);
again wrong attemp first read comments and apply them. 
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #7 on: November 27, 2012, 11:30:21 pm » |
Hi SAButter, and welcome to the forums.
Before we can help, you'll need to explain what the sketch does that is incorrect. What's it doing wrong?
The leds on the output pins to the segment are all high and not changing with the input state.
|
|
|
|
|
Logged
|
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 623
Scientia potentia est.
|
 |
« Reply #8 on: November 27, 2012, 11:32:56 pm » |
Also change: int sensePin = 0;
to: uint8_t sensePin = A0;
Pin 0 is digital pin 0 Pin A0 is analog pin 0 I think that may be your problem  FYI, A0 is defined for each Arduino boards, for example on my Arduino Mega: static const uint8_t A0 = 54;
So I could replace "A0" by "54", but then it won't work on a different board. See <arduino ide folder>\hardware\arduino\variants\ for pins names and other interesting things
|
|
|
|
« Last Edit: November 27, 2012, 11:40:11 pm by guix »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #9 on: November 27, 2012, 11:34:30 pm » |
Hello, there are some mistakes in your code, I will correct a few and you do the rest: void loop() { int val = analogRead(sensePin);
Serial.println( val ); delay (200); if (val >= 1 && val <= 100) { digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, LOW); }
else if (val >= 101 && val <= 200) { digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); digitalWrite(gPin, LOW); } //etc etc }
Study the changes, and apply them to the rest of your code  Also you will have to restore the pins to HIGH, or they will stay LOW You fixed it already  I will do that Thanks for looking at it Man I cant believe how many people are on this site so late at night.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #10 on: November 27, 2012, 11:36:28 pm » |
Man I cant believe how many people are on this site so late at night. All of them are not from same place. 
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #11 on: November 27, 2012, 11:56:13 pm » |
Hello, there are some mistakes in your code, I will correct a few and you do the rest: void loop() { int val = analogRead(sensePin);
Serial.println( val ); delay (200); if (val >= 1 && val <= 100) { digitalWrite(aPin, HIGH); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, HIGH); digitalWrite(ePin, HIGH); digitalWrite(fPin, HIGH); digitalWrite(gPin, LOW); }
else if (val >= 101 && val <= 200) { digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); digitalWrite(cPin, HIGH); digitalWrite(dPin, LOW); digitalWrite(ePin, LOW); digitalWrite(fPin, LOW); digitalWrite(gPin, LOW); } //etc etc }
Study the changes, and apply them to the rest of your code  Also you will have to restore the pins to HIGH, or they will stay LOW You fixed it already  Thanks a million with your help it's working now. I remember twenty years ago when to school for electronics the coolest new chip that just came out could store 7 seconds of audio. So thing have changed a bit  thanks for the help guys
|
|
|
|
|
Logged
|
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 623
Scientia potentia est.
|
 |
« Reply #12 on: November 28, 2012, 12:18:57 am » |
Good 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: November 28, 2012, 03:10:07 am » |
Pin 0 is digital pin 0 Pin A0 is analog pin 0 It depends on the context - A0 and 0 mean exactly the same thing in the context of analogRead (as is the case here), though they have different numerical values.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
France
Online
God Member
Karma: 19
Posts: 623
Scientia potentia est.
|
 |
« Reply #14 on: November 28, 2012, 03:13:31 am » |
Okay, thanks I didn't know 
|
|
|
|
|
Logged
|
|
|
|
|
|