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 1993 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…
  • 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
  • mcb1
    mcb1 over 12 years ago in reply to e14 Contributor

    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 the very basic of arduino c programming language

    IT eBooks - Free Download - Big Library

    you should find a number there to download.

    I'd suggest this one Arduino Cookbook, 2nd Edition - Free Download eBook - pdf

     

     

    There is an example in the Arduino IDE called BASICS - Fade

    /*
     Fade
    
     This example shows how to fade an LED on pin 9
     using the analogWrite() function.
    
     This example code is in the public domain.
    
     */
    int brightness = 0;    // how bright the LED is
    int fadeAmount = 5;    // how many points to fade the LED by
    
    void setup()  { 
      // declare pin 9 to be an output:
      pinMode(9, OUTPUT);
    } 
    
    void loop()  { 
      // set the brightness of pin 9:
      analogWrite(9, brightness);    
    
      // change the brightness for next time through the loop:
      brightness = brightness + fadeAmount;
    
      // reverse the direction of the fading at the ends of the fade: 
      if (brightness == 0 || brightness == 255) {
        fadeAmount = -fadeAmount ; 
      }     
      // wait for 30 milliseconds to see the dimming effect    
      delay(30);                            
    }

     

    In this they basically change the value of the 'step' from the default of 5 to -5 (see line 27).

     

    As you can see there are several different ways to achieve the same result, some are faster than others but with a delay of 500 or 1000 who cares.

     

    You should also look at the example 'BlinkWithoutDelay' for methods of slowing down a process without using the delay function.

    This allows you to do other things while you're waiting. ie check an input.

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to mcb1

    thanks for all the info

    fantastic forum

    I want to learn more and more

    I'll post many other question soon

     

    cheers

    D

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

    Another solution to consider (angleForced = val)

     

    int direction = 1;

    int val = 180;

     

    void loop()

    {

       if (val == 0 || val == 180)

       {

             direction = direction * -1;

       }

       val = val + (10 * direction);

    }

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

    hi

    looks like a very elegant solution

    can you add same explanation on every line?

    cheers

    d

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

    Well, you start with 180 before going into the loop then in the loop:

    - whenever the value gets down to zero or up to 180 you want to change direction so the direction is either 1 or -1

    - now you update the value by adding ten each time going in the loop and factor in the direction

    - if the direction is positive (1) then the value goes up (and eventually hit 180)

    - if the direction is negative (-1) then the value goes down (and eventually hit 0)

    Cheers

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

    thanks for your time

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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 © 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