I have 2 separate codes below. Instead of opening arduino IDE sending the program for each one whenever I want a different program.
What i want to do is combine them into one program. If a switch goes LOW run the first Loop, if its HIGH run the second loop.
I know how to edit the Setups and such to make it work.
its just that I am not sure if "while"(never used a "while" before) is what i am looking for, or do i use to separate Loops or what exactly???
Any examples would be great!
Thanks!
First Program
#include <Keyboard.h>
void setup() {
pinMode(9, INPUT_PULLUP);
Keyboard.begin();
digitalWrite(9, HIGH);
}
void loop() {
if(digitalRead(9)==LOW){//point button pressed
Keyboard.write('H');
delay(100);
Keyboard.write('S');
delay(150);
Keyboard.write('X');
delay(100);
Keyboard.releaseAll();
delay(8000);
Keyboard.write('Z');
delay(100);
Keyboard.releaseAll();
delay(110000);
Keyboard.write('B');
delay(100);
Keyboard.releaseAll();
}
}
Second Program
#include "Keyboard.h"
int ready1 = 5;
int ready2 = 3;
int point = 9;
boolean b1 = false;
boolean b2 = false;
boolean p = false;
void setup() {
pinMode(ready1, INPUT_PULLUP);
pinMode(ready2, INPUT_PULLUP);
pinMode(point, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
if(!digitalRead(ready1)){
b1 = true;
}
if(!digitalRead(ready2)){
b2 = true;
}
if(!digitalRead(point)){
p = true;
}
if(b1 && b2){ //both ready buttons are pressed
delay(1500);//Wait 1s
Keyboard.write('B');
delay(9000);//wait 9s
Keyboard.write('S');
Keyboard.write('H');
//Reset keys
b1 = false;
b2 = false;
}
if(p){//point button pressed
Keyboard.write('H');
delay(100);
Keyboard.write('S');
delay(200);
Keyboard.releaseAll();
delay(1500);
p = false;
}
}
#include <Keyboard.h>
int ready1 = 5;
int ready2 = 3;
int point = 9;
boolean b1 = false;
boolean b2 = false;
boolean p = false;
void setup() {
pinMode(10, INPUT_PULLUP); //<<<<<<<<<<<<YOUR NEW SWITCH
pinMode(9, INPUT_PULLUP);
Keyboard.begin();
digitalWrite(9, HIGH);
pinMode(ready1, INPUT_PULLUP);
pinMode(ready2, INPUT_PULLUP);
pinMode(point, INPUT_PULLUP);
}
void program1() {
if(digitalRead(9)==LOW){//point button pressed
Keyboard.write('H');
delay(100);
Keyboard.write('S');
delay(150);
Keyboard.write('X');
delay(100);
Keyboard.releaseAll();
delay(8000);
Keyboard.write('Z');
delay(100);
Keyboard.releaseAll();
delay(110000);
Keyboard.write('B');
delay(100);
Keyboard.releaseAll();
}
}
void Program2() {
if(!digitalRead(ready1)){
b1 = true;
}
if(!digitalRead(ready2)){
b2 = true;
}
if(!digitalRead(point)){
p = true;
}
if(b1 && b2){ //both ready buttons are pressed
delay(1500);//Wait 1s
Keyboard.write('B');
delay(9000);//wait 9s
Keyboard.write('S');
Keyboard.write('H');
//Reset keys
b1 = false;
b2 = false;
}
if(p){//point button pressed
Keyboard.write('H');
delay(100);
Keyboard.write('S');
delay(200);
Keyboard.releaseAll();
delay(1500);
p = false;
}
}
void loop{
if (digitalRead(10) == HIGH) program1();
else program2();
}
ok then So "while" is really not the answer its just adding a Loop that refers to the Programs?
THANKS SO MUCH!!
Try the code suggested and come back when you discover that you can't instantly switch between the 2 "programs", but then again you did not ask for that and it may not matter.
In particular you will need to hold the new switch for a long while when program1() is running because of
delay(110000);
There are ways round this but you will need to restructure your program to implement non blocking timing.
You could get ahead by reading Using millis() for timing. A beginners guide
I never did understand Millis.
That did actually solve my issue. One program (say program1) will only be selected an used the whole Day basically.
You guys rock thanks for your help!
I never did understand Millis.
Please take a few minutes to read this.
Or this.
Jacques
That did actually solve my issue.
That's good, but I would encourage you to get to grips with millis() timing because if you continue to program for the Arduino you are going to need it.
I am reading it right now! 
I have seen it used before and i know it will make my programs much cleaner