Demonstration code for several things at the same time

evancleary:
any ideas?

I don't know about the code, but you have a serial.print() line in the loop() function that I would like to encourage you to edit, or the moderators may do it for you.

/remember, this forum caters to many age groups and such; let's try to keep it somewhat in line with that...

cr0sh:
...or the moderators may do it for you.

Too late.

/remember, this forum caters to many age groups and such; let's try to keep it somewhat in line with that...

Well stated @cr0sh. Thank you for your post.

@evancleary, I make you this promise: I will not waste my time again cleaning up your mess.

I take it that no input is required from me until there is some code to look at?

...R

apologies for the bad language in the last post guys
was messing about with the print function and forgot to remove the phrase before post

const int ledPin1= 13;
const int ledPin2= 12;
const int ledPin3= 11;  
const int ledPin4= 10;
const int ledPin5= 9;
int sensorValue = 0;

// ==================variables==================================== 

int degrees_21 = LOW;
int degrees_22 = LOW;
int degrees_23 = LOW;   // these are my seperate functions
int degrees_24 = LOW;   // kept the rang of temp around room temp for 
int degrees_25 = LOW;   // trial run
int degrees_26 = LOW;

//===================================================================

void setup()

{
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);  
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  
  Serial.begin(9600);
}

//========================================================================

void loop()

{
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  

updatedegrees_21(); 
updatedegrees_22(); 
updatedegrees_23(); 
updatedegrees_24(); 
updatedegrees_25(); 
updatedegrees_26(); 


}

//=====================================================================

void updatedegrees_21()      
{
  if (degrees_21 == LOW)     // was thinking about putting an else (degrees_21 = LOW)
  {                              // but didnt think it was required
    if (sensorValue <= 43)//21
    degrees_21 = HIGH;
  } 
        if (degrees_21 == HIGH)   
      {  
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, HIGH);
          digitalWrite(ledPin4, LOW);
          digitalWrite(ledPin5, HIGH);
      }
   
}

  
//==================================================================

void updatedegrees_22()
{
  if (degrees_22 == LOW)
  {
    if (sensorValue > 43 && sensorValue <= 45)//22
    degrees_22 = HIGH;
  }
        if (degrees_22 == HIGH)
      {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, HIGH);
          digitalWrite(ledPin4, HIGH);
          digitalWrite(ledPin5, LOW);
       }
   
}
      
//======================================================================

void updatedegrees_23()
{
  if (degrees_23 == LOW)
  {
    if (sensorValue > 45 && sensorValue <= 47)
     degrees_23 = HIGH;
   }
        if (degrees_23 == HIGH); 
        {
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, LOW);
            digitalWrite(ledPin3, HIGH);
            digitalWrite(ledPin4, HIGH);
            digitalWrite(ledPin5, HIGH);
        }
    
}
  
//==========================================================================

void updatedegrees_24()
{
  if (degrees_24 == LOW)
  {
    if (sensorValue > 45 && sensorValue <= 47)
     degrees_24 = HIGH;
   }
        if (degrees_24 == HIGH); 
        {
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, LOW);
            digitalWrite(ledPin4, LOW);
            digitalWrite(ledPin5, LOW);
        }
    
}

//========================================================================

void updatedegrees_25()
{
  if (degrees_25 == LOW)
  {
    if (sensorValue > 49 && sensorValue <= 52)//25
    degrees_25 == HIGH;
  }
      if (degrees_25 == HIGH) 
      {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, LOW);
          digitalWrite(ledPin4, LOW);
          digitalWrite(ledPin5, HIGH);
      }
  
}

//===========================================================================

void updatedegrees_26()
{
  if (degrees_26 == LOW)
  {
     if (sensorValue > 52 && sensorValue <= 54)
     degrees_26 = HIGH;
  }
        if (degrees_26 == HIGH)
      {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, LOW);
          digitalWrite(ledPin4, HIGH);
          digitalWrite(ledPin5, LOW);
      }

in the version of this code that i have working i basically have a load of if and else function on a delayed loop which constantly checks sensorValue, thought applying it with non-blocked architecture would be a good training exercise

@CodingBadly Im not looking for anyone to clean up my mess just looking for some advice or point in the right direction because i have a breathalyzer with multiple inputs/outputs and functions to have completed in the next 2 weeks

yet again folks my apologies

For all these, you could simplify some:

void updatedegrees_25()
{
if (degrees_25 == LOW)
{
if (sensorValue > 49 && sensorValue <= 52)//25
degrees_25 == HIGH;
~~ }~~
~~ if (degrees_25 == HIGH)~~
~~ {~~ digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, HIGH);
}

}

What makes degrees_25 and the others Low again to allow another reading?

(apparently strikeout does not work within code tags)

Do you also find this update really fast so the outputs seem to be the same all the time?
I think I would add a time check and only update every 1/10 or 1/5 second or so.

was thinking that falling out of the stated sensorValue range would set the function back to when checked and found not to be

may use an else function stating that when not within range = LOW

im gona be looking into some tutorials posted up by GrumpyMike to try get my head more around the logic required

What is all the digitalWrite(ledPinX, HiGH/LOW) stuff?

If these were put in named functions it would make the code much clearer.

It may also be possible to do them all in one function to reduce duplication of code.

...R

its representing the degrees celcius in a 5 bit binary led setup

ie 26 degrees = 1 1 0 1 0

thus led 1 HIGH and so on

got some advice from GrumpyMike earlier saying that they can be given an array function which this page shows how to

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

it would seem ive got a lot of ground to cover and not a whole lot of time to do it considering i have 2 exams in the next couple of weeks to boot, so it all systems are non stop. This is why Ive been looking for a bit of direction, gona run through as much of Mikes tutorials as I can and hopefully Ill be able to work out this project

I think we could help you much better if you give us a clear description (in english, not code) of what you want to do.

You have jumped into the middle of this Thread (which is fine) but you might have been better starting your own Thread. I suspect you are assuming we know a great deal more about your project than we actually do. Assume we know nothing!

It's not even clear to me how you are thinking of making use of my demo sketch (which, I assume, is the reason you have asked your question here).

...R

Robin2:
...but you might have been better starting your own Thread.

He has. Twice...
http://forum.arduino.cc/index.php?topic=225213.0
http://forum.arduino.cc/index.php?topic=224682.0

@CodingBadly Im not looking for anyone to clean up my mess...

Then stop posting obscenities, stop cross-posting, and stop hijacking.

Thanks, I hadn't spotted those.

@evancleary, over to you to sort things out ....

...R

ok so i made a post first time round which is the second link

after doing that i came across your post here which seemed extremely helpful because im going to have to integrate multiple functions into the one code , so i made a reply then explaining fully what it was i hoped to achieve and how i was planning to use your code in helping me learn what it was i needed to know, however that reply that i made to this post ended up being its own post? with its own new title,a title which i didnt give it. Dont know how or why?

Apologies if my code dosnt seem neat or logical, yet again im very new to this and am just trying to find me feet with it

also yet again apologies for the obscenities but as already stated it was an absolute mistake

didnt know that i was hijacking or that posting on multiple links was a bad thing, i dont really use forums

how and ever thanks for the post it has given me an idea of how to go about getting a main loop to reference function loops, between this and GrumpyMikes stuff i should be on the right track

The problem with multiple Threads on the same subject is that none of them has the full picture. And someone may waste his/her time giving advice on one Thread which has already been given on another.

From your own point of view the problem is that contributors will only have part of the picture and won't be able to give you the best advice.

Hijacking a Thread is slightly different - it usually means causing the Thread to wander off into a discussion that the person who started the Thread hadn't intended. In this case your interest seems to have been to adapt my code to your project. I think you lose most from using this Thread for that because readers won't expect your problem to appear here.

I suggest you ask the moderator to merge your other two threads and then continue your enquiries there, and not here.

And, as I said earlier, you need to provide a very clear description of what you are trying to do before you tell us how you are trying to do it.

...R

Robin,

Keith here, you referred me to this thread a couple of days ago. Thanks again for doing something like this for irritating beginners like myself :slight_smile:

It's a bit brain frying for me at present but with a bit of repetitive studying I'm sure it will start coming together for me. One of the earlier responses was about the bracketing positioning. As a beginner the first thing that made it difficult for me to read through the code, was the positioning of the curly brackets.

I modified the bracket positioning to make the functions more graphically visible (if that's a way to put it). Now I can clearly see a function inside a function inside a function, so to speak, and it leaves my beginners brain more free to learn the coding.

Example:

void updateLed_A_State()
{
if (led_A_State == LOW)
{
if (currentMillis - previousLed_A_Millis >= led_A_Interval)
{
led_A_State = HIGH;
previousLed_A_Millis += led_A_Interval;
}
}
else
{
if (currentMillis - previousLed_A_Millis >= blinkDuration)
{
led_A_State = LOW;
previousLed_A_Millis += blinkDuration;
}
}
}

I hope I did all that correctly LOL.

Opening and closing brackets of each function line up vertically. Brackets of "inner" functions have good distance on indentation. I realise as a proficient experienced programmer you may detest this for your own use, but seeing as your sketch is for beginners I thought I'd mention this.

Keith.

beefy23:
I modified the bracket positioning to make the functions more graphically visible (if that's a way to put it). Now I can clearly see a function inside a function inside a function, so to speak, and it leaves my beginners brain more free to learn the coding.

That indentation is horrible. I recommend you use Tools / Autoformat to put it right.

Indentation is a personal choice. Do it any way you like.

I like my way.

It is a good idea to use a system that is similar to what autoformat produces for stuff that you propose to post here. People find it easier to get a quick understanding if the format is familiar.

MUCH more important, @beefy23, ALWAYS enclose code in CODE tags so that it displays properly. Read the stickies at the top of the Forum for instructions.

...R

PeterH:

beefy23:
I modified the bracket positioning to make the functions more graphically visible (if that's a way to put it). Now I can clearly see a function inside a function inside a function, so to speak, and it leaves my beginners brain more free to learn the coding.

That indentation is horrible. I recommend you use Tools / Autoformat to put it right.

Wow Pete,

that's highly constructive and educational. I love the explanation you give of why it's horrible. I was speaking as a beginner, saying what I personally find makes the code much easier to read for me. Robin started this for beginners so I simply said what aided me as a beginner to follow the code. I wasn't saying Robin or anyone else should coding should code like this themselves. I even said to Robin that he may detest that method himself.

I'll take note of your recommendation and see what results I get when I use the auto feature. For a beginner I may personally find that result horrible. Then I can come back to you and say it's horrible and not say why.

As for my recommendation to you, don't comment if all you can say is something is horrible without giving any explanation as to why. Should I start being just as "constructive" as you and we can both start throwing rubbish at each other. Forums becomes a great place when that starts.

Edit:
I've just went at took a look at the way Jiggy Ninja (the one who said he prefers a different bracketing arrangement) does his code and remarkably it's very similar to what I did. Looks like there's 2 of us have a "horrible" arrangement. To me it's very clear and easy on the eyes. Functions stand out so clearly, perfect for a beginners eyes. I suspect I will find your personal method of bracketing much more horrible.

Keith.

Hi Robin,

thanks for the explanation on why to use the autoformat, and I'll read the stickies about putting code in the threads.

Keith.