One time loop for portion of program....

Please Help,

Attach is a sketch that I am having issues with. The power control I need to run just one time. The BT serial I need to continue in loop. I have tried some of the recommendations on this site without success. Any suggestion would be greatly appreciate.
Thank You
Don
#include <avr/io.h>
#include <SoftwareSerial.h>

SoftwareSerial BTserial(0, 1); // RX |

int PowerP1=2;//power
int PowerP2=5;//Power
int ledPin1 =3;//light

int A ;
int B ;
char val;

void setup() {
pinMode(ledPin1,OUTPUT);//red
pinMode(PowerP1,OUTPUT);
pinMode(PowerP2,OUTPUT);
digitalWrite(ledPin1,LOW);
delay(5000);
BTserial.begin(9600);
pinMode(A5, INPUT);//Btube
pinMode(A1, INPUT);//Atube
}

void loop () {

/////I need this to only run once/////
for (int fadeValue = 0; fadeValue >= 0; fadeValue += 10) {
// sets the value (range from 0 to 255):
analogWrite(PowerP1, fadeValue);
analogWrite(PowerP2, fadeValue);

if(fadeValue < 255)
{

}
else if (fadeValue >=255)
{
digitalWrite(ledPin1,HIGH);

////I need this to loop////
const int chanA = analogRead(A1);
float A = chanA * (5.0 / 1023.0);
float Amilli = (A * 0.41);
BTserial.print("\tAmilli: ");
BTserial.print(Amilli,2);
BTserial.println();
delay(500);

const int chanB = analogRead(A5);
float B = chanB * (5.0 / 1023.0);
float Bmilli = (B * 0.41);
BTserial.print("\tBmilli: ");
BTserial.print(Bmilli,2);
BTserial.println();
delay(500);
if (BTserial.available() > 0)
{
char val = BTserial.read();
} } } }

bias_control.ino (1.27 KB)

SoftwareSerial BTserial(0, 1); // RX |Forget about other problems for now and explain why you are using the hardware Serial pins for SoftSerial

/////I need this to only run once///// So put it in setup()

I am novice at best, both are parts of other examples just redone to suit my purpose. The first half is to run HV power supply with a P.W to slow down the inrush of voltage/current. The second half is to monitor the current. In the past I have not combined both of them. I have used while(1) in the power sketch without issue. I tried the power part in Set Up however it will not compile without errors. I may have come up with a way to keep both parts in the loop by digitalWrite both P1& P2 high at the end of the PW. using pin 0&1 for rx tx is really not a issues only when programming the BT module is disconnected. Plus this is kinda set in copper/PCB, hindsight is always 20/20 i could have reassigned to other pins. Again thank you for any help on this one.

Don

#include <avr/io.h>
#include <SoftwareSerial.h>

SoftwareSerial BTserial(0, 1); // RX |

int PowerP1= 2;//power
int PowerP2= 5;//Power
int ledPin1= 3;//light

int A ;
int B ;
char val;

void setup() {
pinMode(ledPin1,OUTPUT);//red
pinMode(PowerP1,OUTPUT);
pinMode(PowerP2,OUTPUT);
/// digitalWrite(ledPin1,LOW);
delay(5000);
BTserial.begin(9600);
pinMode(A5, INPUT);//Btube
pinMode(A1, INPUT);//Atube
delay(5000);

}

void loop () {

for (int fadeValue = 0; fadeValue >= 0; fadeValue += 10) {
// sets the value (range from 0 to 255):
analogWrite(PowerP1, fadeValue);
analogWrite(PowerP2, fadeValue);

if(fadeValue < 255)
{
digitalWrite(ledPin1,LOW);
}
else if (fadeValue >=255)
{
digitalWrite(ledPin1,HIGH);
digitalWrite(PowerP1,HIGH);
digitalWrite(PowerP2,HIGH);

}

////I need this to loop////
const int chanA = analogRead(A1);
float A = chanA * (5.0 / 1023.0);
float Amilli = (A * 0.41);
BTserial.print("\tAmilli: ");
BTserial.print(Amilli,2);
BTserial.println();
delay(500);

const int chanB = analogRead(A5);
float B = chanB * (5.0 / 1023.0);
float Bmilli = (B * 0.41);
BTserial.print("\tBmilli: ");
BTserial.print(Bmilli,2);
BTserial.println();
delay(500);
if (BTserial.available() > 0)
{
char val = BTserial.read();
} } }

I have used while(1) in the power sketch without issue. I tried the power part in Set Up however it will not compile without errors.

Then let’s fix those errors. Post the code here for advice

I note that your latest code still uses pins 0 and 1 for SoftSerial

I know enough just to be dangerous, Here is the latest code. Running it this morning checking outputs on pins 3&5 with a scope I get my PW going into solid high 5 volts. But because its still in the loop its dropping off for a few milli seconds then back to high. Pins for serial soft are 0/1 is hardwired on to the PCB board, I really can’t charge this. I am using a nano board with its own power supply. I did place the power control into setup and ended with errors.
Thank You
Don

#include <avr/io.h>
#include <SoftwareSerial.h>

SoftwareSerial BTserial(0, 1); // RX |

int PowerP1= 3;//power
int PowerP2= 5;//Power
int ledPin1= 2;//light

int A ;
int B ;
char val;
int fadeValue;

void setup() {
pinMode(ledPin1,OUTPUT);//red
pinMode(PowerP1,OUTPUT);
pinMode(PowerP2,OUTPUT);
/// digitalWrite(ledPin1,LOW);
delay(5000);
BTserial.begin(9600);
pinMode(A5, INPUT);//Btube
pinMode(A1, INPUT);//Atube
for (int fadeValue = 0; fadeValue >= 0; fadeValue += 10) {
// sets the value (range from 0 to 255):
analogWrite(PowerP1, fadeValue);
analogWrite(PowerP2, fadeValue);

if(fadeValue < 255)
{
digitalWrite(ledPin1,LOW);
}
else if (fadeValue >=255)
{
digitalWrite(ledPin1,HIGH);
digitalWrite(PowerP1,HIGH);
digitalWrite(PowerP2,HIGH);

}

void loop () {

////I need this to loop////
const int chanA = analogRead(A1);
float A = chanA * (5.0 / 1023.0);
float Amilli = (A * 0.41);
BTserial.print("\tAmilli: ");
BTserial.print(Amilli,2);
BTserial.println();
delay(500);

const int chanB = analogRead(A5);
float B = chanB * (5.0 / 1023.0);
float Bmilli = (B * 0.41);
BTserial.print("\tBmilli: ");
BTserial.print(Bmilli,2);
BTserial.println();
delay(500);
if (BTserial.available() > 0)
{
char val = BTserial.read();
} } }

You are missing a } at the end of setup()

The use of pins 0 and 1 will be a problem

PCB designed before proper testing?

    for (int fadeValue = 0; fadeValue >= 0; fadeValue += 10) 
    {
        .
        .
        .

Is it your intent to never leave this loop in setup()?

Also, the code you posted is missing at least two closing braces in setup, fairly easy to see when one formats the code and lines up and labels the braces.

   for (int fadeValue = 0; fadeValue >= 0; fadeValue += 10) 
    {
        // sets the value (range from 0 to 255):
        analogWrite(PowerP1, fadeValue);
        analogWrite(PowerP2, fadeValue);
  
        if(fadeValue < 255)
        {
            digitalWrite(ledPin1,LOW);  
        }//if
        else if (fadeValue >=255)
        {
            digitalWrite(ledPin1,HIGH);
            digitalWrite(PowerP1,HIGH);
            digitalWrite(PowerP2,HIGH);

        }//elseif
   
void loop () 
.
.
.

Why are these consts?

   const int chanA = analogRead(A1);
.
.
.
    const int chanB = analogRead(A5);

And there’s an extra brace after loop:

    if (BTserial.available() > 0)
    {
        char val = BTserial.read();
    }//if
    
}//loop

}//??

Are you sure you actually ran this code and not what was previously in the Arduino? Because I don’t think this compiles, let alone runs.