Can't get a loop to work.

Hi,

I’m fairly new to Arduino so I’m probably missing something small.

I’m using the new Microview so you’ll see some calls to the uView library.

My idea was to create a combination lock using a pot as the selector dial. Starting out I’m just trying to get the Microview to print some simple instructions on the screen and leave them there until the pot is moved. If the pot is moved the screen should clear and then display the Microview slider widget.

The problem is that the loop doesn’t work. It displays the instructions for a flash, then displays the widget even though the pot hasn’t moved. I’ve tried with a “while” and “do while” loop with the same results. I’ve also taken the pot out of the picture setting the variables equal. Same result.

Any help would be greatly appreciated.

#include <MicroView.h>			// include MicroView library

MicroViewWidget *widget;		// create widget pointer

int sensorPin = A1;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int currentPosition = 0;
int lastPosition = 0;

void setup()
{
	//digitalWrite(sensorPin, HIGH);		// Internal Pull-up
	pinMode(sensorPin, INPUT);			// make pin as INPUT
	uView.begin();					// start MicroView
	uView.clear(PAGE);			        // clear page
}

void loop()
{
	// Read current position of the pot
	currentPosition = analogRead(sensorPin);
        lastPosition = currentPosition;                //m ake sure that last and current posstions are the same to start with
        
        //Print instructions on the Microview
        uView.print("select the	first number.");
	uView.display();
	
        // Loop until the pot posstion changes leaving the instructions ont he screen.
	if (currentPosition == lastPosition)
          {
            currentPosition = analogRead(sensorPin);
          }
	
	uView.clear(PAGE);                                    // clear the page
	widget = new MicroViewSlider(0, 0, 0, 30);	      // make widget as Slider
	uView.display();                                      // display the widget
	sensorValue = analogRead(sensorPin);	              // read sensorPin
	int outputValue = map(sensorValue, 0, 1023, 0, 30);   // map the output value between 0 and 30
	widget->setValue(outputValue);			      // set value of sensorPin to widget
	uView.display();               			      // display the content in the screen buffer
        delay(1000);

I don’t know if I get the right idea, but try this:

#include <MicroView.h>			// include MicroView library

MicroViewWidget *widget;		// create widget pointer

int sensorPin = A1;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int currentPosition = 0;
int lastPosition = 0;

void setup()
{
  //digitalWrite(sensorPin, HIGH);		// Internal Pull-up
  pinMode(sensorPin, INPUT);			// make pin as INPUT
  uView.begin();					// start MicroView
  uView.clear(PAGE);			        // clear page
}

void loop()
{
  // Read current position of the pot
  currentPosition = analogRead(sensorPin);

  //Print instructions on the Microview
  uView.print("select the	first number.");
  uView.display();

  // Loop until the pot posstion changes leaving the instructions ont he screen.
  if (currentPosition != lastPosition)
  {
    lastPosition = currentPosition;                //m ake sure that last and current posstions are the same to start with
    uView.clear(PAGE);                                    // clear the page
    widget = new MicroViewSlider(0, 0, 0, 30);	      // make widget as Slider
    uView.display();                                      // display the widget
    sensorValue = analogRead(sensorPin);	              // read sensorPin
    int outputValue = map(sensorValue, 0, 1023, 0, 30);   // map the output value between 0 and 30
    widget->setValue(outputValue);			      // set value of sensorPin to widget
    uView.display();               			      // display the content in the screen buffer
    delay(1000);
  }

Thanks for the quick response luisilva.

However, I tried this as well with the same results.

In addition if I add the following code under the void loop() it repeatedly prints the instruction. It's like the "if" loop is being completely ignored. :-(

lastPosition = currentPosition;

Of course that is ignored, because it only be executed if you move the pot, and:

  if (currentPosition != lastPosition)

the currentPosition is different from the lastPosition.

EDIT: I don't believe this:

robinvfr: (...) However, I tried this as well with the same results. (...)

Can anyone see a reason why my original code shouldn't work? I tried luisilva's suggestion, but I got the same results. I'm stumped! LOL

        while (lastPosition == currentPosition)
        {
          currentPosition = analogRead(sensorPin);
          lastPosition = currentPosition
        }

Note, however, that this relies on the pot returning exactly the same value all the time when stationary which is unlikely. It would be better to build in a check to see of the pot value has changed and by how much.

        while (abs(lastPosition - currentPosition) < 20)