Go Down

Topic: Help with Sketch (Read 940 times) previous topic - next topic

SAButter

Nov 28, 2012, 05:15 am Last Edit: Nov 28, 2012, 05:23 am by SAButter Reason: 1
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: [Select]

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);
 
 
}

 

patduino

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?
There are 10 types of people in the world, those that understand binary, and those that don't.

Cybernetician

Code: [Select]

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
From Idea To Invention

guix

#3
Nov 28, 2012, 05:22 am Last Edit: Nov 28, 2012, 05:30 am by guix Reason: 1
Hello, there are some mistakes in your code, I will correct a few and you do the rest:
Code: [Select]

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

Cybernetician

Code: [Select]

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: [Select]

if (val >= 1 && val <= 100)
{
 digitalWrite(aPin, LOW);
 digitalWrite(bPin, LOW);
 digitalWrite(cPin, LOW);
 digitalWrite(dPin, LOW);
 digitalWrite(ePin, LOW);
 digitalWrite(fPin, LOW);
}
From Idea To Invention

SAButter

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

Cybernetician

Code: [Select]

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. 8)
From Idea To Invention

SAButter


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.

guix

#8
Nov 28, 2012, 05:32 am Last Edit: Nov 28, 2012, 05:40 am by guix Reason: 1
Also change:
Code: [Select]

int sensePin = 0;


to:
Code: [Select]

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:
Code: [Select]

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

SAButter


Hello, there are some mistakes in your code, I will correct a few and you do the rest:
Code: [Select]

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.

Cybernetician

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


All of them are not from same place. 8)
From Idea To Invention

SAButter


Hello, there are some mistakes in your code, I will correct a few and you do the rest:
Code: [Select]

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

guix


AWOL

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.
"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.

guix

Okay, thanks I didn't know ;)

Go Up