millis() and Bluetooth

I have this school project, it’s a solar panel tracker and Voltage tester through bluetooth at the same time.
I’m controlling the solar panel tracker with a simple Servo at pin11 with the comparator sensors (two LDRs) at A0 and A1. Next is the Bluetooth, I’m using a potentiometer as a representation for the varying voltage of a Solar panel. It is connected at A7. You can visually see the output voltage with an App that I made which is connected through a bluetooth shield to my arduino. My main problem is how I simultaneously run the both of them. The two codes work independently but when I integrated it, it didn’t work. The reading that is shown on my phone is always delayed while the program waited for the servo to align with the sensors. Also, I’ve tried using millis() but can’t seem to make it work.

Thank you for your help in advance!

#include <Servo.h> 
byte volt = A7;
Servo tracker;  // create servo object to control a servo 
int eastLDRPin = A0;  //Assign analogue pins
int westLDRPin = A2;
int eastLDR = 0;   //Create variables for the east and west sensor values
int westLDR = 0;
int error = 0;
int calibration = 10;  //Calibration offset to set error to zero when both sensors receive an equal amount of light
int trackerPos = 90; 
int Bsend = 10;//Create a variable to store the servo position

const int Binterval = 200; //Bluetooth interval
  unsigned long previousMillis1=0;
  unsigned long previousMillis2=0;
  unsigned long currentMillis;
  unsigned long currentMillis2;
const int Sinterval = 100; //Servo interval  
  unsigned long previousMillis3=0;
  unsigned long previousMillis4=0;
  unsigned long currentMillis3;
  unsigned long currentMillis4;
  unsigned long Bint=0;
  unsigned long Sint=0;
;

void setup() 
{ 
    pinMode(volt,INPUT);
Serial.begin(9600);
  tracker.attach(11);  // attaches the servo on pin 11 to the servo object
} 
 
 
void loop() 
{ 

  if (millis()  >= Binterval + Bint){
    Bint = millis();
     byte lvl = map(analogRead(volt),164,1023,0,20);;
     Serial.write(lvl);
  }

  if (millis() >= Sinterval + Sint) {
  Sint = millis();
  eastLDR = calibration + analogRead(eastLDRPin);    //Read the value of each of the east and west sensors
  westLDR = analogRead(westLDRPin);

  if(eastLDR<100 && westLDR<100)  //Check if both sensors detect very little light, night time
  {
    while(trackerPos<=160)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);

    }
  }
  error = eastLDR - westLDR;          //Determine the difference between the two sensors.

  if(error>15)        //If the error is positive and greater than 15 then move the tracker in the east direction
  {
    if(trackerPos<=160)  //Check that the tracker is not at the end of its limit in the east direction
    {
      trackerPos++;
      tracker.write(trackerPos);  //Move the tracker to the east
    }
  }  
  
  if(error<-15)  //If the error is negative and less than -15 then move the tracker in the west direction
  {
    if(trackerPos>10)  //Check that the tracker is not at the end of its limit in the west direction
    {
      trackerPos--;
      tracker.write(trackerPos);  //Move the tracker to the west
    }
  }

  if (15>error>-15){
}
}
}

electronicskid: My main problem is how I simultaneously run the both of them.

You have posted one program - which of the two is it?

Or, if it is your attempt at combining the programs please post the two separate working programs so that we can see all three programs.

And tell us exactly what happens when you try to run the combined program?

...R

  if (millis()  >= Binterval + Bint){

You should NOT be adding times. You SHOULD be using meaningful names. What the heck is a Bint?

    byte lvl = map(analogRead(volt),164,1023,0,20);;

You;; should;;;; pay; attention;;;;;; to where;; semicolons;;;;;; are needed, and;;; how many.

   while(trackerPos<=160)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);

    }

Why do you need a while loop? Just set trackerPos to 160, and write that one value to the servo. That while loop will complete long before the servo actually gets to 160.

 if (15>error>-15){

That is NOT how to test that a value is in a range. There are NO shortcuts.

these are my working codes

For the Servo

#include <Servo.h> 
byte volt = A7;
Servo tracker;  // create servo object to control a servo 
int eastLDRPin = A0;  //Assign analogue pins
int westLDRPin = A2;
int eastLDR = 0;   //Create variables for the east and west sensor values
int westLDR = 0;
int error = 0;
int calibration = 10;  //Calibration offset to set error to zero when both sensors receive an equal amount of light
int trackerPos = 90;    //Create a variable to store the servo position

void setup() 
{ 
   pinMode(volt,INPUT);
Serial.begin(9600);

 tracker.attach(11);  // attaches the servo on pin 11 to the servo object
} 


void loop() 
{ 
 eastLDR = calibration + analogRead(eastLDRPin);    //Read the value of each of the east and west sensors
 westLDR = analogRead(westLDRPin);

 if(eastLDR<100 && westLDR<100)  //Check if both sensors detect very little light, night time
 {
   while(trackerPos<=160)  //Move the tracker all the way back to face east for sunrise
   {
     trackerPos++;
     tracker.write(trackerPos);

     delay(100);
   }
 }
 error = eastLDR - westLDR;          //Determine the difference between the two sensors.

 if(error>15)        //If the error is positive and greater than 15 then move the tracker in the east direction
 {
   if(trackerPos<=160)  //Check that the tracker is not at the end of its limit in the east direction
   {
     trackerPos++;
     tracker.write(trackerPos);
     delay(100);
   }
 }  
 
 if(error<-15)  //If the error is negative and less than -15 then move the tracker in the west direction
 {
   if(trackerPos>10)  //Check that the tracker is not at the end of its limit in the west direction
   {
     trackerPos--;
     tracker.write(trackerPos); 
     delay(100);
   }
 }

if (15>error>-15){
delay(100);
}

}

And This is for the Bluetooth

byte volt = A7;
void setup() {
 pinMode(volt,INPUT);
Serial.begin(9600);
}

void loop() {


byte lvl = map(analogRead(volt),164,1023,0,20);
 Serial.write(lvl);
 delay(1000);

}

Thank you

Please use the code button </> so your programs are presented like the one you posted in your Original Post. It make it much easier to copy the code.

Have you taken account of the comments in Reply #2?

If so please post your revised program in your next Reply and tell us exactly what happens when you run it.

…R

if (millis()  >= Binterval + Bint){

should be…

if (millis() - Bint >= Binterval){
while(trackerPos<=160) {

should be…

if(trackerPos<=160) {
if (15>error>-15){

should be…

if (15 > error && error > -15){

Since you are looping through code, very fast, you want to avoid all “blocking” code. If you wait for a mechanical part, like a servo to move, You are not doing a lot of things you could be doing in the same time.

PaulS: a while loop? Just set trackerPos to 160, and write that one value to the servo. That while loop will complete long before the servo actually gets to 160.

How do I set the trackerPos?

I'm sorry, I'm a complete newbie with arduino for servos

electronicskid: How do I set the trackerPos?

I'm sorry, I'm a complete newbie with arduino for servos

Just change while to if, knowing that you are going to run through a loop and check it again in a few nanoseconds.

oh okay. thanks!

Tried the revision on my project but the problem is still reoccurring. The servo first adjusts while the arduino sends a good signal to the bluetooth shield, yet whenever I try to adjust the potentiometer it takes a long time before there is a change on the graph on my phone. Especially when I try to do it simultaneously. I try to change the direction of light (so that the servo will respond and readjust the tracker) and change the position of the potentiometer

This is my revised program

#include <Servo.h> 
byte volt = A7;
Servo tracker;  // create servo object to control a servo 
int eastLDRPin = A0;  //Assign analogue pins
int westLDRPin = A2;
int eastLDR = 0;   //Create variables for the east and west sensor values
int westLDR = 0;
int error = 0;
int calibration = 10;  //Calibration offset to set error to zero when both sensors receive an equal amount of light
int trackerPos = 90; 
int Bsend = 10;//Create a variable to store the servo position

const int Binterval = 200; //Bluetooth interval
  unsigned long previousMillis1=0;
  unsigned long previousMillis2=0;
  unsigned long currentMillis;
  unsigned long currentMillis2;
const int Sinterval = 100; //Servo interval  
  unsigned long previousMillis3=0;
  unsigned long previousMillis4=0;
  unsigned long currentMillis3;
  unsigned long currentMillis4;
  unsigned long Bint=0;
  unsigned long Sint=0;
;

void setup() 
{ 
    pinMode(volt,INPUT);
Serial.begin(9600);
  tracker.attach(11);  // attaches the servo on pin 11 to the servo object
} 
 
 
void loop() 
{ 

  if (millis() - Bint >= Binterval){
    Bint = millis();
     byte lvl = map(analogRead(volt),164,1023,0,20);
     Serial.write(lvl);
  }

  if (millis() -Sint >= Sinterval) {
  Sint = millis();
  eastLDR = calibration + analogRead(eastLDRPin);    //Read the value of each of the east and west sensors
  westLDR = analogRead(westLDRPin);

  if(eastLDR<100 && westLDR<100)  //Check if both sensors detect very little light, night time
  {
    if(trackerPos<=160)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);

    }
  }
  error = eastLDR - westLDR;          //Determine the difference between the two sensors.

  if(error>15)        //If the error is positive and greater than 15 then move the tracker in the east direction
  {
    if(trackerPos<=160)  //Check that the tracker is not at the end of its limit in the east direction
    {
      trackerPos++;
      tracker.write(trackerPos);  //Move the tracker to the east
    }
  }  
  
  if(error<-15)  //If the error is negative and less than -15 then move the tracker in the west direction
  {
    if(trackerPos>10)  //Check that the tracker is not at the end of its limit in the west direction
    {
      trackerPos--;
      tracker.write(trackerPos);  //Move the tracker to the west
    }
  }

  if (15>error && error>-15){
}
}
}
int Bsend = 10;//Create a variable to store the servo position

If you used a meaningful name, the comment would be unnecessary.

Getyourspacekeyfixed.

const int Binterval = 200; //Bluetooth interval

That comment is stupid. There is NO interval associated with bluetooth. YOU might wish to only send data after some interval, but that send interval is NOT a bluetooth interval.

Sending data after some interval is NOT the correct way to do it. Send the data immediately, when it changes. Of course, that means you need to keep track of what you sent last time, so you know when a change occurs.

const int Sinterval = 100; //Servo interval

Again, that is NOT a servo interval. Servos don’t have intervals.

  unsigned long currentMillis;
  unsigned long currentMillis2;
  unsigned long currentMillis3;
  unsigned long currentMillis4;

How many copies of now do you need?

  unsigned long previousMillis1=0;
  unsigned long previousMillis2=0;
  unsigned long previousMillis3=0;
  unsigned long previousMillis4=0;

More stupid names. These are supposed to hold the time that an event occurred. Name the variable for the event, not the unit of measure of the data in the variable.

I understand that some of my comments and the use of name on my code do not fit typical norms. I'm sorry, but what I really need now is how I could integrate both of my codes so that I could simultaneously change the position of the servo while my arduino is sending data to my phone on what is the current voltage of the potentiometer. I believe I could use millis() for this but I've tried I don't know why it won't work. I've tried it on both separate codes but whenever I try to integrate it, it just doesn't work. It always waits for another to finish what it's doing before it send the next data to the bluetooth

Thank you

It always waits for another to finish what it's doing before it send the next data to the bluetooth

Of course it does, because you can NOT do two things at the same time. You MUST stop writing blocking code, or ANY code that loops. The loop() function should do all the looping required.

You do not need to apologize for using stupid names. Just change them.

I think that when you do, you'll understand exactly why your code doesn't work the way you want.

You'll see that you are deliberately constraining the application to send data on a fixed (but possibly incorrect, because of the meaningless names) schedule, which is NOT what you want to do.

You want to send new data when the new data doesn't match the old data, regardless of what time it is when the data changes.

electronicskid: I'm sorry, but what I really need now is how I could integrate both of my codes so that I could simultaneously change the position of the servo while my arduino is sending data to my phone on what is the current voltage of the potentiometer.

It looks to me like the program you posted in Reply #10 should do that. But perhaps I am misunderstanding the rather cryptic variable names. I am assuming the the first piece of code in loop() (this bit)

if (millis() - Bint >= Binterval){
    Bint = millis();
     byte lvl = map(analogRead(volt),164,1023,0,20);
     Serial.write(lvl);
  }

is what sends the data.

If I have misunderstood then please correct me and tell us exactly what happens when you run the program and what you want it to do that is different.

...R

Yep, this sends the code to the Android

     byte lvl = map(analogRead(volt),164,1023,0,20);
     Serial.write(lvl);

Bint means Bluetooth Interval while Sint means Servo Interval

I'll try to rename my code then repost it

Hey guys! I tried to rename some of the code and de-clutter some of the comments. Please take time to read my code. Thank you

#include <Servo.h> 
byte volt = A7;
Servo tracker;         // create servo object to control a servo 
int eastLDRPin = A0;  //Assign analogue pins
int westLDRPin = A2;
int eastLDR = 0;        //Create variables for the east and west sensor values
int westLDR = 0;
int error = 0;
int calibration = 10;  //Calibration offset to set error to zero when both sensors receive an equal amount of light
int trackerPos = 90; 
int Bsend = 10;       //Create a variable to store the servo position

const int Binterval = 200; //Bluetooth interval
const int Sinterval = 100; //Servo interval
  unsigned long previousMillis;
  unsigned long previousMillis2;
  

;

void setup() 
{ 
    pinMode(volt,INPUT);
    Serial.begin(9600);
    tracker.attach(11);  // attaches the servo on pin 11 to the servo object
} 
 
 
void loop() 
{ 

  if (millis() - currentMillis >= Binterval){
    previousMillis = millis(); 
     byte lvl = map(analogRead(volt),164,1023,0,20); //Maps the Values of the voltages sensed at A7
     Serial.write(lvl);   //Sends the Data to the Android
  }

  if (millis() - currentMillis2 >= Sinterval) {
  previousMillis2 = millis();
  eastLDR = calibration + analogRead(eastLDRPin);    //Read the value of each of the east and west sensors
  westLDR = analogRead(westLDRPin);

  if(eastLDR<100 && westLDR<100)  //Check if both sensors detect very little light, night time
  {
    if(trackerPos<=160)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);

    }
  }
  error = eastLDR - westLDR;          //Determine the difference between the two sensors.

  if(error>15)        //If the error is positive and greater than 15 then move the tracker in the east direction
  {
    if(trackerPos<=160)  //Check that the tracker is not at the end of its limit in the east direction
    {
      trackerPos++;
      tracker.write(trackerPos);  //Move the tracker to the east
    }
  }  
  
  if(error<-15)  //If the error is negative and less than -15 then move the tracker in the west direction
  {
    if(trackerPos>10)  //Check that the tracker is not at the end of its limit in the west direction
    {
      trackerPos--;
      tracker.write(trackerPos);  //Move the tracker to the west
    }
  }

  if (15>error && error>-15){ //When the sensors read that it is equal or withing specified range, it holds the current postion of the servo
}
}

electronicskid: Hey guys! I tried to rename some of the code and de-clutter some of the comments. Please take time to read my code. Thank you

That program is not complete.

And it looks like you have not used the AutoFormat tool to indent it consistently. Doing that make it much easier to identify the different logical blocks.

As far as naming variables is concerned, I see this

int eastLDR = 0;        //Create variables for the east and west sensor values

If you called the variable

int eastLDRsensorValue = 0;

there would be no need for the comment.

...R

I tried to rename some of the code

const int Binterval = 200; //Bluetooth interval
const int Sinterval = 100; //Servo interval
  unsigned long previousMillis;
  unsigned long previousMillis2;

You didn't try hard enough. These 4 lines still have stupid names.

;

Well, that's useful... NOT!

What does the code actually do now, and how does that differ from what you want?