//DC motor code not present in wokwi. assignment to turn off if tilted and back on if upright.

int speedPin=5;
int dirPin1=4;
int dirPin2=3;
int tiltPin=6;
int tiltRead;


int speed=70;//0-255; may not be able to start at all at low voltages. 
//might need physical or high voltage help to start spin.


int rest=100;//milliseconds
int runtime=5000;



void setup() {
Serial.begin(9600);
pinMode(speedPin,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(dirPin2,OUTPUT);
pinMode(tiltPin,INPUT);
digitalWrite(tiltPin,HIGH);//pull up resistor for tilt switch
}

void loop() {
tiltRead=digitalRead(tiltPin);

if(tiltRead==0){//if upright
 digitalWrite(dirPin1,HIGH);// these digitalwrites always need to be opposite each other to set direction.
 digitalWrite(dirPin2,LOW);

 analogWrite(speedPin,255);//max voltage briefly to kickstart spin cycle before downgrading to slow
 delay(rest);//delay to hold kickstart

 while(tiltRead==0){
 analogWrite(speedPin,speed);
 delay(rest);
 tiltRead=digitalRead(tiltPin);
 }
}
else{//emergency tilt shutoff
  digitalWrite(dirPin1,LOW);
  digitalWrite(dirPin2,LOW);
  analogWrite(speedPin,0);
}

delay(rest);
}