Why is this not tranfering? one int to another int?

Hi all,
I'm sure ive done this before but cant work out why its not working.

The result it get is :

...
TOTAL: 83
LAST TOTAL :0
TOTAL: 86
LAST TOTAL :0
TOTAL: 89
LAST TOTAL :0
TOTAL: 90
LAST TOTAL :0
...

ie the total isnt getting moved to the last total variable..

// Arduino VW buggy controller v1.1

void setup(){
 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);
 pinMode(5,INPUT);
 pinMode(6,INPUT);
 pinMode(7,INPUT);
 pinMode(8,INPUT);
  
  Serial.begin(9600);       


}

void loop(){
 int total;
 int lasttotal;
 lasttotal=total; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< is this where my problem lies?
 total = totalcounter();

Serial.print("TOTAL: ");
Serial.println(total);
Serial.print("LAST TOTAL :");
Serial.println(lasttotal);
delay(1000);

}

int totalcounter(){
  int pins = 7;
  int i;
  int temptotal;
  int total;
  int currentpin;
  int currentval;
  total=0;
  for(i=0;i<pins;i++){
    currentpin = i+2;
    currentval = pow(2,i)+0.5;
    temptotal = countercheck(currentpin,currentval);
    total = temptotal + total;
  }
  return(total);
}

int countercheck(int cnt, int value){
  
  if(digitalRead(cnt)==0){
    return (0);
  }
  else if(digitalRead(cnt)==1){
    return (value);
  }
 
}

Thanks in advance :slight_smile:

You shouldn't use a local variable for that. Make it static or global.

you legend, im an idiot! lol
Too many hours sorting out the cuircuity last night lol

void loop(){
 int total;
 int lasttotal;
 lasttotal=total; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< is this where my problem lies?

total is not initialized either ... // you might use setup() for that :wink: