0

See the picture of serial monitor showing I get a large number, 4 billion, when I multiply two called functions together. It always happens after 53 if I change the amount the if statement counts to. I serial monitored the if statement and it counts to 600 then down then up and repeats good and the sensorValue stays the same.
I get rid of the division in y and I put it all in the for loop and still get a large number. My purpose is to fade pin and adjusts analogWrite at same time. It adjusts but does not fade. Does replacing for loop with if statement or calling to many functions create a bug.


int analogInPin = A0;
int analogOutPin3 = 3;

int sensorValue;

int i = 0;
int a;
int y;

void sens(int &sensorValue){
  sensorValue = (((long)analogRead(analogInPin)*255)/1023);   
}

void inOut(int &i){ 
  if (i > -1){
    i = i + a;      
  if (i == 0){
     a = 1;
  }
  if (i == 600){
    a = -1;
  } 
  return i;
 }
}

void updateFade(){
  sens(sensorValue);
  inOut(i);
  unsigned long y = ((sensorValue*i)/600);
  Serial.println(sensorValue);
}

void setup() {
  Serial.begin(9600);
  pinMode(analogInPin,INPUT);
  pinMode(3,OUTPUT);
}

void loop() { 
  updateFade();
  delay(100); 
  analogWrite(3, y);
}

enter image description here

4
  • Signed into unsigned doesn't go. If i is -1 then what is y going to be?
    – Majenko
    Mar 28 '20 at 16:28
  • y will always be 0 because the one calculated inside that function is local to it and dies when the function ends without having ever been used. So the y that is used int he analogWrite in loop will always be 0.
    – Delta_G
    Mar 28 '20 at 17:50
  • If I can make another unrelated suggestion, your sens function doesn't need to take any parameters. It could just be sens(). Since sensorValue is global, you can modify it and access it from anywhere in the program. You don't need to pass it around as an argument. You only need the whole pass by reference thing if you are trying to modify local variables in one function from another function. But that's not what you're doing here. You've got a global variable. Same thing with your inOut function. i is global, so you can just use it. You don't need to pass it around.
    – Delta_G
    Mar 28 '20 at 17:54
  • these are all the same integer .... binary 1111 1111 1111 1111 1111 1111 1100 1010 == hexadecimal FFFFFFCA == unsigned decimal 4294967242 == signed decimal -54
    – jsotola
    Mar 28 '20 at 17:59
2

You define i,y,i as global variables so there is no need to hand them over to different functions. They can be accessed and changed in all parts of your program.

define instead of

int y; 

change to

long y;

and change

 void inOut(int &i){ 

to

void inOut(){ 

and remove

   return i;// Its a global var

change the line

unsigned long y = ((sensorValue*i)/600);

to

y = ((sensorValue*i)/600);

Try to avoid the same names for global and local vars. It might happen that the compiler handles it as a redefinition, which might lead to all sort of trouble in complex programs. (Also for you if you look onto an ill documented code some months later)
And get rid of the delay it stops processing (also your subroutines) see Blinkwithoutdelay how to do it properly
ArduinoIDE->file->examples->2 Digital->Blinkwithoutdelay

1

Thanks for the help, this is my working code. I changed things to long, got rid of some long's, stopped passing variables, and forced number in sensorValue to be long. It now counts up and down were I set the potentiometer every minute.


int analogInPin = A0;
int analogOutPin3 = 3;

long sensorValue;

int i = 0;
int a;
long y;

void sens(){
  sensorValue = ((analogRead(analogInPin)*255L)/1023); //force 255 to be long  
}

void inOut(){ 
  if (i > -1){
    i = i + a;      
  if (i == 0){
     a = 1;
  }
  if (i == 600){
    a = -1;
  } 
 }
}

void updateFade(){
  sens();
  inOut();
  y = ((sensorValue*i)/600);
  Serial.println(y);
}

void setup() {
  Serial.begin(9600);
  pinMode(analogInPin,INPUT);
  pinMode(3,OUTPUT);
}

void loop() { 
  updateFade();
  delay(100); 
  analogWrite(3, y);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.