Build Your Own Arcade Controls Forum

Main => Raspberry Pi & Dev Board => Topic started by: lilshawn on May 08, 2016, 09:52:18 pm

Title: Arduino code
Post by: lilshawn on May 08, 2016, 09:52:18 pm
picked up an uno kit to use for a project. i'm having some difficulty with the programming (total noob here...last time i coded was in basic) i have everything fleshed out on breadboard and everything works.

i managed to piece together some code and it almost does what I want... it looks right to me, but i've been staring at it so long i'm sure i'm looking past a glaring mistake. I think it's right, but it's wrong.

basically i'm lighting a "normal status" LED....monitoring the temp of a probe....when it reaches a set value, it should shut off the output of the normal status LED and turn on another output that is a "warning" LED.

as you will see below, I have the initalization of the normal status LED so it lights on startup... it switches the other LED on when the threshold is reached as intended, but does not turn off the normal status LED as intended...and does not turn off the warning LED once back below the threshold. my if else loop looks okay but is likely wrong.

i output the temp data to the serial port to confirm proper operation, this will be removed later.

any coders out there want a laugh?

Quote
unsigned long tepTimer ;
int ledred=9;
int lednorm=11;
void setup(){
     
    Serial.begin(9600);
 
}

void loop(){
    int val;               
    double data;         
    val=analogRead(0); 
    data = (double) val * (5/10.24);  // convert the voltage to temperture
     
    if(data>27){        // If the temperture is over 27 degrees then alarm 
          digitalWrite(ledred,HIGH);
          digitalWrite(lednorm,LOW);
          delay(2);
     }   
     else { digitalWrite(ledred,LOW);
           digitalWrite(lednorm,HIGH);
           delay(2);}
     
    if(millis() - tepTimer > 500){     // output the temperture value per 500ms
             tepTimer = millis();
             
             Serial.print(data);             
           
    }       
}
Title: Re: Arduino code
Post by: lilshawn on May 08, 2016, 10:28:56 pm
changing to analogWrite and using 0/255 seems to work?

 :dunno

 is "low" actually "floating"?
Title: Re: Arduino code
Post by: ed12 on May 09, 2016, 12:03:14 am
 digitalWrite(lednorm,LOW);

ed
Title: Re: Arduino code
Post by: baritonomarchetto on May 09, 2016, 03:51:00 am
You must declare the pinMode as output for pin 9 and 11. By default, pins are set as (floating) inputs.
Title: Re: Arduino code
Post by: lilshawn on May 09, 2016, 05:56:53 pm
You must declare the pinMode as output for pin 9 and 11. By default, pins are set as (floating) inputs.

ahhh i see.

so add in the void setup

pinMode(lednorm,output);
pinmode(ledred,output);

so declared as an output the pin is either pulled HIGH or LOW and

if left undeclared the pin is left floating after being pulled HIGH or LOW.

is there a scenario you would want this type of output? external pull up/down?
Title: Re: Arduino code
Post by: baritonomarchetto on May 10, 2016, 12:46:46 am
You can pull high or low pins both setting them as input (by external trigger) or output (by internal trigger). The main difference is the high impedence of pins set as input (and other things the IDE sets for you).
When using the pin as input be sure to connect a pulldown resistor or activate the internal pullup resistor (see arduino basics for this: there are tons of examples for starters included with the IDE)