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 limit switches within a counter
  • 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 Verified Answer
  • Replies 10 replies
  • Answers 2 answers
  • Subscribers 392 subscribers
  • Views 2289 views
  • Users 0 members are here
Related

limit switches within a counter

Former Member
Former Member over 11 years ago

Hi folks,

Im a technology teacher in Scotland and the powers above have decided that we move from our old PBASIC run Stamp boards, and use Arduino. It has also been decided this will start of next month! this means during my holidays I have been left with no choice but to learn the language and how it works myself, but to create a scheme of work for my pupils to follow. Most of what Ive done is based on Jeremy Blums brilliant online tutorials , and things that are different I was mostly able to work out by looking up.

 

I have came across a problem though and for the life of my I cant seem to solve it and I was hoping someone on here could help! Ive designed a project of dancing ballerina toy (my feeble attempts to try and get more girls into Engineering). When a master switch is pressed, it will put on an ultabrite LED to act as a spotlight for the dancer. The ballerina will then "dance" by sitting on top of a turntable that will rotate one way until it hits a limit switch. When it hits this limit switch it will rotate the other direction until it hits another limit switch, sending it back the original direction. This dancing will repeat 5 times, then everything will switch off.

 

This is a flowchart of what I'm attempting to do, which will hopefully make things a little clearer:

 

image

 

This is how I have wired up my Arduino Uno:

 

image

 

and this is my attempt at the code. It was originally more complicated added in debouncing and commands to keep outputs on once the switches have been presssed, but I deleted all that to try and work out what is going wrong.

 

/*

dancing toy System

*/

 

int MasterSwitchPin = 13;      // Master switch

int LeftSwitchPin = 12;        // Makes motor turn left

int RightSwitchPin = 8;        // Makes motor turn right

int Motor1APin = 7;            // H-bridge leg 1

int Motor2APin = 6;            // H-bridge leg 2

int LEDPin = 2;                // LED

 

 

void setup()

{

  Serial.begin(9600);

  pinMode (MasterSwitchPin, INPUT);

  pinMode (LeftSwitchPin, INPUT);

  pinMode (RightSwitchPin, INPUT);

  pinMode (Motor1APin, OUTPUT);

  pinMode (Motor2APin, OUTPUT);

  pinMode (LEDPin, OUTPUT);

}

 

void loop()

{

   if (digitalRead(MasterSwitchPin) == HIGH)

     {

       digitalWrite(LEDPin, HIGH);

     }

     Serial.print("button pressed LED on");

for (int counter = 1 ; counter <= 5; counter = counter +1) 

   { 

    while (digitalRead(LeftSwitchPin) == HIGH)

      {

      digitalWrite(Motor1APin, HIGH);

      digitalWrite(Motor2APin, LOW);

      Serial.print("motor goes one way");

      }      

      delay(1000);

   

    while (digitalRead(RightSwitchPin) == HIGH)

      {

      digitalWrite(Motor1APin, LOW);

      digitalWrite(Motor2APin, HIGH);

      Serial.print("motor goes the other wat way");

      }

  delay(1000);     

   }

   Serial.print("repeated 5 times");

       digitalWrite(LEDPin, LOW);

       digitalWrite(Motor1APin, LOW);

       digitalWrite(Motor2APin, LOW);

       Serial.print("switch everything off");

}

 

 

can anyone help me please and fix my code? thanks in advance!!!

  • Sign in to reply
  • Cancel
Parents
  • Former Member
    0 Former Member over 11 years ago

    Thank you guys for your help - it works! As I said previously I am a beginner at this (but expected to become an expert within a few weeks as I have to teach this stuff to certificate level classes) and you were of great help! This has made it so much easier

     

    .... I did not know about the #define command too so that is of help for the future too. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 11 years ago in reply to Former Member

    Hi Paul,

     

    Great! Glad it's sorted. Good luck with the rest of your training : )

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • shabaz
    0 shabaz over 11 years ago in reply to Former Member

    Hi Paul,

     

    Great! Glad it's sorted. Good luck with the rest of your training : )

    • 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