element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 arduino count (simple C question)
  • 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
  • Replies 9 replies
  • Subscribers 418 subscribers
  • Views 1994 views
  • Users 0 members are here
  • controlflow
  • programming
  • basic
  • c++
  • arduino
Related

arduino count (simple C question)

e14 Contributor
e14 Contributor over 12 years ago

Hi

another rookie question about coding with arduino

i simple need to make arduino to count from 0 to 180 with steps of 10 when it reach 180 I want that start to count from 180 to 0 and again again

I wrote this:

 

#include <Servo.h>

Servo myServo;

 

 

int const potPin = A0;

 

 

 

int angleForced=0;

void setup() {

  myServo.attach(9);

 

  Serial.begin(9600);

}

 

 

void loop() {

 

 

  Serial.print("angleFORCED: ");

  Serial.print(angleForced);

 

  if (angleForced < 179){

    angleForced=angleForced+10;

   

  } else if (angleForced >=179){

    angleForced=angleForced-10;

  }

 

  myServo.write(angleForced);

  delay(1000);

 

}

 

 

the problem in the code is:

when it reach 180 it count down to 170 and I get in to a loop of 180 170 180 numbers.

What I want is 0 10 20 30 ....to 180 170 160 ...to 0 etc etc

 

Cheers

D

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 12 years ago in reply to e14 Contributor +1
    Dani There have been many questions posted in the past regarding tutorials, with some good suggestions. You should be able to use the search function to find them. do you have a web side book that teach…
Parents
  • neilk
    neilk over 12 years ago

    Hi Dani

     

    You need a flag to show whether you are counting up or down.

     

    Here is a little sketch to illustrate a possible solution:

     

    /*

      demonstrate up down counter

      Neil Kenyon 01 February 2014

      Public domain

    */

    int count = 0;                   // counter value

    int upperLimit = 180;      // Upper limit

    int lowerLimit = 0;           // Lower limit

    boolean up = true;          // Counter direction flag

                                              // true says "countup"; false says "count down"

    void setup()

    {

      Serial.begin(9600);  

    }

     

    void loop()

    {

      Serial.println(count);

      Serial.println(up);                   // This will show 1 for true and 0 for false

     

      if (up ) {                                    // Only do this if we are counting up

        if (count < upperLimit ) {

          count = count + 10;

          if (count == upperLimit) {    // Reached upper limit

            up = false;                           // Set the flag false to count down

          }

        }

      }

      else if (count > lowerLimit) {

        count = count - 10;

        if (count == lowerLimit) {      // Reached lower limit

          up = true;                              // Set the flag false to count down

        }

      }

      delay(500); 

    }

     

    I hope this helps

     

    Best wishes

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to neilk

    thanks so much

    ill try as soon as possible

     

    it makes so sense

     

    do you have a web side book that teach the very basic of arduino c programming language

     

    I feel that even if I follow the tutorial in the arduino starter kit book I don't have the right background in C to over come issue that I found when I want to do something different then the tutorial.

     

    cheeeeers

     

    D

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • neilk
    neilk over 12 years ago in reply to e14 Contributor

    Dani

     

    I'm not a trained C programmer either. However, I have worked through ALL of Jeremy Blum's Tutorials, as well as lots of others you can find in the web.

     

    You'll soon pick it up.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • neilk
    neilk over 12 years ago in reply to e14 Contributor

    Dani

     

    I'm not a trained C programmer either. However, I have worked through ALL of Jeremy Blum's Tutorials, as well as lots of others you can find in the web.

     

    You'll soon pick it up.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
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 © 2026 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.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube