Go Down

Topic: Turning on and off LEDs with Labview (Read 2 times) previous topic - next topic

centersnare30

Hi,

I am starting to experiment with using Labview to control the Arduino.

I wrote this code which should accept certain letters and turn on and off certain LEDs

When I use the serial monitor and put in a, b, c, d, e, or f the lights do what they are supposed to do.

So, I go into Labview 8.2 and run the Basic Serial Write and Read (I would put a picture in but it is not letting me).
Its options are set to  
BAUD: 9600
Data bits: 8
Parity: none
Stop bits: 1
Flow control: none

I write a, b, c, d, e, or f and the lights don't turn on. I know it is receiving something, because the lights blink on the arduino.  I guess my question is, what am I missing? I'm guessing it is some sort of conversion that I do not understand is going on.  Thank you for the help.



Code: [Select]
void setup() {
 
  Serial.begin(9600);
 
        pinMode(10, OUTPUT);
        pinMode(5, OUTPUT);
        pinMode(3, OUTPUT);
  }

void loop() {
 
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 
 
    switch (inByte) {
    case 'a':    
      digitalWrite(10, HIGH);
      break;
    case 'c':    
     digitalWrite(5, HIGH);
     break;
    case 'e':    
      digitalWrite(3, HIGH);
      break;
    case 'b':    
      digitalWrite(10,LOW);
      break;
    case 'd':    
     digitalWrite(5, LOW);
     break;
    case 'f':    
      digitalWrite(3, LOW);
      break;
   
    }
  }
}



PaulS

Now that you've made your first post, you can post links. You can also edit posts. So, you could edit your first one, and add links.

I'd add some Serial.print statements to the code, to see what was actually received. It's likely that some thing other than what you expected was sent. Perhaps something as simple as 'a' -> 'A'. Printing the character that was received is the only way to know.

centersnare30

So i got the lights to be able to be controlled with labview.

The next thing i was trying to do is control how bright or dark  an LED is.

I wrote up a program that used

analogWrite(ledPin, val);
in a for loop increasing val from 0-255 and back down again.
That worked like i expected turning the led on and off slowly


So then I wrote the code below expecting the lights to turn on different levels as I wrote 0-255 into the serial monitor.

I don't understand why it is not working but, 0 does not turn it off as expected or even make it dim.

Thanks for the help

Code: [Select]
void setup()
{
 Serial.begin(9600);
 pinMode(3, OUTPUT);
}

void loop()
{
 if (Serial.available()) {
   
 int val = Serial.read();
  analogWrite(ledPin, val);
 }
}

Coding Badly

Quote
0 does not turn it off as expected or even make it dim

Is your LabView progam sending a 0 (binary zero) or a "0" (ASCII character)?

centersnare30

I figured it is something like that. Any quick hints on how to do that in Labview I haven't used it in a few years so I'm a little rusty.

For an even simpler version than that is there a way to use the serial communicator that is included with the arduino coding program.

PaulS

Just start the Arduino IDE, select the appropriate com port, and open the Serial Monitor. There is a field to type data into. Type and hit send. Off the data goes to the Arduino.

centersnare30

I guess to add onto what I just wrote. The basis of all my problems comes down to I don't know how to send numbers to the arduino.

For example:  If I wanted to make a program that would turn on a light when the number sent to it is higher than 50.  How can I write in the Labview or even the serial monitor numbers 1, 2,3,4,5, etc and have the light not turn on until I get to 50 or greater?  I tried searching for examples and can't find anything. But, I might but searching for the wrong topics.

Thank you for the help




PaulS

The Serial Monitor window knows nothing about numbers. Everything is a character. Some of the characters are on the top row of keys, and look like numbers to us, but to the serial monitor, they are characters, just like t, G, or >.

So, type a "number" in, and hit send.

On the Arduino:

Code: [Select]
void loop()
{
   int num = 0;
   while(Serial.available() > 0)
   {
       char digit = Serial.read();
       if(digit >= '0' && digit <= '9')
       {
           num *= 10;
           num += digit - '0'; // Change the character to a number
       }
   }

   if(num > 50)
   {
       // Turn some light on, somewhere, somehow
   }
}

Go Up