Go Down

Topic: Executing different part of sketch on button press (Read 456 times) previous topic - next topic

dEE4dog

Totally new to arduino, and struggling  with the following, please help.
I want to control 2 motors with motor shield, this part is easy. One motor on certain speed other on another speed and servo on certain sweep.
On a toggle button press i want to execute a different set of speeds and sweep for the servo, on a third or forth set on each button press. Loop the sets on button press.
A switch statement?

Please help

UKHeliBob

Quote
A switch statement?

Sounds good.  Increment a counter each time the button is pressed and use the variable with switch/case to carry out the actions that you want.  You will probably need to debounce the switch to ensure a smooth transition between cases.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Quote
Increment a counter each time the button is pressed

Each time the switch BECOMES pressed will work a lot better than IS pressed.

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Robin2

I have never come across "a switch becomes pressed" but I hear people talk and write about "button is pressed" almost every day. What point of english grammar is at stake here?

Also I think in general usage a switch is something that changes state and stays in the new state whereas a button press is a momentary thing - which is what the OP seemed to have in mind so that I think UKhelibob's english was correct.

(anything for a bit of entertainment on a wet and windy morning :) )
...R


Quote
Increment a counter each time the button is pressed

Each time the switch BECOMES pressed will work a lot better than IS pressed.

UKHeliBob

'BECOMES pressed' - the event happens just once for each button press
'IS pressed' - could be read as 'whilst it remains pressed'

The first indicates a change of state.  The latter is ambiguous as to its meaning.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Robin2

Then I think plain english would be "button is pressed briefly" or "button is pressed once". However I think the normal usage would be to infer this meaning from "button is pressed" as few people have the patience to keep a button pressed continuously - that's why they install switches :)

AND the software needs to be designed to be indifferent to how long the button is held down!

...R


'BECOMES pressed' - the event happens just once for each button press
'IS pressed' - could be read as 'whilst it remains pressed'

The first indicates a change of state.  The latter is ambiguous as to its meaning.

PaulS

Quote
AND the software needs to be designed to be indifferent to how long the button is held down!

Not always. Sometimes a tap is to be interpreted different from a press and hold.

In any case, just checking that the switch is pressed is not sufficient, in many cases. Determining that the switch has transitioned to pressed (was not pressed last time; is pressed now) is necessary in some cases. In other cases, determining that the switch has transitioned to released (was pressed last time; is not now pressed) is what is needed.

Knowing how to determine that a transition occurred is how you determine how long a switch has been held, if that is important.

dEE4dog

Thanks for the hint in right direction, and grammar lesson  :)
After further reading this is my code, and it works. Added Lcd just for fun.
Comments welcome, might be sloppy (cut and past some code)

Code: [Select]
include <LiquidCrystal.h>

LiquidCrystal lcd(10, 7, 5, 4, 2, 1);

// set up a constant for the switchPin
const int switchPin = 6;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

int buttonPushCounter = -1;   // counter for the number of button presses

void setup() {
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);
 
  // set up the switch pin as an input
  pinMode(switchPin,INPUT);
 
  // Print a message to the LCD.
  lcd.print("Laser Ready");
  // set the cursor to column 0, line 1
  // line 1 is the second row, since counting begins with 0
lcd.setCursor(0, 1);
  // print to the second line
  lcd.print("Press button");
 
   //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin
}

void loop() {
  // check the status of the switch
  switchState = digitalRead(switchPin);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {
   
    if (switchState == LOW) {

      buttonPushCounter ++;
buttonPushCounter %= 6;
      // clean up the screen before printing a new reply
      lcd.clear();
      // set the cursor to column 0, line 0     
      lcd.setCursor(0, 0);
      // print some text
      lcd.print("Motor setup");
      // move the cursor to the second line
      lcd.setCursor(0, 1);

      // choose a saying to print baed on the value in reply
     
       switch (buttonPushCounter)
{
      case 0:
        lcd.print("1");
          //delay(3000);
          digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B
   //delay(1000);


  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A

  analogWrite(3, 255);   //Spins the motor on Channel A at full speed

  //Motor B backward @ half speed
  digitalWrite(13, LOW);  //Establishes backward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 123);    //Spins the motor on Channel B at half speed

        break;

      case 1:
        lcd.print("2");
        //delay(3000);
         digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B
    // delay(1000);
        //Motor A forward @ full speed
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);    //Spins the motor on Channel A at half speed
 
  //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed
        break;

      case 2:
        lcd.print("3");
        break;

      case 3:
        lcd.print("4");
        break;

      case 4:
        lcd.print("5");
        break;

      case 5:
        lcd.print("Stop Motors");
        digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B
        break;

     
      }
    }
  }
  // save the current switch state as the last state
  prevSwitchState = switchState;
}

PaulS

On the Tools menu, there is a function called Auto Format. I think you'll be pleased with what it does to your code. I know I would be if you used it, and replaced that code.

UKHeliBob

Code: [Select]
       digitalWrite(9, HIGH);  //Engage the Brake for Channel A
        digitalWrite(9, HIGH);  //Engage the Brake for Channel B

This mistake (3 times) probably came from your cut and paste.
Can I suggest that you give the motor control pins names.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


Go Up