Light source PWM regulation through labview

Hey,

I'm doing this project where the goal is to control the power of a light source through labview. I have both of those code but at the end of the day I can never contol the light anyone can help me ?

float labviewFLT = 0;
const byte numChars = 32;
char labviewSTR[numChars];
int pwm = 4; // assigns pin 4 to variable pwm
int t1 = 0;   // declares variable t1
int t2 = 0;   // declares variable t2
float a = 5;
float b = 1000;
int c = 1000;
boolean newData = false;

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

void loop() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            labviewSTR[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            labviewSTR[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
    if (newData) {
      labviewFLT = atof(labviewSTR);
      t2  = labviewFLT/(a/b); // reads the voltage at A0 and saves in t2; // reads the voltage at A0 and saves in t2
      t1= c-t2;         // subtracts t2 from 1000 ans saves the result in t1
      digitalWrite(pwm, HIGH); // sets pin 12 HIGH
      delayMicroseconds(t1);   // waits for t1 uS (high time)
      digitalWrite(pwm, LOW);  // sets pin 12 LOW
      delayMicroseconds(t2);   // waits for t2 uS (low time)
      newData = false;
    }
    else {
      digitalWrite(pwm,LOW);
    }
}

And for labview :


I'm really sorry if this seems to be an obvious problem but I'm clearly not talented at electricity or programming
Thanks a lot

I don't know about labview, but your program does not generate PWM as you wrote. When receiving new data, your program only ONCE sets the pin high for a certain amount of time and then turns it off. Then it waits for new data and does nothing. If you want this pin to have a continuous signal, use the analogWrite() command and make sure the selected pin has hardware PWM - you didn't say what Arduino you're using, but the UNO version doesn't have PWM on pin 4, only on pins 3,5, 6, 9, 10 and 11.

FYI, digitalWrite is not an arduino PWM command. The correct command is analogWrite, as Flashko
pointed out.
Why are using digitalWrite ?
I know this is going to sound obvious , but if you are going to use an Arduino, wouldn't it be wise to
at LEAST due your due diligence and study the basics ? Read some tutorials, use the Arduino Reference page, SEARCH FOR "PWM" ON THE "Search FORUM " search bar ?
Posting here without doing the above is like saying "I really don't have time to learn anything, could
you just please tell me what to do ?" , is it not ?
(I call it like I see it)
Give this a read:
PWM LIGHT DIMMER

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.