Author Topic: Simple Arduino code rookie question  (Read 12316 times)

0 Members and 1 Guest are viewing this topic.

Offline IvoSTopic starter

  • Frequent Contributor
  • **
  • Posts: 312
  • Country: us
Re: Simple Arduino code rookie question
« Reply #50 on: June 16, 2015, 11:30:24 pm »
I have my code refined, I added 2 temperature sensors and I also added "LEDON" as my on/off status LED light.
Everything works but, lower down in the code following
Quote
val=analogRead (TEMP1);
if (val > UPPER_BOUND)
  {
      if (! lightsOn)             
      {
        do
        {
I want to have the LEDON to blink using "counter" while the temperature is still high above the LOWER_BOUND
Quote
while  (val > LOWER_BOUND);
but the LEDON blinks only once at the beginning when the "do" executes and then it's constantly ON during the while condition but it is not flashing. I am trying to figure out how to make it blink, but it looks like I have some conflicts there at the beginning >:( perhaps that prevent the blinking?  |O Any help appreciated.
Code: [Select]

#define BLEEDERS 0         //define IO
#define TRIAC 1
#define BYPASS 2
#define LEDON 3
#define BUTTON 4
#define TEMP1 5
#define TEMP2 6

const int LOWER_BOUND = 87;
const int UPPER_BOUND = 115;
int val = 0;

boolean lightsOn = false;    // state variables
boolean lastButton = LOW; // Variable containing the previous button state.
boolean currentButton = LOW;  // Variable containing the current button state.
int whichLED = 0;
int counter = 0;            // main counter

void setup()
{
  pinMode(BLEEDERS, OUTPUT);      // set up IO
  pinMode(TRIAC, OUTPUT);
  pinMode(BYPASS, OUTPUT);
  pinMode(LEDON, OUTPUT);
  pinMode(BUTTON, INPUT);
  pinMode(TEMP1, INPUT);
  pinMode(TEMP2, INPUT);
}
/*
* Debouncing function
* Pass it to previous button state,
* and get back the current debounced button state.
*/
boolean debounce (boolean last)
{
  boolean current = digitalRead (BUTTON);   // Read the button state
  if (last != current)                      // if it's different...
  {
    delay (10);                              // wait 10ms
    current = digitalRead (BUTTON);         // read it again
    return current;                         // return the current value.
  }
}

void loop ()
{
    currentButton = debounce (lastButton);            // read debounced state
    if (lastButton == LOW && currentButton == HIGH)   // if it was pressed...
  {
    lightsOn = ! lightsOn;
  }                                                 // toggle the LED value
lastButton = currentButton;                         // reset button value
             digitalWrite (whichLED, lightsOn);     // change the LED state
             digitalWrite (LEDON, lightsOn);

             if (lightsOn == false)                 // turn LED off and clean up
  {
  counter = 0;
    digitalWrite(BLEEDERS, LOW);
    digitalWrite(TRIAC, LOW);
    digitalWrite(BYPASS, LOW);
    digitalWrite(LEDON, LOW);
    whichLED = 0;
  }
  else                              // Or if we are activating LEDs
  {                                 
    digitalWrite(whichLED, HIGH);   // Turn first LED on to start
    digitalWrite(LEDON, HIGH);
  }

  if (lightsOn == true)              // Manage LED state
{
  counter++;                      // Increment counter
  if (counter >= 500)            // ~ 1 second has passed
    { // So increment LED state
      counter = 0;
      digitalWrite(whichLED, HIGH);  //
      whichLED++;
      if (whichLED > 2)            // Don't let whichLED go to invalid state
        whichLED = 2;
    }
  }
  delay(1);                          // 1 ms delay for 1000 count = ~ 1 second

val=analogRead (TEMP1);                // Read the TEMP1 sensor
    if (val >= UPPER_BOUND)            // If value is higher than upper bound
  {
    if (lightsOn)                      // and OUTPUTs are ON
    {
      counter = 0;                     // shut everything OFF and clear the counter
      digitalWrite (BLEEDERS, LOW);
      digitalWrite (TRIAC, LOW);
      digitalWrite (BYPASS, LOW);
      digitalWrite (LEDON, LOW);
      lightsOn = false;
    }
  }
   
    else
    {
      lightsOn == true;                // Otherwise, toggle button, ON - OFF
  }
   
    val=analogRead (TEMP1);        // Read the TEMP1 sensor
    if (val > UPPER_BOUND)         //If value is higher than upper bound...
    {
      if (! lightsOn)              //...and if outputs were shut OFF by TEMP1 sensor
      {
        do
        {
     lightsOn = false;             // turn all outputs OFF
     val=analogRead (TEMP1);       // and read the TEMP1 sensor again
     digitalWrite(LEDON, HIGH);
     
     counter++;                      //  I WANT THE "LED" TO BLINK
     if (counter >= 500)            //  USING counter INSTEAD OF delay()
    {                               //
      counter = 0;
      digitalWrite(LEDON, LOW);  //
             
    }
    else
    {
      digitalWrite(LEDON, HIGH);
    }
   }
      while  (val > LOWER_BOUND);  // keep all outputs OFF during the time when value is
      }                            // above lower bound, BUTTON is disabled
    }
    else                           
                                 
     {                           
      lightsOn == true;             // Otherwise, toggle button, ON - OFF
    }
   
    val=analogRead (TEMP2);             //Read the TEMP2 sensor
    if (val >= UPPER_BOUND)         //If value is higher than upper bound
  {
    if (lightsOn)                   // and OUTPUTs are ON
    {
      counter = 0;
      digitalWrite (BLEEDERS, LOW);  //shut everything OFF and clear the counter
      digitalWrite (TRIAC, LOW);
      digitalWrite (BYPASS, LOW);
      lightsOn = false;           
    }
  }
   
    else
    {
      lightsOn == true;            // Otherwise, toggle button, ON - OFF
  }
   
    val=analogRead (TEMP2);        // read the TEMP2 sensor
    if (val > UPPER_BOUND)         // if value is higher than upper bound...
    {
      if (! lightsOn)              //...and if outputs were shut OFF by TEMP2 sensor
      {
        do
        {
     lightsOn = false;             // turn all outputs OFF
     val=analogRead (TEMP2);       // and read the TEMP2 sensor again
   }
      while  (val > LOWER_BOUND);  // keep all outputs OFF during the time when value is
      }                            // above lower bound, BUTTON is disabled
    }
    else                           
                                   
     {                             
      lightsOn == true;            // Otherwise, toggle button, ON - OFF
     }
}
« Last Edit: June 16, 2015, 11:44:54 pm by IvoS »
 

Offline kolonelkadat

  • Regular Contributor
  • *
  • Posts: 202
  • Country: us
  • Obviously, windows are central to Windows.
    • Force Project X
Re: Simple Arduino code rookie question
« Reply #51 on: June 17, 2015, 12:04:40 am »
wow what a mess...
anyways what I think i did to your code was move the initial truning on of the led outside of the loop, then set up a led state variable (probably a terrible way to do it ram wise) and then every time counter >= 500 we toggle the ledstate and write the ledstate to ledon

(~ledstate)&HIGH should do the business, bitwise inversion of ledstate, taking only the HIGH bit...

Code: [Select]
#define BLEEDERS 0         //define IO
#define TRIAC 1
#define BYPASS 2
#define LEDON 3
#define BUTTON 4
#define TEMP1 5
#define TEMP2 6

const int LOWER_BOUND = 87;
const int UPPER_BOUND = 115;
int val = 0;

boolean lightsOn = false;    // state variables
boolean lastButton = LOW; // Variable containing the previous button state.
boolean currentButton = LOW;  // Variable containing the current button state.
int whichLED = 0;
int counter = 0;            // main counter

void setup()
{
  pinMode(BLEEDERS, OUTPUT);      // set up IO
  pinMode(TRIAC, OUTPUT);
  pinMode(BYPASS, OUTPUT);
  pinMode(LEDON, OUTPUT);
  pinMode(BUTTON, INPUT);
  pinMode(TEMP1, INPUT);
  pinMode(TEMP2, INPUT);
}
/*
* Debouncing function
* Pass it to previous button state,
* and get back the current debounced button state.
*/
boolean debounce (boolean last)
{
  boolean current = digitalRead (BUTTON);   // Read the button state
  if (last != current)                      // if it's different...
  {
    delay (10);                              // wait 10ms
    current = digitalRead (BUTTON);         // read it again
    return current;                         // return the current value.
  }
}

void loop ()
{
    currentButton = debounce (lastButton);            // read debounced state
    if (lastButton == LOW && currentButton == HIGH)   // if it was pressed...
  {
    lightsOn = ! lightsOn;
  }                                                 // toggle the LED value
lastButton = currentButton;                         // reset button value
             digitalWrite (whichLED, lightsOn);     // change the LED state
             digitalWrite (LEDON, lightsOn);

             if (lightsOn == false)                 // turn LED off and clean up
  {
  counter = 0;
    digitalWrite(BLEEDERS, LOW);
    digitalWrite(TRIAC, LOW);
    digitalWrite(BYPASS, LOW);
    digitalWrite(LEDON, LOW);
    whichLED = 0;
  }
  else                              // Or if we are activating LEDs
  {                                 
    digitalWrite(whichLED, HIGH);   // Turn first LED on to start
    digitalWrite(LEDON, HIGH);
  }

  if (lightsOn == true)              // Manage LED state
{
  counter++;                      // Increment counter
  if (counter >= 500)            // ~ 1 second has passed
    { // So increment LED state
      counter = 0;
      digitalWrite(whichLED, HIGH);  //
      whichLED++;
      if (whichLED > 2)            // Don't let whichLED go to invalid state
        whichLED = 2;
    }
  }
  delay(1);                          // 1 ms delay for 1000 count = ~ 1 second

val=analogRead (TEMP1);                // Read the TEMP1 sensor
    if (val >= UPPER_BOUND)            // If value is higher than upper bound
  {
    if (lightsOn)                      // and OUTPUTs are ON
    {
      counter = 0;                     // shut everything OFF and clear the counter
      digitalWrite (BLEEDERS, LOW);
      digitalWrite (TRIAC, LOW);
      digitalWrite (BYPASS, LOW);
      digitalWrite (LEDON, LOW);
      lightsOn = false;
    }
  }
   
    else
    {
      lightsOn == true;                // Otherwise, toggle button, ON - OFF
  }
   
    val=analogRead (TEMP1);        // Read the TEMP1 sensor
    if (val > UPPER_BOUND)         //If value is higher than upper bound...
    {
      if (! lightsOn)              //...and if outputs were shut OFF by TEMP1 sensor
      {
  int ledstate= HIGH;
  digitalWrite(LEDON, ledstate);
        do
        {
     lightsOn = false;             // turn all outputs OFF
     val=analogRead (TEMP1);       // and read the TEMP1 sensor again   
     counter++;                      //  I WANT THE "LED" TO BLINK
     if (counter >= 500)            //  USING counter INSTEAD OF delay()
    {                               //
      counter = 0;
  ledstate = (~ledstate)&HIGH; //toggle ledstate
      digitalWrite(LEDON, ledstate);  //
             
    }

   }
      while  (val > LOWER_BOUND);  // keep all outputs OFF during the time when value is
      }                            // above lower bound, BUTTON is disabled
    }
    else                           
                                 
     {                           
      lightsOn == true;             // Otherwise, toggle button, ON - OFF
    }
   
    val=analogRead (TEMP2);             //Read the TEMP2 sensor
    if (val >= UPPER_BOUND)         //If value is higher than upper bound
  {
    if (lightsOn)                   // and OUTPUTs are ON
    {
      counter = 0;
      digitalWrite (BLEEDERS, LOW);  //shut everything OFF and clear the counter
      digitalWrite (TRIAC, LOW);
      digitalWrite (BYPASS, LOW);
      lightsOn = false;           
    }
  }
   
    else
    {
      lightsOn == true;            // Otherwise, toggle button, ON - OFF
  }
   
    val=analogRead (TEMP2);        // read the TEMP2 sensor
    if (val > UPPER_BOUND)         // if value is higher than upper bound...
    {
      if (! lightsOn)              //...and if outputs were shut OFF by TEMP2 sensor
      {
        do
        {
     lightsOn = false;             // turn all outputs OFF
     val=analogRead (TEMP2);       // and read the TEMP2 sensor again
   }
      while  (val > LOWER_BOUND);  // keep all outputs OFF during the time when value is
      }                            // above lower bound, BUTTON is disabled
    }
    else                           
                                   
     {                             
      lightsOn == true;            // Otherwise, toggle button, ON - OFF
     }
}
 

Offline IvoSTopic starter

  • Frequent Contributor
  • **
  • Posts: 312
  • Country: us
Re: Simple Arduino code rookie question
« Reply #52 on: June 17, 2015, 08:25:22 pm »
Super! Thank you very much.  :-+ I will be honest, I would probably never figure it out like this (lack of experience). I don't have any example in my book similar to this one, or at least something close to it I could use and modify.
I was looking at this line you wrote: "ledstate = (~ledstate)&HIGH;" and I have no clue of its exact function other your note: //toggle ledstate.
Only I know of a state toggling is something like this: LEDON = ! LEDON, for example.
Anyway, it works as I need and I will leave it as it is. Even though it's a mess, it only has 1.5k bytes, so I am not expecting any misbehavior.
Thanks.
 

Offline kolonelkadat

  • Regular Contributor
  • *
  • Posts: 202
  • Country: us
  • Obviously, windows are central to Windows.
    • Force Project X
Re: Simple Arduino code rookie question
« Reply #53 on: June 18, 2015, 06:29:37 am »
I was looking at this line you wrote: "ledstate = (~ledstate)&HIGH;" and I have no clue of its exact function other your note: //toggle ledstate.
Only I know of a state toggling is something like this: LEDON = ! LEDON, for example.

Thanks.

let me break down that line for you.
(~ledstate)
~ means to do a bitwise inversion, meaning to look at the variable and turn every bit that is 0 to 1 and turn every bit that is 1 to 0
so if ledstate were 0b00000011, ~ledstate would be 0b11111100.

&HIGH
& means do a bitwise AND operation. The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

so in the case of &HIGH we limit ourselves to looking only at the bit that HIGH uses.



something like LEDON = !LEDON will do a boolean inversion, which might work fine in this use case, but it technically only swaps between 'true' and 'false'.
 

Offline IvoSTopic starter

  • Frequent Contributor
  • **
  • Posts: 312
  • Country: us
Re: Simple Arduino code rookie question
« Reply #54 on: June 18, 2015, 07:01:53 pm »
Thanks. :-+
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf