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
  • m.ratcliffe
    0 m.ratcliffe over 10 years ago

    For Your First Code it isnt too bad.

    Just a few small problems on top of those mentioned above:

     

    >You already initialised buttonAState at the beginning of the code, no need to use int buttonAState in the main loop

    >You dont say what motor you are controlling [make sure its not too big to be driven off a digital pin]

    >Again no circuit diagram, so we dont know if you have external pull down resistors, the input pins are high impendance


    Why Pull downs: the input pins are high impedance and will stay at 5v after you stop pressing the button if you dont include some way to drain current, or go high randomly from noise etc [google it for more info]


    If you dont want to wire external resistors for the button you can do it like this:

    Make a pushbutton without a resistor


    This Code should fix the problems: [I have no arduino handy to test it, but it is a simple code and should work if you wire the button as described above]



    //*******************************************************************************************************************//

    /*

    Michael Ratcliffe  mike@michaelratcliffe.com

      

        This program is free software: you can redistribute it and/or modify

        it under the terms of the GNU General Public License as published by

        the Free Software Foundation, either version 3 of the License, or

        (at your option) any later version.

     

     

        This program is distributed in the hope that it will be useful,

        but WITHOUT ANY WARRANTY; without even the implied warranty of

        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

        GNU General Public License for more details.

     

     

        You should have received a copy of the GNU General Public License

        along with this program.  If not, see <http://www.gnu.org/licenses/>.

     

    Wireless pin detail:

    Remember the Button Is now Connected To Ground #################################

     

    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;

    const int MotorPin =6;

    int motorspeed = 128;

    int buttonAState = 0;

    int buttonBState = 0;

     

    void setup() {

    pinMode(MotorPin, OUTPUT);                // MotorPin

    pinMode(Abutton, INPUT_PULLUP);       //A button & turn on pullup resistors

    pinMode(Bbutton,INPUT_PULLUP);     //B button &turn on pullup resistors

    Serial.begin(9600);

    }

     

    void loop() {

                    buttonAState = digitalRead (Abutton);

                    delay(10);//not needed

                    buttonBState = digitalRead (Bbutton);

     

    //********** If Else Because there is no need to do the second request if the first one is satisfied **//

    if (buttonAState == LOW){

         motorspeed++;

      }

     

    else if(buttonBState ==LOW){

      motorspeed--;

    }

                  if((motorspeed)>254);

                  motorspeed=254;

                

                  if((motorspeed)<10);

                  motorspeed= 10;

     

    analogWrite (MotorPin, motorspeed);

     

    Serial.print("PWM: ");

    Serial.println(motorspeed);

     

    delay(100);

    };

    //*************************************************************************************************************************//

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to m.ratcliffe

    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 the MAX is reached. If you want the behaviour to be such that both buttons means stay at the current value, simply remove the "else" from the else if.

     

    asside from that, Michaels solution is a good fix for your problem and he also presents several good coding practices

     

    namely

    COMMENTING THE CODE

    Using well definesd and PRE-INITIALIZED variables

    An upfront description and Licence declaration

    Use of not often used parameters in defining the functionality of the pins, namely the INPUT_PULLUP which in many cases removes the need for an external pullup resistor on a switch... nice

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

    You guys are awesome ! Thank you so much, it is working great. The motor speed is actually going to a signal input pin on a gear motor speed control. It's working perfectly and I am welding pipe with it today. Thanks so much !!!!

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

    Make a short video and post it here, that would be awsome

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago in reply to Former Member

    Hi Darren.

     

    Thanks for the feedback. It's nice to hear a success story. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • gadget.iom
    0 gadget.iom over 10 years ago in reply to Former Member

    Hi Darren.

     

    Thanks for the feedback. It's nice to hear a success story. image

    • Cancel
    • Vote Up 0 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