Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Ok, more programming help........
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Ok, more programming help........

by cashman04 on Sun Nov 28, 2010 2:41 pm

I think I am doing pretty well for only having my arduino for 4 days now. But I got stuck this morning again.
So I have been playing with 7-segment displays(single digit).
Yesterday I had one hooked up through a shift register and was displaying a number(0-9) based on the input from a pot. And then I changed the pot out for a photoresistor, and mapped the values and had the same thing.

Then I decided to try multiplexing, with the shift register. I got it hooked up and was displaying numbers in no time for me. Now I decided let's take it a step further and have both of those inputs, each driving a different digit on the multiplexed display. Here is where I am running into my problem.

I am running a bunch of ifs and else ifs. I need a way to set a variable under each of those to set a value = zero if the pot = 0, or set value = 1 if between 1 and 149. and so forth.
I need the "value" to be the same name for whatever number it ends up being.

The way I have it set up I get an error saying value1 isn't declared in this scope. So I don't know if that means I can't do this at all the way I want to.
SInce I am using a shift register, i have to shift out bits manually. I was doing fine when I was just shifting out B11101110 or the like. But now I need to have a dynamic variable set up in that line of code so it will read shiftout blah blah, value1); and go to read that value1 = zero, or two, or whatever it is at the time depending on the if's.

The reason I am doing it this way is cause of the timing involved with multiplexing displays. I thought it would be easier if i just had one bit of code dedicated to writing the displays all the time and switching back and forth between them, and having the numbers to be displayed be a variable inside of this bit of code. That way I don't have to try and write to the displays after every if and else if instance.

I hope that makes any sense to anyone. Again, I have never messed with programming before I received my arduino 4 days ago. So I am sure Ihave missed many basic things, like setting variables....

Here's my code.




Code: Select all | TOGGLE FULL SIZE
//2 7-segment displays multiplexed run through a tilt shift register
//input from pot displaying from 0-9 on display1
//input from photoresistor displaying 0-9 on display 2


int latchPin = 4;
int clockPin = 3;
int dataPin = 2;
int sensePin1 = 0; // analog input from pot
int sensorValue = 0;
int sensePin2 = 1; // analog input from photoresistor
int gnd1 = 12;
int gnd2 = 13;
int timer = 500;

byte zero  = B10111011;
byte one   = B00001010;
byte two   = B01110011;
byte three = B01011011;
byte four  = B11001010;
byte five  = B11011001;
byte six   = B11111000;
byte seven = B00001011;
byte eight = B11111011;
byte nine  = B11011011;


void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(gnd1, OUTPUT);
  pinMode(gnd2, OUTPUT);
 
 
}

void loop() {
 
  int value3 = analogRead(sensePin1);
  int value4 = analogRead(sensePin2);
  Serial.println(value3);  // so I can read optput and map values later accordingly
  Serial.println(value4);  //ditto
 
 
  for (int i=0; i<timer; i++){
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, zero);
    digitalWrite(latchPin, HIGH);
    digitalWrite(gnd1, LOW);
    delay(1);
    digitalWrite(gnd1, HIGH);
   
   
     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, one);
    digitalWrite(latchPin, HIGH);
    digitalWrite(gnd2, LOW);
    delay(1);
    digitalWrite(gnd2, HIGH);}
 
 
 
 
 
  if(analogRead(sensePin1) >= 0){
  int value1 = zero;}      //HERE'S THE PROBLEM I'M HAVING
  else if((analogRead(sensePin1) >= 1) && (analogRead(sensePin1) < 149) ){
  int value1 = one;  }    //DITTO
  else if((analogRead(sensePin1) >= 150) && (analogRead(sensePin1) < 249) ){
  int value1 = two;}      //AND SO FORTH
  else if((analogRead(sensePin1) >= 250) && (analogRead(sensePin1) < 349) ){
  int value1 = three;}
  else if((analogRead(sensePin1) >= 350) && (analogRead(sensePin1) < 449) ){
  int value1 = four;}
  else if((analogRead(sensePin1) >= 450) && (analogRead(sensePin1) < 549) ){
  int value1 = five;}
  else if((analogRead(sensePin1) >= 550) && (analogRead(sensePin1) < 649) ){
  int value1 = six;}
  else if((analogRead(sensePin1) >= 650) && (analogRead(sensePin1) < 749) ){
  int value1 = seven;}
  else if((analogRead(sensePin1) >= 750) && (analogRead(sensePin1) < 849) ){
  int value1 = eight;}
  else if((analogRead(sensePin1) >= 850) && (analogRead(sensePin1) < 1050) ){
  int value1 = nine;}




  if(analogRead(sensePin2) >= 0){
  int value2= zero;}
  else if((analogRead(sensePin2) >= 1) && (analogRead(sensePin2) < 149) ){
  int value2 = one;  }
  else if((analogRead(sensePin2) >= 150) && (analogRead(sensePin2) < 249) ){
  int value2 = two;}
  else if((analogRead(sensePin2) >= 250) && (analogRead(sensePin2) < 349) ){
  int value2 = three;}
  else if((analogRead(sensePin2) >= 350) && (analogRead(sensePin2) < 449) ){
  int value2 = four;}
  else if((analogRead(sensePin2) >= 450) && (analogRead(sensePin2) < 549) ){
  int value2 = five;}
  else if((analogRead(sensePin2) >= 550) && (analogRead(sensePin2) < 649) ){
  int value2 = six;}
  else if((analogRead(sensePin2) >= 650) && (analogRead(sensePin2) < 749) ){
  int value2 = seven;}
  else if((analogRead(sensePin2) >= 750) && (analogRead(sensePin2) < 849) ){
  int value2 = eight;}
  else if((analogRead(sensePin2) >= 850) && (analogRead(sensePin2) < 1050) ){
  int value2 = nine;}

}
     

 
   
 




cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by oPossum on Sun Nov 28, 2010 3:12 pm

Don't call analogRead multiple times. The value may change from call to call.
Don't declare value1 & value2 multiple times.
When one if is true, the following else-if will be skipped. No need to test the lower bound again.
Use const to declare variables that will not change.

Code: Select all | TOGGLE FULL SIZE
//2 7-segment displays multiplexed run through a tilt shift register
//input from pot displaying from 0-9 on display1
//input from photoresistor displaying 0-9 on display 2


const int latchPin = 4;
const int clockPin = 3;
const int dataPin = 2;
const int sensePin1 = 0; // analog input from pot
const int sensePin2 = 1; // analog input from photoresistor
const int gnd1 = 12;
const int gnd2 = 13;
const int timer = 500;

const byte zero  = B10111011;
const byte one   = B00001010;
const byte two   = B01110011;
const byte three = B01011011;
const byte four  = B11001010;
const byte five  = B11011001;
const byte six   = B11111000;
const byte seven = B00001011;
const byte eight = B11111011;
const byte nine  = B11011011;


void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(gnd1, OUTPUT);
  pinMode(gnd2, OUTPUT);
}

