error message/help with code.

error message and led messing up for code.

Z:\arduino\Voltage\Voltage.ino: In function ‘long int rgb(int, int, int)’:

Z:\arduino\Voltage\Voltage.ino:21:1: warning: no return statement in function returning non-void [-Wreturn-type]

}

^

code is below.

#define ANALOG_IN_Ch1 0
#define ANALOG_IN_Ch2 1
#define ANALOG_IN_Ch3 2
const int rgbred  = 2;
const int rgbgrn  = 3;
const int rgbblu  = 4;
void setup(){
  Serial.begin(19200);
  pinMode(ANALOG_IN_Ch1,INPUT);
  pinMode(ANALOG_IN_Ch2,INPUT);
  pinMode(ANALOG_IN_Ch3,INPUT);
  pinMode(rgbred,OUTPUT);
  pinMode(rgbgrn,OUTPUT);
  pinMode(rgbblu,OUTPUT);
}
long rgb(int red, int green, int blue) {
  analogWrite(rgbred, (red));
  analogWrite(rgbgrn, (green));
  analogWrite(rgbblu, (blue));
}
void loop(){
  int val1 = analogRead(ANALOG_IN_Ch1);
  int val2 = analogRead(ANALOG_IN_Ch2);
  int val3 = analogRead(ANALOG_IN_Ch3);
  int cnt = 0;
  int v1 = map(val1,0,1023,0,5.0);
  int v2 = map(val2,0,1023,0,5.0);
  int v3 = map(val3,0,1023,0,5.0);
  if(v1 >= 4.8 || v1 == -1){cnt = cnt+1;}
  if(v2 >= 4.8 || v2 == -1){cnt = cnt+1;}
  if(v3 >= 4.8 || v3 == -1){cnt = cnt+1;}
  if(cnt == 0){
    rgb(255,0,0);
  }else if(cnt == 1){
    rgb(255,165,0);
  }else if(cnt == 2){
    rgb(255,255,0);
  }else if(cnt == 3){
    rgb(0,255,0);
  }
  //Serial.print(cnt);
  Serial.print('H'); /* Unique header to identify start of message */
  Serial.print(",");
  Serial.print(val1,DEC);
  Serial.print(",");
  Serial.print(val2,DEC);
  Serial.print(",");
  Serial.print(val3,DEC);
  Serial.print(",");  /* Note that a comma is sent after the last field */
  Serial.println();  /* send a cr/lf */
  delay(90);
}

what im trying to do is when my 3 little caps reach at 5 volts the led is suppose to change colors for each one that gets full. but its constantly blinking from red to green and repeat.

long rgb(int red, int green, int blue) {
  analogWrite(rgbred, (red));
  analogWrite(rgbgrn, (green));
  analogWrite(rgbblu, (blue));
}

You promised the compiler that you’d return a value from this function., but didn’t.

Pants on fire.

i dont know how to fix that. im not trying to send anything back im simply trying to control the rgb light.

So, tell the compiler you're not sending anything back, in the same way you tell it "setup()" and "loop()" don't send anything back.
(the compiler really doesn't care which is why it's only a warning)

dude. i swear....i had it set to void(obviously) before with nothing changed and the damn thing gave a critical error. I dont know what happened.

But now that we got that odd mistake out of the way. the LED problem. its only going green/red. Meaning the count isnt even jumping to 2 at all.

The led depends on the cnt variable. if you check the if for cnt i added an OR statement in there because sometimes the signal would blink -1 sometimes and it really hurt my eyes when i tried reading it with processing.exe. So i tinked it a bit and now its steady.

But moving back to the led. do you have any ideas? And if you have ideas to make my code more efficient please share.

if(v1 >= 4.8 || v1 == -1){cnt = cnt+1;}

Seems pretty unlikely that v1:
a) will have a fractional part
b) be less than zero.

i mapped the analogread to display in volts. so 4.8 would be 4.8v

int v1 = map(val1,0,1023,0,5.0);

after the map it checks if v1 is 4.8 or higher. this is the part where its screwing up. the cnt isnt updating as it would expected.

i mapped the analogread to display in volts. so 4.8 would be 4.8v

And then assigned the value to an integer.

Back to the drawing board.

4.00H,1014,1014,1013,
4.00H,1013,1013,1014,
4.00H,1014,1013,1013,

from serial monitor goes in increments of whole int numbers. I dont know where its getting converted or if i screwed up my map somehow to chip off the extra numbers for the volt.

changes to code

  float v1 = map(val1,0,1023.0,0,5.0);
  float v2 = map(val2,0,1023.0,0,5.0);
  float v3 = map(val3,0,1023.0,0,5.0);
  Serial.print(v1);
  Serial.print('H'); /* Unique header to identify start of message */
  Serial.print(",");

added a line above h

"map()" is a function returning an integer, whatever you may wish.
Much simpler is float v1 = (val1 * 5.0) / 1024.0;
Or better still, just use the value "val1", or just keep it all in integer millivolts.

ok im useless. i literally did it that way just last week and now i didnt even think of it.

Anyways after a night of rest i solved it.

#define ANALOG_IN_Ch1 0
#define ANALOG_IN_Ch2 1
#define ANALOG_IN_Ch3 2
const int rgbred  = 2;
const int rgbgrn  = 3;
const int rgbblu  = 4;
void setup(){
  Serial.begin(19200);
  pinMode(ANALOG_IN_Ch1,INPUT);
  pinMode(ANALOG_IN_Ch2,INPUT);
  pinMode(ANALOG_IN_Ch3,INPUT);
  pinMode(rgbred,OUTPUT);
  pinMode(rgbgrn,OUTPUT);
  pinMode(rgbblu,OUTPUT);
}
void rgb(int red, int green, int blue) {
  analogWrite(rgbred, (red)/2);
  analogWrite(rgbgrn, (green)/2);
  analogWrite(rgbblu, (blue)/2);
}
void loop(){
  int val1 = analogRead(ANALOG_IN_Ch1);
  int val2 = analogRead(ANALOG_IN_Ch2);
  int val3 = analogRead(ANALOG_IN_Ch3);
  float v1 = (val1 * 5.0) / 1024.0;
  float v2 = (val2 * 5.0) / 1024.0;
  float v3 = (val3 * 5.0) / 1024.0;
  float avg = (v1 + v2 + v3) / 3;
  if(avg >= 2.00 && avg <= 3.99){rgb(100,0,0);}
  else if(avg >= 4.00 && avg <= 4.89){rgb(0,0,100);}
  else if(avg >= 4.90){rgb(0,100,0);}
  else{rgb(0,0,0);}
  Serial.print('H'); /* Unique header to identify start of message */
  Serial.print(",");
  Serial.print(val1,DEC);
  Serial.print(",");
  Serial.print(val2,DEC);
  Serial.print(",");
  Serial.print(val3,DEC);
  Serial.print(",");  /* Note that a comma is sent after the last field */
  Serial.println();  /* send a cr/lf */
  delay(90);
}

the if statements werent catching right so i had to change it from 4.0 to 4.00 and etc. worked like a charm. detects the full charge on all capacitors and lights up green. it goes blue when its almost full and red for charging. anything else nothing.

Next step is understanding the voltage dividers to read values from caps higher than 5v. Im still trying to do the math part of it though. Im gonna try a 9v powersource.

According to math i have to get a 19kohm and 20kohm to get close to 5v readable values. Problem is i dont have those. soo…i gotta improvise.

But a question arose. how much can it withstand and how do i deal with the current pull. These areas are shady.