Problem with arduino programming

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
dragonuv
 
Posts: 235
Joined: Tue Dec 04, 2007 1:22 pm

Problem with arduino programming

Post by dragonuv »

Hi, I have written this code:

Code: Select all

   
int state,incoming1,incoming2;

void setup()                
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);    
  pinMode(11,OUTPUT);
}

void loop() {
if (Serial.available() > 0) {
  state = Serial.read();
  if (state == 49)
  {
    if (Serial.available() > 0) {
    incoming1 = Serial.read();
        if (Serial.available() > 0) {
          incoming2 = Serial.read();
           if ((incoming1==49) && (incoming2==51))
            {
             digitalWrite(13, HIGH);
            }
            else if ((incoming1==49) && (incoming2==49))
            {
             digitalWrite(11, HIGH);
            }
        }
    }
  }
  else  //(state ==48)
  {
    if (Serial.available() > 0) {
    incoming1 = Serial.read();
        if (Serial.available() > 0) {
          incoming2 = Serial.read();
           if ((incoming1==49) && (incoming2==51))
            {
             digitalWrite(13, HIGH);
            }
            else if ((incoming1==49) && (incoming2==49))
            {
             digitalWrite(11, HIGH);
            }
        }//get incoming2
    }//get incoming1
  }
}//end-getstate
  Serial.print(state);
  Serial.print(incoming1);
  Serial.println(incoming2);
  delay(200);
}
its goal is to turn on and turn off 2 leds (pin 13 and 11).
it first gets the state, '1' (49 in ascii) or '0' (48 in ascii) and then gets another 2 numbers which indicates the led number, if the state is 1 it turns on the led, else it turns off the led. for example, if i want to turn on led 13 ill type '113' and to turn off led 11 ill type '011'.
my problem is that it doesnt work when i try to turn them off. is anything wrong with the statement? or anything else? because they just dont turn off.
Thanks!

edit:
OMG! thats the most stupid mistake ive ever made!! instead of writing LOW to turn it off ive written HIGH. anyways, u can have a free copy of that code lol :oops:

dragonuv
 
Posts: 235
Joined: Tue Dec 04, 2007 1:22 pm

Re: Essay on War Power Leveling

Post by dragonuv »

deleted
Last edited by dragonuv on Wed Jan 21, 2009 6:31 pm, edited 1 time in total.

User avatar
halley
 
Posts: 73
Joined: Fri Nov 21, 2008 11:07 pm

Re: Problem with arduino programming

Post by halley »

I know it's an old thread, but I thought I'd mention that you should use character constants instead of plain integers.

Program source code (aka "sketches" in the Arduino world) is first and foremost for humans to read.

Code: Select all

if (mode == '1') { ... }

if (mode == '0') { ... }
Saying '1' is equivalent to saying 48 (on an ASCII based system). Since the '1' and '0' make more sense to your application, with characters received via a serial line, it is more readable and understandable to use that.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Problem with arduino programming

Post by Franklin97355 »

Also you are not driving the leds low anywhere.

User avatar
karlgg
 
Posts: 212
Joined: Sat Dec 27, 2008 2:41 pm

Re: Problem with arduino programming

Post by karlgg »

DragonUV, I would edit that comment and remove the URL links if I were you. That was just a stupid spammer giving himself links to his website, and your quote has inadvertently kept them alive even after the message was deleted.

dragonuv
 
Posts: 235
Joined: Tue Dec 04, 2007 1:22 pm

Re: Problem with arduino programming

Post by dragonuv »

done

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”