void loop() {

  const int value3 = analogRead(sensePin1);
  const int value4 = analogRead(sensePin2);
  Serial.println(value3);  // so I can read optput and map values later accordingly
  Serial.println(value4);  //ditto
 
 
  for (int i=0; i<timer; i++){
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, zero);
    digitalWrite(latchPin, HIGH);
    digitalWrite(gnd1, LOW);
    delay(1);
    digitalWrite(gnd1, HIGH);
   
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, one);
    digitalWrite(latchPin, HIGH);
    digitalWrite(gnd2, LOW);
    delay(1);
    digitalWrite(gnd2, HIGH);}

    const int an1 = analogRead(sensePin1);
    int value1;
    if(an1 < 1){               // 0 or negative
        value1 = zero;
    } else if(an1 < 150) {      // 1 to 149
        value1 = one;
    } else if(an1 < 250) ) {    // 150 to 249
        value1 = two;
    } else if(an1 < 350) ) {    // 250 to 349
        value1 = three;
    } else if(an1 < 450) ) {    // 350 to 449
        value1 = four;
    } else if(an1 < 550) ) {    // 450 to 549
        value1 = five;
    } else if(an1 < 650) ) {    // 550 to 649
        value1 = six;
    } else if(an1 < 750) ) {    // 650 to 749
        value1 = seven;
    } else if(an1 < 850) ) {    // 750 to 849
        value1 = eight;
    } else if(an1 < 1050) ) {   // 850 to 1049
        int value1 = nine;
    } else {                    // >= 1050
        value1 = 0;
    }

    const int an2 = analogRead(sensePin2);
    int value2
    if(an2 < 1){
        value2 = zero;
    } else if(an2 < 150) {
        value2 = one;
    } else if(an2 < 250) ) {
        value2 = two;
    } else if(an2 < 350) ) {
        value2 = three;
    } else if(an2 < 450) ) {
        value2 = four;
    } else if(an2 < 550) ) {
        value2 = five;
    } else if(an2 < 650) ) {
        value2 = six;
    } else if(an2 < 750) ) {
        value2 = seven;
    } else if(an1 < 850) ) {
        value2 = eight;
    } else if(an2 < 1050) ) {
        int value2 = nine;
    } else {
        value2 = 0;
    }
}
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Re: Ok, more programming help........

by oPossum on Sun Nov 28, 2010 3:32 pm

Create functions to eliminate duplicate code and provide clarity.

Code: Select all | TOGGLE FULL SIZE

int map_analog(const int an)
    if(an < 1){                // 0 or negative
        return zero;
    } else if(an < 150) {      // 1 to 149
        return one;
    } else if(an < 250) ) {    // 150 to 249
        return two;
    } else if(an < 350) ) {    // 250 to 349
        return three;
    } else if(an < 450) ) {    // 350 to 449
        return four;
    } else if(an < 550) ) {    // 450 to 549
        return five;
    } else if(an < 650) ) {    // 550 to 649
        return six;
    } else if(an < 750) ) {    // 650 to 749
        return seven;
    } else if(an < 850) ) {    // 750 to 849
        return eight;
    } else if(an < 1050) ) {   // 850 to 1049
        return nine;
    } else {                    // >= 1050
        return 0;
    }
}

void loop(void) {

   // ...

   const int value1 = map_analog( AnalogRead(sensePin1) );

   const int value2 = map_analog( AnalogRead(sensePin2) );

   // ...
}
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Re: Ok, more programming help........

by stinkbutt on Sun Nov 28, 2010 4:55 pm

cashman04 wrote:I think I am doing pretty well for only having my arduino for 4 days now. But I got stuck this morning again.
So I have been playing with 7-segment displays(single digit).
Yesterday I had one hooked up through a shift register and was displaying a number(0-9) based on the input from a pot. And then I changed the pot out for a photoresistor, and mapped the values and had the same thing.

Then I decided to try multiplexing, with the shift register. I got it hooked up and was displaying numbers in no time for me. Now I decided let's take it a step further and have both of those inputs, each driving a different digit on the multiplexed display. Here is where I am running into my problem.

I am running a bunch of ifs and else ifs. I need a way to set a variable under each of those to set a value = zero if the pot = 0, or set value = 1 if between 1 and 149. and so forth.
I need the "value" to be the same name for whatever number it ends up being.

The way I have it set up I get an error saying value1 isn't declared in this scope. So I don't know if that means I can't do this at all the way I want to.
SInce I am using a shift register, i have to shift out bits manually. I was doing fine when I was just shifting out B11101110 or the like. But now I need to have a dynamic variable set up in that line of code so it will read shiftout blah blah, value1); and go to read that value1 = zero, or two, or whatever it is at the time depending on the if's.

The reason I am doing it this way is cause of the timing involved with multiplexing displays. I thought it would be easier if i just had one bit of code dedicated to writing the displays all the time and switching back and forth between them, and having the numbers to be displayed be a variable inside of this bit of code. That way I don't have to try and write to the displays after every if and else if instance.

I hope that makes any sense to anyone. Again, I have never messed with programming before I received my arduino 4 days ago. So I am sure Ihave missed many basic things, like setting variables....

Here's my code.

Code: Select all | TOGGLE FULL SIZE
//2 7-segment displays multiplexed run through a tilt shift register
//input from pot displaying from 0-9 on display1
//input from photoresistor displaying 0-9 on display 2


int latchPin = 4;
int clockPin = 3;
int dataPin = 2;
int sensePin1 = 0; // analog input from pot
int sensorValue = 0;
int sensePin2 = 1; // analog input from photoresistor
int gnd1 = 12;
int gnd2 = 13;
int timer = 500;

byte zero  = B10111011;
byte one   = B00001010;
byte two   = B01110011;
byte three = B01011011;
byte four  = B11001010;
byte five  = B11011001;
byte six   = B11111000;
byte seven = B00001011;
byte eight = B11111011;
byte nine  = B11011011;


void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(gnd1, OUTPUT);
  pinMode(gnd2, OUTPUT);
 
 
}

void loop() {
 
  int value3 = analogRead(sensePin1);
  int value4 = analogRead(sensePin2);
  Serial.println(value3);  // so I can read optput and map values later accordingly
  Serial.println(value4);  //ditto
 
 
  for (int i=0; i<timer; i++){
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, zero);
    digitalWrite(latchPin, HIGH);
    digitalWrite(gnd1, LOW);
    delay(1);
    digitalWrite(gnd1, HIGH);
   
   
     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, one);
    digitalWrite(latchPin, HIGH);
    digitalWrite(gnd2, LOW);
    delay(1);
    digitalWrite(gnd2, HIGH);}

 
  if(analogRead(sensePin1) >= 0){
  int value1 = zero;}      //HERE'S THE PROBLEM I'M HAVING
  else if((analogRead(sensePin1) >= 1) && (analogRead(sensePin1) < 149) ){
  int value1 = one;  }    //DITTO
  else if((analogRead(sensePin1) >= 150) && (analogRead(sensePin1) < 249) ){
  int value1 = two;}      //AND SO FORTH
  else if((analogRead(sensePin1) >= 250) && (analogRead(sensePin1) < 349) ){
  int value1 = three;}
  else if((analogRead(sensePin1) >= 350) && (analogRead(sensePin1) < 449) ){
  int value1 = four;}
  else if((analogRead(sensePin1) >= 450) && (analogRead(sensePin1) < 549) ){
  int value1 = five;}
  else if((analogRead(sensePin1) >= 550) && (analogRead(sensePin1) < 649) ){
  int value1 = six;}
  else if((analogRead(sensePin1) >= 650) && (analogRead(sensePin1) < 749) ){
  int value1 = seven;}
  else if((analogRead(sensePin1) >= 750) && (analogRead(sensePin1) < 849) ){
  int value1 = eight;}
  else if((analogRead(sensePin1) >= 850) && (analogRead(sensePin1) < 1050) ){
  int value1 = nine;}

  if(analogRead(sensePin2) >= 0){
  int value2= zero;}
  else if((analogRead(sensePin2) >= 1) && (analogRead(sensePin2) < 149) ){
  int value2 = one;  }
  else if((analogRead(sensePin2) >= 150) && (analogRead(sensePin2) < 249) ){
  int value2 = two;}
  else if((analogRead(sensePin2) >= 250) && (analogRead(sensePin2) < 349) ){
  int value2 = three;}
  else if((analogRead(sensePin2) >= 350) && (analogRead(sensePin2) < 449) ){
  int value2 = four;}
  else if((analogRead(sensePin2) >= 450) && (analogRead(sensePin2) < 549) ){
  int value2 = five;}
  else if((analogRead(sensePin2) >= 550) && (analogRead(sensePin2) < 649) ){
  int value2 = six;}
  else if((analogRead(sensePin2) >= 650) && (analogRead(sensePin2) < 749) ){
  int value2 = seven;}
  else if((analogRead(sensePin2) >= 750) && (analogRead(sensePin2) < 849) ){
  int value2 = eight;}
  else if((analogRead(sensePin2) >= 850) && (analogRead(sensePin2) < 1050) ){
  int value2 = nine;}
}


First of all, to fix the value1 out of scope issue, just declare value1 outside of the main loop:

Code: Select all | TOGGLE FULL SIZE
int latchPin = 4;
int clockPin = 3;
int dataPin = 2;
int sensePin1 = 0; // analog input from pot
int sensorValue = 0;
int sensePin2 = 1; // analog input from photoresistor
int gnd1 = 12;
int gnd2 = 13;
int timer = 500;

int value1 = 0;           // DECLARE value1 HERE TO KEEP IT GLOBAL.


That way, it's visible to all segments of the program. The way you're declaring it, it exists only within the if-then block and then goes away once the block ends. Keep in mind, now you just reference value1, you don't reference int value1. So this code works:

Code: Select all | TOGGLE FULL SIZE
value1 = four;


BUT THIS CODE will not:

Code: Select all | TOGGLE FULL SIZE
int value1 = four;

That code won't compile because you're attempting to declare value1 twice.

However, it's wasteful to declare a whole big pile of "one", "two", "three" variables when you can just declare a byte[] array. So instead declare the font table like this instead:

Code: Select all | TOGGLE FULL SIZE
int timer = 500;

int value1 = 0;

byte[] font_table =
{
B10111011,
B00001010,
B01110011,
B01011011,
B11001010,
B11011001,
B11111000,
B00001011,
B11111011,
B11011011,
}


Now, instead of calling variable zero you just call font_table[0], and instead of calling variable seven you just call font_table[7].

Finally, you can do integer division to simplify some of the code later on:

This is what you started with:
Code: Select all | TOGGLE FULL SIZE
if(analogRead(sensePin1) >= 0){
  int value1 = zero;}      //HERE'S THE PROBLEM I'M HAVING
  else if((analogRead(sensePin1) >= 1) && (analogRead(sensePin1) < 149) ){
  int value1 = one;  }    //DITTO
  else if((analogRead(sensePin1) >= 150) && (analogRead(sensePin1) < 249) ){
  int value1 = two;}      //AND SO FORTH
  else if((analogRead(sensePin1) >= 250) && (analogRead(sensePin1) < 349) ){
  int value1 = three;}
  else if((analogRead(sensePin1) >= 350) && (analogRead(sensePin1) < 449) ){
  int value1 = four;}
  else if((analogRead(sensePin1) >= 450) && (analogRead(sensePin1) < 549) ){
  int value1 = five;}
  else if((analogRead(sensePin1) >= 550) && (analogRead(sensePin1) < 649) ){
  int value1 = six;}
  else if((analogRead(sensePin1) >= 650) && (analogRead(sensePin1) < 749) ){
  int value1 = seven;}
  else if((analogRead(sensePin1) >= 750) && (analogRead(sensePin1) < 849) ){
  int value1 = eight;}
  else if((analogRead(sensePin1) >= 850) && (analogRead(sensePin1) < 1050) ){
  int value1 = nine;}


This is how you do it with integer division:
Code: Select all | TOGGLE FULL SIZE
value1 = (analogRead(sensePin1) / 150) + 1;      // You're done.


Much more elegant, huh? One line of code, no waiting. Now you see why I suggested the font_table variable? It lets you VASTLY simplify your code. Oh, there's also an error in the first line of that if/else block, though not one that will stop it from compiling:

