My First REAL Program

This is my first program that works well does what i set out to do and overall I am pretty darn proud of. OK, if it works why am I posting at all? Hoping for suggestions and criticism from everyone. This is a project I am working on for my degree in electronics technology, so I think I have a pretty good grasp on the hardware end of things. What I am completely new to is programming these buggers. I have no experience in any programming language so the last week or so (dedicated to this project and some school and work) has been a little frustrating. I feel I am beginning to make progress though and believe I am ready for some interactive help.

Sorry for all the comments in the code this is serving as part of my lab report so there is quite a bit in there.

/* 
this program was created using 100% open software and hardware
thank you to all who are willing to share your thoughts and ideas
making this possible!

thank you
sheepdogsoftware.co.uk invaluable resource for learning Arduino programing

ladyada.net/learn/arduino/ lots of great tutorials and examples

Arduino Cookbook by Michael Margolis

code was borrowed from all the above sources and many more
************************************************************
created by bFORTIFIED 03/11/2011 revision 2 (code cleanup)


requires LM35 and 5 LEDs (1 green, 3 yellow, 1 red)
and 5 current limiting resitors

LEDs configured common cathode

this program will measure temperature and display reading
in bargraph form

future plans include:

shortening/simplyfying code

driving 10 segment led bargraph with shift registers

driving 7 segment led display with temperature reading

driving brushless DC fan with PWM, based on temp reading

expanding system to 4 zones (4 independent temp sensors,
bargraphs, and fans)
*/



const byte sens=A0; // sets analog pin A0 to name "sens"
                    // LM35 output will be conected here
                    // the LM35 is a linearly proportional
                    // temperature sensor (the output increase
                    // is directly proportional to increase
                    // in tempeature) for every 1 degree celsius
                    // the tempeature changes the output will
                    // change by 10 mV

const byte LED0=2;  // assigns LEDs to digital pins 2-6
const byte LED1=3;
const byte LED2=4;
const byte LED3=5;
const byte LED4=6;

void setup()
{
  pinMode(LED0,OUTPUT);  // sets digital LED pins to outputs
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LED4,OUTPUT);
  
  setLEDs(1,1,1,1,1); //Turns on all LEDs for 1 second ->
  delay(1000);        //to check LEDs lighting properly.
  setLEDs(0,0,0,0,1); //Turns on and off LEDs in sequence->
  delay(500);         //to test LEDs and look coool.
  setLEDs(0,0,0,1,1);
  delay(500);
  setLEDs(0,0,1,1,1);
  delay(500);
  setLEDs(0,1,1,1,1);
  delay(500);
  setLEDs(1,1,1,1,1);
  delay(500);
  setLEDs(0,1,1,1,1);
  delay(500);
  setLEDs(0,0,1,1,1);
  delay(500);
  setLEDs(0,0,0,1,1);
  delay(500);
  setLEDs(0,0,0,0,1);
  delay(500);
  setLEDs(0,0,0,0,0);
  delay(1000);       /*all LEDs turn off for one second this lets
                       operator know when the LEDs come back on 
                       this is a legitimate reading*/ 
}

