Programming

Hi! We're two girls who need HELP!!
We are trying to program an automatic chees slider.
If a button is pressed, a led should turn on and a motor is starting.
We also going to use an IR-sensor wyc h2010.
This is what we've done so far. Please help us!

int motorPin = 13;
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;

void setup() {
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, 0);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState==HIGH){

digitalWrite(ledPin, HIGH);

digitalWrite(motorPin, HIGH);

delay(2000);

digitalWrite(ledPin, LOW);

digitalWrite(ledPin, LOW);
delay(1000);

}

What does that code ACTUALLY do? How does that differ from what you want? How is the switch wired?

Nothing happend when I push the button :frowning:

Do you really plan to use the same pin for motor and led?

Three questions, one answer is the wrong ratio.

try this :

int motorpin = 12;//dont use 13
int buttonpin = 2;
int ledpin = 13;

void setup() {
 pinMode(motorpin, OUTPUT);
 pinMode(buttonpin, INPUT);
 pinMode(ledpin, OUTPUT);
 Serial.begin(9600);
}

void loop() {
 if(digitalRead(buttonpin) == HIGH) {
   digitalWrite(ledpin, HIGH);
   digitalWrite(motorpin, HIGH);
   delay(2000);
   digitalWrite(ledpin, LOW);
   digitalWrite(motorpin, LOW);
  } else {
   digitalWrite(ledpin, LOW);
   digitalWrite(motorpin, LOW);
 }
}

WOOW IT WORKS! Thank you so much!!! You're the best!

Thank you :slight_smile: