BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Unable to retrieve values from an int function

Does anyone have any idea on how to grab the variable value from another function to the function in which you’re pushing data to Blynk ?

I am trying to push the value of variable a that is present in int readcolor () function and void reoLoop() function

#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX   
#include <BlynkSimpleStream.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
 char auth[] = "ffde7e4bea3c47f0a1bc96a9a9e4e91a";
SimpleTimer timer;

#include <LiquidCrystal.h>
#include <Servo.h>
#define S0 6
#define S1 7
#define S2 8
#define S3 9
#define sensorOut 10
Servo topServo;
Servo bottomServo;
int frequency = 0;
int color=0;
int a=1;
int b=1;
int c=1;
int d=1;
int e=1;
int j=65;

LiquidCrystal lcd2(12, 11, 5, 4, 3, 2);
void setup() {
  
  lcd2.begin (16,2); 
  lcd2.setCursor (1,0);
  lcd2.print("RED=");
  lcd2.setCursor (9,0);
  lcd2.print("YLW=");
  lcd2.setCursor (1,1);
  lcd2.print("BL=");
  lcd2.setCursor (6,1);
  lcd2.print("GN=");
  lcd2.setCursor (11,1);
  lcd2.print("BN=");
 
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  // Setting frequency-scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  topServo.attach(14);
  bottomServo.attach(15);
 
Serial.begin(9600);
  Blynk.begin(Serial, auth);


  // Setup a function to be called every second
  timer.setInterval(1000L, sendSeconds);
  // Setup a function to be called every second
  timer.setInterval(1000L, reoLoop);
}

void sendSeconds() {
  
 Blynk.virtualWrite(V6,a);
 
 
}

void loop() {     
  Blynk.run();
  timer.run();
} 
 void reoLoop () {
    topServo.write(115);
  delay(500);
  
  for(int i = 115; i > 65; i--) {
    topServo.write(i);
    delay(2);
  }
  delay(500); 
  
  color = readColor();
  delay(10);  
  switch (color) {
    case 1:
    bottomServo.write(25);
    a ++;// Red
    
   
    break;
    case 2:
    bottomServo.write(38);  
   
    b ++;// yellow
   
    
    break;
    case 3:
    bottomServo.write(66);
  
    c++;// Green
   
    break;
    case 4:
    bottomServo.write(94);
   
    d ++;// Brown
   
    break;
    case 5:
    bottomServo.write(125);
    e ++; // Blue
 
    break;
    
    case 0:
    break;
  }
  delay(300);
  
  for(int i = 65; i > 29; i--) {
    topServo.write(i);
    delay(2);
  } 
  delay(200);
  
  for(int i = 29; i < 115; i++) {
    topServo.write(i);
    delay(2);
  }
  color=0;
}


// Custom Function - readColor()
int readColor() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int R = frequency;
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);
  
  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int G = frequency;
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);
  
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int B = frequency;
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.println("  ");
  delay(50);

if(R<B & B<G){
    color = 1; 
    lcd2.setCursor (5,0);
    lcd2.print(a);
    delay(500); //Red
  }
  if(R<G & G<B){
    color = 2; 
    lcd2.setCursor (13,0);
    lcd2.print(b); 
    delay(500); //Yellow
  }
  if(G<B & B<R){
    color = 3; 
 
    lcd2.setCursor (9,1);
    lcd2.print(c);
    delay(500); //Greem
  }
  if(B<R & R<G){
    color = 4;
   
    lcd2.setCursor (14,1);
    lcd2.print(d);
    delay(500); // Brown
  }
  if (B<G & G<R){
    color = 5; 
   
    lcd2.setCursor (4,1);
    lcd2.print(e);
    delay(500); //Blue

  }
  return color;  
} 


I don’t understand your question. Can you explain more, and which variable you’re referring to?

Pete.

A Count is being created every time the sensor detects a color, so if the sensor picks up red I’ve stored the count in variable a in the code. But when I use the Blynk.virtualWrite(V2, a) it does not show on Blynk, it’s just a blank value display widget.