void loop()
{
 int sensVal=analogRead(sens); //sets register "sensVal" to reading ->
                               //from the temp sensor    
 float voltage = sensVal * 5.0;//converts "sensVal" to a voltage
 voltage /= 1024;
 
 float tempC = voltage * 100;  //converts voltage to temp in celsius
 
 float tempF = (tempC * 9/5) +32; //converts temp celsius to temp fehrenheit
 
 if (tempF<75) setLEDs(0,0,0,0,1);  //sets LEDs according to temp fehrenheit
 if (tempF>=80) setLEDs(0,0,0,1,1);
 if (tempF>=85) setLEDs(0,0,1,1,1);
 if (tempF>=90) setLEDs(0,1,1,1,1);
 if (tempF>=95) setLEDs(1,1,1,1,1);
 if (tempF>100) warningFlash(1);    //flashes LEDs when temp exceeds 100F

 
}
//a function to easily set the LEDs in the preceding if statements
void setLEDs(byte ledReg4, byte ledReg3, byte ledReg2, byte ledReg1, byte ledReg0)
{
  if (ledReg0==0) digitalWrite(LED0,LOW);
            else digitalWrite(LED0,HIGH);
  if (ledReg1==0) digitalWrite(LED1,LOW);
            else digitalWrite(LED1,HIGH);
  if (ledReg2==0) digitalWrite(LED2,LOW);
            else digitalWrite(LED2,HIGH);
  if (ledReg3==0) digitalWrite(LED3,LOW);
            else digitalWrite(LED3,HIGH);
  if (ledReg4==0) digitalWrite(LED4,LOW);
            else digitalWrite(LED4,HIGH);
}         
// a function to easily flash all the LEDs that also calls on the
//setLEDs function
void warningFlash(byte flashReg)
{
  if (flashReg==1)
  
      setLEDs(1,1,1,1,1);
      delay(300);
      setLEDs(0,0,0,0,0);
      delay(300);
}

I have a feeling I could clean up the test sequence in the setup somehow like making it add 1 until it gets to five then subtracting 1 till 0 not sure exactly how to do that thought.

And then would the LEDs be a could way to practice an array? Still not quite understanding the concept of arrays.

Your help is greatly appreciated!

hmmmmm [ code ] and [ /code ] didn't seem to work any ideas on that? trying to put the code in a scrolling text box of its own

Please modify your post to remove the space between the [ and the word code, and the one between the word code and the ]. Do the same in the [ /code ] bit, too.

10 and half seconds for setup() to run. Wow, you're way more patient than I am. The same effect could be performed with much shorter pauses.

setLEDs() gets passed 5 bytes, when one would do, if you used bitRead to extract the bits from a byte.

Not using floating point arithmetic is always good. You just have to make sure you don't loose to much precision or overflow.

float voltage = sensVal * 5.0;//converts "sensVal" to a voltage
 voltage /= 1024;
 
 float tempC = voltage * 100;  //converts voltage to temp in celsius
 
 float tempF = (tempC * 9/5) +32; //converts temp celsius to temp fehrenheit

Ends up reducing to:
tempF = (225 * sensVal)/256+32 (thank you wolfram|alpha). Since 255 * 1024 (max value of sensVal) is 18 digits long (in binary), you could store it in an unsigned long and retain the exact same precision. If you do this, make sure to put in a big comment as to where you got the magic numbers.

PaulS:
setLEDs() gets passed 5 bytes, when one would do, if you used bitRead to extract the bits from a byte.

OK I really do appreciate your input i just haven't been able to spend much time here until the semester ended.
Can someone help me with the bitRead function PaulS has suggested? I get the concept but can't quite figure out how to implement it. I have read about it in several places but don't know how to make all the LEDs one BYTE i think once i have that the bitRead will be simple

 if (tempF<75) bitWrite (LEDReg, 0, 1);
 if (tempF>=80) bitWrite (LEDReg, 1, 1);
 if (tempF>=85) bitWrite (LEDReg, 2, 1);
 if (tempF>=90) bitWrite (LEDReg, 3, 1);
 if (tempF>=95) bitWrite (LEDReg, 4, 1);
 if (tempF>100) warningFlash(1);    //flashes LEDs when temp exceeds 100F

or could i do something like
if (tempF<75) LEDReg=1
if (tempF>=80) LEDReg=3
if (tempF>=85) LEDReg=7
if (tempF>=90) LEDReg=15
?
can you use an if with a bitRead?
again i can't quite put it all together any help would be great

or could i do something like
if (tempF<75) LEDReg=1
if (tempF>=80) LEDReg=3
if (tempF>=85) LEDReg=7
if (tempF>=90) LEDReg=15
?

Either approach would work.

can you use an if with a bitRead?

Of course. Although it is not really needed.

void setLEDs(byte pattern)
{
  for(int i=0; i<6; i++)
  {
     digitalWrite(LEDPin[i], bitRead(pattern, i));
  }
}

Putting the LED pins in an array would be a code-saver, too.

Oh crap more stuff to learn :slight_smile:
Let me go learn the new commands and im sure i will have more questions thanks for you reply

void setLEDs(byte pattern)
{
  for(int i=0; i<6; i++)
  {
     digitalWrite(LEDPin[i], bitRead(pattern, i));
  }
}

Putting the LED pins in an array would be a code-saver, too.
[/quote]

bFORTIFIED:

void setLEDs(byte pattern)

{
  for(int i=0; i<6; i++)
  {
    digitalWrite(LEDPin[i], bitRead(pattern, i));
  }
}

OK I'm trying but I don't understand how this all comes together. First is (byte pattern) a byte sized variable named pattern? I don't understand why we use the "for" statement. I think i understand what it does, "for" is a loop, "int i" declares an integer variable i "i=0" sets "i" to zero, and then "i<6; i++" increments i by 1 if it is less then 6. Why are we introducing variable "i" and why are we incrementing it? wont this just turn the leds on one at a time and then stop? Why 6 if we only have 5 leds? OK still more questions but let me stop there and see if this helps understand it all.

I really do appreciate the help. I don't want to be the person who is too lazy to learn this stuff and really just wants someone here to do it for them, but man this programming is tough for me to grasp.

void setLEDs(byte pattern)

means, you're creating a function that doesn't return a value (void). The function is expecting a variable of type byte which will be called pattern.

for(int i=0; i<6; i++)

is indeed a loop. You're introducing the variable "i" as a counter. The loop will execute each time "i" - is less than 6. You increment it to step through the loop "i" times. You use 6 to count 5 leds because computers start counting from 0. i-1 = 5 LEDS

Hope this helps!

inboxjason:

void setLEDs(byte pattern)

means, you're creating a function that doesn't return a value (void). The function is expecting a variable of type byte which will be called pattern.

for(int i=0; i<6; i++)

is indeed a loop. You're introducing the variable "i" as a counter. The loop will execute each time "i" - is less than 6. You increment it to step through the loop "i" times. You use 6 to count 5 leds because computers start counting from 0. i-1 = 5 LEDS

Hope this helps!

Um sorta it confirms what I thought it did but i still dont understand why we are doing it. Why do we step through the loop 5 times?

You step the loop 5 times because you want to do the same thing 5 times.

and what "thing" would that be?

this thing

digitalWrite(LEDPin[i], bitRead(pattern, i));

ie. writing to pins 1-5 stored in the array LEDPin

OK OK i think i get the concept so... write a value to the first pin in LEDPin array, write a value to the second pin in LEDPin array, write a value to the third pin in LEDPin array... right? We are incrementing through the output pins?

OK so it must get the value to write from this bit of code digitalWrite(LEDPin[i], bitRead(pattern, i)); Ummm can you help me understand how the value it writes is extrapolated from this code? Would we set the variable "pattern" based on the temp? I.E. "if (tempF<75) pattern == 1);" but pattern isn't defined in the void loop so can we still write to that variable if it isn't defined in that function?

Sorry guys I feel like a first grader trying to understand how to spell THE. Again I really really really appreciate it!

You're incrementing the loop to increment the pin nos. bitRead is getting it's input from the function declaration (pattern)

void setLEDs(byte pattern)

as well as the counter "i".

You would set the variable "pattern" by calling the function setLEDS.

setLEDS(myPattern);

Pattern isn't defined in the main loop as it is only used by this funtion. When running the function, you give pattern a value, the other input is the counter in the for loop ("i").

Hope this helps!

Sooo

void setLEDs(byte pattern)

and

setLEDS(myPattern);

Where do we define the latter? It caint be a seprate function because of ";" right but what function does it go in?

Where do we define the latter?

"The latter" is a call to the function. You don't define calls, you make calls.

but what function does it go in?

It goes where you want the function called. Typically in loop(), but you can call it from a function called from loop(), too.