Help with pointers

Status
Not open for further replies.

APutz

Member
I am having trouble writing code that adds or subtracks from the value of a variable with the turn of an encoder on my teensy 3.2. I want to be able to assign what variable is to be increased/decreased by calling a function. Here is the code I have so far.

Code:
#include "Encoder.h"

Encoder enc(16,15);

int number1 = 100;
int number2 = 25;

int **ptrActive;

int encCount;
int encCountOld;
int encMin;
int encMax;

void encGrab(int min, int max, int *ptr1){  //assigns what variable is to be manipulated by encoder

	ptrActive = &ptr1;
	enc.write(*ptr1);
	encCount = *ptr1;
	encCountOld = encCount;
	
	encMin = min;
	encMax = max;
	
}

void encScan(){                                     //scan for change in encoder, increase/decrease accordingly
	
	encCount = enc.read();
	if (encCount != encCountOld){
		
		if (encCount < encMin){
			
			encCount = encMin;
		}
		if (encCount > encMax){
			
			encCount = encMax;
		}
						
		enc.write(encCount);
		encCountOld = encCount;
		**ptrActive = encCount;
		
		Serial.println(**ptrActive);
		
	}		
}


void setup() {

	Serial.begin(9600);

}

void loop() {
	
	encGrab(0, 127, &number1);                 //placed outside while() for testing. encGrab() doesnt work from setup()
	
	while(1){
		
		encScan();
		
	}	
}

The code in this form works just fine. The problem occurs when I try to call encGrab() from outside the main loop(), the teensy just crashes or doesnt run at all (for instance if i call it from setup() ). This is a problem for me as I want to be able to toggle which variable is being manipulated by the encoder with the press of a button. I am sure there is something wrong with the way i'm setting up my pointers, but im not seeing it. Thanks!
 
I think it may be luck that your code works.

This statement stores the address of ptr1, which is somewhere on the stack.
Code:
	ptrActive = &ptr1;

I've changed the pointers a bit and this at least works even when encGrab is in setup.
Code:
#include "Encoder.h"

Encoder enc(16,15);

int number1 = 100;
int number2 = 25;

int *ptrActive;

int encCount;
int encCountOld;
int encMin;
int encMax;

void encGrab(int min, int max, int *ptr1)   //assigns what variable is to be manipulated by encoder
{

  ptrActive = ptr1;
  enc.write(*ptr1);
  encCount = *ptr1;
  encCountOld = encCount;

  encMin = min;
  encMax = max;

}

void encScan()                                      //scan for change in encoder, increase/decrease accordingly
{

  encCount = enc.read();
  if (encCount != encCountOld) {

    if (encCount < encMin) {
      encCount = encMin;
    }
    if (encCount > encMax) {
      encCount = encMax;
    }

    enc.write(encCount);
    encCountOld = encCount;
    *ptrActive = encCount;

    Serial.println(*ptrActive);
  }
}


void setup()
{
  Serial.begin(9600);
  while(!Serial);
  delay(100);
  Serial.println("Start");
}

void loop()
{
  encGrab(0, 127, &number1);                 //placed outside while() for testing. encGrab() doesnt work from setup()
Serial.println("End");
  while(1) {
    encScan();
  }
}

Pete
 
Ah, I see what I was doing wrong. Gracias el_Supremo.


I think it may be luck that your code works.

This statement stores the address of ptr1, which is somewhere on the stack.
Code:
	ptrActive = &ptr1;

I've changed the pointers a bit and this at least works even when encGrab is in setup.
Code:
#include "Encoder.h"

Encoder enc(16,15);

int number1 = 100;
int number2 = 25;

int *ptrActive;

int encCount;
int encCountOld;
int encMin;
int encMax;

void encGrab(int min, int max, int *ptr1)   //assigns what variable is to be manipulated by encoder
{

  ptrActive = ptr1;
  enc.write(*ptr1);
  encCount = *ptr1;
  encCountOld = encCount;

  encMin = min;
  encMax = max;

}

void encScan()                                      //scan for change in encoder, increase/decrease accordingly
{

  encCount = enc.read();
  if (encCount != encCountOld) {

    if (encCount < encMin) {
      encCount = encMin;
    }
    if (encCount > encMax) {
      encCount = encMax;
    }

    enc.write(encCount);
    encCountOld = encCount;
    *ptrActive = encCount;

    Serial.println(*ptrActive);
  }
}


void setup()
{
  Serial.begin(9600);
  while(!Serial);
  delay(100);
  Serial.println("Start");
}

void loop()
{
  encGrab(0, 127, &number1);                 //placed outside while() for testing. encGrab() doesnt work from setup()
Serial.println("End");
  while(1) {
    encScan();
  }
}

Pete
 
Status
Not open for further replies.
Back
Top