element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum how to have anologue input and while loop?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 2 replies
  • Subscribers 392 subscribers
  • Views 607 views
  • Users 0 members are here
Related

how to have anologue input and while loop?

Former Member
Former Member over 11 years ago

I have a program with a joystick and a LCD. The joystick controls "!"s on the LCD(makes them appear in different places. The joystick if made from potentiometers and when the value gets to a certain point a "!" is suppose to appear on a part of the screen using a while loop. When the value reaches the point to show a "!" on the screen the while loop starts and stops the anologue input from the potentiometers. This makes the value constant forever and the "!" never goes away. An IF statement hasn't worked because i need to clear the screen if the value is not correct for that "!" and that makes it impossible for other "!"s never appear. Are there any solutions to this or other ways of going about it? I am inexperienced so I am in much need of help. (BTW the code is made for four different potentiometers (Two joysticks) but I an only using one joystick (so two potentiometers, sensePinOrange with val1, and, sensePinYellow with val).) CODE:

 

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

 

int sensePinYellow =0;

int sensePinOrange =1;

int sensePinBrown =2;

int sensePinBlue =3;

int val = 0;

int val1 = 0;

int val2 = 0;

int val3 = 0;

 

 

void setup() {

  analogReference(DEFAULT);

  Serial.begin(9600);

 

  lcd.begin(16, 2);

 

 

}

void loop() {

    val = analogRead(sensePinYellow);

    val1 = analogRead(sensePinOrange);

    val2 = analogRead(sensePinBrown);

    val3 = analogRead(sensePinBlue);

    Serial. print(" Yellow=");

    Serial. print(val); /*930=right 210=left */

    Serial. print(" Orange=");

    Serial. print(val1); //925=maxup 195=mindown

    Serial. print(" Brown=");

    Serial. print(val2); //930=right 180=left

    Serial. print(" Blue=");

    Serial. println(val3); //920=maxup 150=mindown

    delay(50);

   

    while (val > 700) {

      lcd.setCursor(15,1); //right

      lcd.print("!");

    }

    while (val < 400) {

      lcd.setCursor(0,1); //left

      lcd.print("!");

    }

    while (val1 > 700) {

      lcd.setCursor(9,0); //up

      lcd.print("!");

    }

    while (val < 400) {

      lcd.setCursor(9,1); //down

      lcd.print("!");

    }

   

}

  • Sign in to reply
  • Cancel
  • michaelwylie
    0 michaelwylie over 11 years ago

    I would think about it as looking for a change.

    if(cursor changed)

    {

         clear all cursors

         write proper cursors

    }

    The harder part is determining if there was a change. So, think about keeping track of the previous values and comparing them to the newest read ones. If they are different (within tolerance) there was a change. Try thinking about this, and if you can't figure it out, come back an update this post.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    Your while loops have no exit criteria, as soon as you read a value within the capture range of one of the while loops it will never exit, exactly as you describe

     

    ie. you read all the values into variables

    then based on a variable you perform the while loop, as long as the variable, say Val >700 it will stay in the loop until val is not >700, as you never update val inside the loop, it will never continue out of the loop

     

    what would be better is something like this

     

    void loop() {

        val = analogRead(sensePinYellow);

        val1 = analogRead(sensePinOrange);
        Serial. print(" Yellow=");

        Serial. print(val);      /*930=right 210=left */

        Serial. print(" Orange=");

        Serial. print(val1);      //925=maxup 195=mindown

        delay(50);

    if (val > 700) {

          lcd.setCursor(15,1); //right

          lcd.print("!");

        }

        else if(val < 400) {

          lcd.setCursor(0,1); //left

          lcd.print("!");

        }

        if(val1 > 700) {

          lcd.setCursor(9,0); //up

          lcd.print("!");

        }

        else if(val1 < 400) { // I modified this to be Val 1 as it seemed to be what you where trying to do based on the rest of the code

          lcd.setCursor(9,1); //down

          lcd.print("!");

        }

     

    of course there is nothing to clear the screen indicators when it goes out of the range but you can figure that out im sure, and use what Michael describes as a clue

     

    Have fun

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube