Basic Code help for a newbie

Hi guys,

So i am trying to setup a basic rig to life test a Nail gun. I have a motor to run a cam which acts on the trigger and fires every revolution (36RPM). Now i want to set up some micro switches in various locations on the rig so that if there is a malfunction the trigger motor will stop. I have tried to write a some code shown below that if any one of the switches is broken for more than 0.5sec the trigger motor will stop.

I haven't got to the point trying to run this and debug as being very new and wanted to see if i am right track first. Any comment appreciated

int MotorPin = 13;                 // Motor is connected to digital pin 13
int RecockState = 14;
int AnvilState = 15;
int StrikerState = 16;
int Counter = 0;

void setup() 
{
pinMode(MotorPin, OUTPUT);      // sets the digital pin as output
pinMode(RecockState, INPUT);
pinMode(AnvilState, INPUT);
pinMode(StrikerState, INPUT);
}

void loop() 
{
  
   if (RecockState == LOW)
{
   delay(500);
      if (RecockState == LOW)
{
      MotorPin = LOW;
      Counter ++;
}}
   if (AnvilState == LOW)
{
    delay(500);
      if (AnvilState == LOW)  
      {MotorPin == LOW;
      Counter ++;
}}
   if (StrikerState == LOW)
{
      delay(500);
      if (StrikerState == LOW)
        {MotorPin == LOW;
        Counter ++;
}}
   else 
    {
MotorPin == HIGH;
} 
   if (Counter >= 10);
   {
MotorPin == LOW;
   while(1) { }
}
    
  
  
}

NEVER USE DELAY(). This is a blocking function, which is not good. Look up the demo "blink without delay()".

Use code tags to post your code.

You will need to provide more information:

  • Are your switches high or low side switches? (i.e. is the output HIGH when pressed or LOW when pressed?)
  • Where are the switches and what are their purposes? (maybe include a drawing or picture?)

All in all, my suggestion is to rethink your code. I am having a hard time following your logic. You might also want to use conventional spacing and indentation methods (not necessary, but makes code more readable).

How are your swithes wired? I see no INPUT_PULLUP in the switch pinModes so you must have pull down resistors? Which Arduino are you using? Why didn't you read the "how to use the forum" stickies? Your formatting could use improvement to make the code easier to follow. Delay() stops your program for the delay duration mking the program very unresponsive. Learn non blocking timing from the blink without delay example in the IDE examples under digital.

I was typing when Power_Broker posted much the same as I am thinking. Great minds, I guess.

Welcome to the Forum Willyd!

You will need to use digitalRead() to read the state of your switches. You will likely also have to debounce them. Here is a great tutorial on how to use switches Gammon SWITCHES.

As mentioned delay() is a bad thing in this situation.
check these:
Demonstration code for several things at the same time
Multi-tasking the Arduino

Before posting your sketch format it by pressing 'CTRL' T

Place your braces { and } on lines by themselves.

.

Hi WillyD,

Start by reading the GPIO pins correctly, like this:

if (RecockState == LOW)

Should be:

if (digitalRead(RecockState) == LOW)

Hey. Your code is a bit tricky to read.

When you have nested if statements (if statements inside if statements), you should add indentation. If you have all your code parallel to the edge of the screen, you can't easily see which statements are nested inside others just by glancing.

Sorry this isn't to do with your question but it'll sure help other people help you.

e.g

if(b_Condition1)
{
     
  //some code

      if(b_Condition2)  //  <--  Notice how we put spaces so we can clearly see that this if() is nested 
        {
          //some code
        }

   //some more code

}

Thanks for the Input Guys.

I am working on the code. as well as the formatting issues it sounds like there is a few other errors also.

I want to go right back to the basics and just get my motor spinning with H bridge module. I am currently stuck probably on the wiring i think. Please let me know if you see any issues what i have got currently(schematic attached). I cant seem to get even this simple program to run.

void setup() 
{
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(2, OUTPUT);
}

void loop() 
{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
analogWrite(2, 80);
delay(1000);
}

willyd:
I cant seem to get even this simple program to run.

void loop() 

{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
analogWrite(2, 80);
delay(1000);
}

This code will:

1)Send power to pin 8
2) Send no power to pin 9 (do nothing)
3) Send a lower power to pin 2 (IF pin 2 is PWM compatible on your board - only certain pins can use analogwrite)
4) Pause the program for a second and then repeat the code (even though nothing in the code changes?).

I don't know what the green board is in your schematic so it's hard to say for sure, but this program seems a little pointless - particularly the delay() part. Generally when you use delay(), it's because you want the program to remain in a certain state for a certain amount of time before it changes. In this case you've written code that says "I want pin 8 to be on, 9 to be off, and 2 to be on a little bit - then I want these pins to stay like that for 1 second." But you see, it just loops and never does anything different after the 1 second period - so why use the delay at all?