Code: Select all | TOGGLE FULL SIZE
if(analogRead(sensePin1) >= 0){        // STOP - STOP RIGHT HERE.  You will NEVER get past this part of the if/else.


  int value1 = zero;}
  else if((analogRead(sensePin1) >= 1) && (analogRead(sensePin1) < 149) ){
// etc. etc. etc...


You will never reach any of the else statements because analogRead > 0. The others require that the value of analogRead are > 1, > 150, > 300, etc... If it is, then it's ALREADY qualified for the first if test of >= 0, so you'll never see those values.
Last edited by stinkbutt on Sun Nov 28, 2010 5:01 pm, edited 3 times in total.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by cashman04 on Sun Nov 28, 2010 4:58 pm

Ok, first off, thanks for taking time to help and teach me this stuff.

Right now, I'm going through your code. And I think I understand most of what you are saying. Im just gonna write tit out to make sure I am understanding .

First off by doing this-
Code: Select all | TOGGLE FULL SIZE
const int an1 = analogRead(sensePin1);

I'm assuming what that does is read the analog input once, and then declare that to an1, so as it doesn't read the analog input but once for that run of code, therefore keeping it the same for all the if's in the case that it changes by the end. Right?

And on your second code. I'm a bit confused.
Actually after trying to type my questions I think I am a bit more than confused. My best guess on this is that to get rid of duplicate code (running all the if's twice, once for each analog input) it is a function to run both analog inputs through at the same time, and it sets the returned value to const value1 or const value2.

If this is the case, doesn't setting them as const values mean that they can't change and are read only? When the analog input changes, will and there is a different returned value, will it be allowed to change the const value1 or 2?

And I havn't seen this yet
Code: Select all | TOGGLE FULL SIZE
void loop(void) {


Does this go after the original void loop? I'm just not sure what or where the code after that goes. Im assuming the rest from above that goes in the regular void loop?

And I still have the problem of calling to value 1 or 2

Code: Select all | TOGGLE FULL SIZE
digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin,MSBFIRST, const int value1);
    digitalWrite(latchPin, HIGH);


Basically all that is just to call to a binary code to write to the shift register. value2 = zero = b10010100. But in the line of code above, it still gives me an error- expected primary-expression before 'int'. Same if I just put int value1. So how do I go about writing that out in the shiftOut()

Again, sorry if this may be frustrating. I have 0 programming experience before a few days ago. I think I'm picking it up pretty well, but I'm sure I have missed a lot of basics.
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Sun Nov 28, 2010 5:07 pm

Don't declare an1 as a constant. I don't know why possum told you to do that, but the next time you attempt to analogRead() and put it into an1 it'll fail. Just don't worry about that whole "const" thing at all right now. It has it's place, but right now it's not really helping you and it's sometimes hard to tell when variables should be constants with the const keyword, and when they shouldn't.

Also, you don't declare variables inside a function call. This code:

Code: Select all | TOGGLE FULL SIZE
digitalWrite(latchPin, LOW);
        shiftOut(dataPin, clockPin,MSBFIRST, const int value1);
        digitalWrite(latchPin, HIGH);


This code won't work. You just PASS IT value1, you don't declare a new int, value1, inside the function call.

Here's a quick explanation. Before you USE a variable, you need to DECLARE it. That means the code works like this:

Code: Select all | TOGGLE FULL SIZE
int x;          // I've declared the variable here.  It exists, but it has not had a value assigned to it yet.
int y;          // ibid
int z;          // ibid

void setup()
{
x = 10;             // Now I've given the three variables values
y = 20;
z = 50;
}

void loop()
{
y = y + x;                       // Here we loop & work with the variables.
z = z - 1;
}
Last edited by stinkbutt on Sun Nov 28, 2010 5:12 pm, edited 1 time in total.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by stinkbutt on Sun Nov 28, 2010 5:09 pm

By the way, you're doing pretty well so far considering that you've no programming experience. You're just getting tripped up on a couple of minor bookkeeping details.

Check this page out for more help on working with variables. Pay close attention to WHERE they declare their variables:

http://www.arduino.cc/en/Tutorial/Variables
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by cashman04 on Sun Nov 28, 2010 5:11 pm

Wow,

Code: Select all | TOGGLE FULL SIZE
value1 = (analogRead(sensePin1) / 150) + 1;


Ya, I'd say thats a bit more elegant.

My only question on that though is this-

Ok, so lets go through the math of that code-

sensePin1=150

150/150 =1

1+1=2

value1 = 2

Now that does get me the number I want=2. But I don't know how to out put the number 2 directly to the 7- segment display.

I have to output B01110011 so it will display the number 2.
Now finally my question-
How can I do my -
Code: Select all | TOGGLE FULL SIZE
shiftOut(dataPin, clockPin,MSBFIRST,  value1);

shift out code so that it knows to output B01110011 when value1 = 2 ?
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by cashman04 on Sun Nov 28, 2010 5:18 pm

Also, Maybe I am doing this one wrong, but this
Code: Select all | TOGGLE FULL SIZE
byte[] font_table =
{
B10111011,
B00001010,
B01110011,
B01011011,
B11001010,
B11011001,
B11111000,
B00001011,
B11111011,
B11011011,
}


keeps giving me an error now, expected unqualified-id before '{'

I was assuming that that bit goes before my loop or setup, where I declare all the variables.
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by oPossum on Sun Nov 28, 2010 5:32 pm

Code: Select all | TOGGLE FULL SIZE
const byte font_table[] =
{
   B10111011,
   B00001010,
   B01110011,
   B01011011,
   B11001010,
   B11011001,
   B11111000,
   B00001011,
   B11111011,
   B11011011,
};

byte map_analog(const int an)
{
   const int n = (an + 149) / 150;
   if((n < 0) || (n >= (sizeof(font_table) / sizeof(font_table[0]))) return 0;
   return font_table[n];
}
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Re: Ok, more programming help........

by oPossum on Sun Nov 28, 2010 5:35 pm

stinkbutt wrote:Don't declare an1 as a constant. I don't know why possum told you to do that, but the next time you attempt to analogRead() and put it into an1 it'll fail.


It prevents this common coding mistake...

Code: Select all | TOGGLE FULL SIZE
if(an1 = 5) {
  // Do something if an1 is equal to 5
}


There was no second assignment in the code, so no problem with const.
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Re: Ok, more programming help........

by cashman04 on Sun Nov 28, 2010 5:40 pm

But if I declare an1 as a constant, doesn't that mean it won't change when my analog input changes?
cashman04
 
Posts: 25
Joined: Fri Nov 19, 2010 7:04 pm

Re: Ok, more programming help........

by stinkbutt on Sun Nov 28, 2010 5:43 pm

cashman04 wrote:Also, Maybe I am doing this one wrong, but this
Code: Select all | TOGGLE FULL SIZE
byte[] font_table =
{
B10111011,
B00001010,
B01110011,
B01011011,
B11001010,
B11011001,
B11111000,
B00001011,
B11111011,
B11011011,
}


keeps giving me an error now, expected unqualified-id before '{'

I was assuming that that bit goes before my loop or setup, where I declare all the variables.


Oh yeah, I have an extra comma at the end, and no semicolon. THIS is the right code:

Code: Select all | TOGGLE FULL SIZE
byte[] font_table =
{
B10111011,
B00001010,
B01110011,
B01011011,
B11001010,
B11011001,
B11111000,
B00001011,
B11111011,
B11011011                      // No comma at the end
};
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by stinkbutt on Sun Nov 28, 2010 5:45 pm

oPossum wrote:
stinkbutt wrote:Don't declare an1 as a constant. I don't know why possum told you to do that, but the next time you attempt to analogRead() and put it into an1 it'll fail.


It prevents this common coding mistake...

Code: Select all | TOGGLE FULL SIZE
if(an1 = 5) {
  // Do something if an1 is equal to 5
}


There was no second assignment in the code, so no problem with const.


I see your point, but now that we're telling him to declare an1 outside of the setup() and loop() code, it's a global variable. So it can't be constant. The reason it worked earlier is that we kept re-declaring it and assigning it once, and then blowing it away.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Ok, more programming help........

by oPossum on Sun Nov 28, 2010 5:47 pm

Why should it be a global?

As a general rule, variables (other than literal constants) should be declared immediately before usage, and no sooner.
Last edited by oPossum on Sun Nov 28, 2010 5:49 pm, edited 1 time in total.
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Please be positive and constructive with your questions and comments.