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 need help with arduino code
  • 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 8 replies
  • Subscribers 392 subscribers
  • Views 575 views
  • Users 0 members are here
  • code
  • darrenhuseby
  • question
Related

need help with arduino code

Former Member
Former Member over 10 years ago

[code] having trouble with figuring this code writing out , I am new to arduino and trying to output a pulse modulated voltage from 0-5v out of pin 6

I start at 128 right in the middle and I want to be able to push one button and increase the value and push another button and decrease the value. It is pretty much my first attempt at writing code so I dont know whats wrong but It actually works but the numbers for the variable (motorspeed) go way beyond 255 they just keep going up and up and down below zero as well can someone help me understand what I am doing wrong and how to fix it.

/*

Wireless pin detail:

Button   Arduino Digital Pin   Wireless Receiver

A          4                    D3

B          3                    D2

C          2                    D1

D          1                    D0

 

 

 

A: Speed up

B: Slow down

*/

 

 

 

 

const int Abutton = 8;

const int Bbutton = 9;

int motorspeed = 128;

int buttonAState = 0;

int buttonBState = 0;

 

 

void setup() {

pinMode(6, OUTPUT); // pulse width modulation output voltage to the motor controller in place of the potentiometer

pinMode(8, INPUT); //A button

pinMode(9, INPUT); //B button

Serial.begin(9600);

}

 

 

void loop() {

int buttonAState = digitalRead (Abutton);

delay(50);

  if (buttonAState == HIGH){

    analogWrite (6, motorspeed++);

  }

else

{

analogWrite (6, motorspeed);

}

 

 

int buttonBState = digitalRead (Bbutton);

delay(50);

if (buttonBState == HIGH) {

   analogWrite (6, motorspeed--);

}

 

 

else

{

analogWrite (6, motorspeed);

}

Serial.println(motorspeed);

if((motorspeed)>254);

analogWrite (motorspeed, 254);

if((motorspeed)<10);

analogWrite (motorspeed, 10);

}

 

 

[/code]

  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +2
    you are not limiting the chenges to the motor speed variable, it is an integer and therefor 16 bits on this platform (0 - 65535) or in this case as it is not an unsigned int -32767 to + 32768 you need…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to m.ratcliffe +2
    The If Else does impart a behaviour you need to be aware of If both buttons are pressed at the same time, rather than the net result being no increase or decrease, the loop will now favour increasing till…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to Former Member +2
    Make a short video and post it here, that would be awsome
Parents
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    you are not limiting the chenges to the motor speed variable, it is an integer and therefor 16 bits on this platform (0 - 65535) or in this case as it is not an unsigned int -32767 to + 32768

     

    you need a check in the loop along the lines of the following

     

    if (motorspeed < 0 ) motorspeed = 0;

    if (motorspeed > 255) motorspeed = 255;

    this will cap the value in each direction

     

    as your currently incrementing or decrementing inside the analogue write, then you can put these two statements anywhere in the loop to keep things in check as you have the ++ -- after the variable, it is performed after the variable is used, not before so the checks will still work without going under or over the right values

    it would probable be more elegent to manage the actual value outside of the analogue write and perform the checks before writing to the output.

     

    Hope this helps

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    you are not limiting the chenges to the motor speed variable, it is an integer and therefor 16 bits on this platform (0 - 65535) or in this case as it is not an unsigned int -32767 to + 32768

     

    you need a check in the loop along the lines of the following

     

    if (motorspeed < 0 ) motorspeed = 0;

    if (motorspeed > 255) motorspeed = 255;

    this will cap the value in each direction

     

    as your currently incrementing or decrementing inside the analogue write, then you can put these two statements anywhere in the loop to keep things in check as you have the ++ -- after the variable, it is performed after the variable is used, not before so the checks will still work without going under or over the right values

    it would probable be more elegent to manage the actual value outside of the analogue write and perform the checks before writing to the output.

     

    Hope this helps

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • 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 © 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