Go Down

Topic: LED switchcase isn't responding properly (Read 233 times) previous topic - next topic

SuperStacie2011

I have tried multiple different ways to get this project to work and this is only step 1. I have attached my code. I am using the correct com port COM4, when I upload everything complies correctly, but when given input to the serial Monitor the arduino uno I am using doesn't respond properly. ON should turn lights on, and OFF should turn them off, but the lights are either on all the time or won't turn on at all. I have my input as 2 and have tried switching through the inputs to be able to get any result and the only thing that works is pin 13, but I am trying other pins because of my project is going to be using a set of two pins to make a flashing light.


Also, I read through a lot of the FAQs and and other forum posts so I feel like where I am supposed to post.

Thank you  

SuperStacie2011

It wouldn't allow me to attach this to my first post. This is the picture of the setup I am doing for my project.

septillion

Since when is 'ON' or 'OFF' a single character?

Aka, you can't compare it like that / use a switch() like that.

Try for example:
Code: [Select]
//no need for byte
//made the variable name more descriptive
const byte LedPin = 8;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  pinMode(LedPin, OUTPUT);
}

void loop() {
  //only read if there is something to read
  if(Serial.available()){
    char data = Serial.read();
    //and really check for a char
    switch(data){
      case 'N':
        digitalWrite(LedPin, HIGH);
        break;
      case 'F':
        digitalWrite(LedPin, LOW);
        break;
    }
  }
}


Please also use code tags next time. See "How to use the forum" for that (and more).
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Paul__B

It wouldn't allow me to attach this to my first post. This is the picture of the setup I am doing for my project.
No picture.

You need to go and read the forum instructions so that you can mark up your code and insert it using the "</>" icon in the posting window.

In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window.  But don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read.  If you do not post it as "code" it can as you now see, be quite garbled and is always more difficult to read.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper.  People can usually see the mistakes directly and do not want to have to actually load it in their own IDE.  And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space.  Do use blank lines, but only between functional blocks.

Go Up