controle a PWM output in Arduino using LabView
controle a PWM output in Arduino using LabView
What is PWM ?
so to create a PWM signal using arduino you just have to write this line :
analogWrite(Pin, nb);
Pin : is the PWM output
nb is a number between [0 , 255] to determin the Duty cycle
now how to control it from labview , you need to to send the Duty Cycle from labeview to arduino and then command the PWM output
to open Serial port in arduino we need to write this line :
Serial.begin(9600);
to read from Serial port :
while (Serial.available() > 0)
{
int inChar = Serial.read();
}
the arduino full code is :
///////////////////////////////////////////////////////////////////////////////////////////////
// pins for the LEDs:
const int Pin = 6;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(Pin, OUTPUT);
digitalWrite(6,1);
delay(1000);
digitalWrite(6,0);
}
String inString = "";
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
int nb = inString.toInt();
analogWrite(Pin, nb);
// clear the string for new input:
inString = "";
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// pins for the LEDs:
const int Pin = 6;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(Pin, OUTPUT);
digitalWrite(6,1);
delay(1000);
digitalWrite(6,0);
}
String inString = "";
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
int nb = inString.toInt();
analogWrite(Pin, nb);
// clear the string for new input:
inString = "";
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
Commentaires
Enregistrer un commentaire