Pages: [1]   Go Down
Author Topic: Help with Sketch  (Read 677 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
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 Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, there are some mistakes in your code, I will correct a few and you do the rest:
Code:
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 smiley

Also you will have to restore the pins to HIGH, or they will stay LOW You fixed it already smiley
« Last Edit: November 27, 2012, 11:30:48 pm by guix » Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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

Code:
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 Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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. smiley-cool
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also change:
Code:
int sensePin = 0;

to:
Code:
uint8_t sensePin = A0;

Pin 0 is digital pin 0
Pin A0 is analog pin 0

I think that may be your problem smiley

FYI, A0 is defined for each Arduino boards, for example on my Arduino Mega:
Code:
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 Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, there are some mistakes in your code, I will correct a few and you do the rest:
Code:
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 smiley

Also you will have to restore the pins to HIGH, or they will stay LOW You fixed it already smiley


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 Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Man I cant believe how many people are on this site so late at night.

All of them are not from same place. smiley-cool
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, there are some mistakes in your code, I will correct a few and you do the rest:
Code:
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 smiley

Also you will have to restore the pins to HIGH, or they will stay LOW You fixed it already smiley


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  smiley thanks for the help guys
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good smiley-wink
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, thanks I didn't know smiley-wink
Logged

Pages: [1]   Go Up
Jump to: