Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Arduino code  (Read 3377 times)

0 Members and 1 Guest are viewing this topic.

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7413
  • Last login:Yesterday at 01:17:00 pm
  • I break stuff...then fix it...sometimes
Arduino code
« 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);             
           
    }       
}
« Last Edit: May 08, 2016, 10:23:09 pm by lilshawn »

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7413
  • Last login:Yesterday at 01:17:00 pm
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #1 on: May 08, 2016, 10:28:56 pm »
changing to analogWrite and using 0/255 seems to work?

 :dunno

 is "low" actually "floating"?
« Last Edit: May 08, 2016, 10:34:14 pm by lilshawn »

ed12

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 3972
  • Last login:March 31, 2018, 03:44:39 pm
  • it is what it is..."Nobody Said It Was Easy"....
Re: Arduino code
« Reply #2 on: May 09, 2016, 12:03:14 am »
 digitalWrite(lednorm,LOW);

ed
Shipping something from the U.S. to Canada for repair/exchange?  Please use USPS to avoid (additional?/excessive?) shipping charges.  PM me if you have any questions.

baritonomarchetto

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 811
  • Last login:Yesterday at 01:17:41 am
Re: Arduino code
« Reply #3 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.
« Last Edit: May 09, 2016, 11:20:14 am by baritonomarchetto »

lilshawn

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7413
  • Last login:Yesterday at 01:17:00 pm
  • I break stuff...then fix it...sometimes
Re: Arduino code
« Reply #4 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?

baritonomarchetto

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 811
  • Last login:Yesterday at 01:17:41 am
Re: Arduino code
« Reply #5 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)
« Last Edit: May 10, 2016, 12:51:24 am by baritonomarchetto »