button and loop

help me to fix this please, when i push the button to call void ruang1 but it wont run repeatly then the sensors cant read by loop, is there any mistakes with my code?

thanks :slight_smile:

const int motor1 = 7;
const int motor2 = 6;
const int motor3 = 5;
const int motor4 = 4;

const int sensor1 = A3;
const int sensor2 = A2;
const int sensor3 = A1;
const int sensor4 = A0;
const int button1 = 10;

const int pinSpeed = 9;

int data1, data2, data3, data4;

int speedMotor = 255;

void setup() {
Serial.begin(250000);
pinMode(button1, INPUT);

pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
pinMode(motor4, OUTPUT);

pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(sensor3, INPUT);
pinMode(sensor4, INPUT);

pinMode(pinSpeed, OUTPUT);
}

void loop() {

while(digitalRead(button1 == HIGH)){
ruang1();}

data1 = digitalRead(sensor1);
data2 = digitalRead(sensor2);
data3 = digitalRead(sensor3);
data4 = digitalRead(sensor4);

analogWrite(pinSpeed, speedMotor);

}
void ruang1(){

if (data1 == LOW && data2 == LOW && data3 == LOW && data4 == LOW)
{
//maju
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
digitalWrite(motor4, HIGH);
}

else if (data1 == HIGH && data2 == LOW && data3 == LOW && data4 == LOW)
{

//kanan
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
digitalWrite(motor3, LOW);
digitalWrite(motor4, HIGH);
}

else if (data1 == LOW && data2 == LOW && data3 == HIGH && data4 == HIGH)
{
//kiri
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}
else if (data1 == HIGH && data2 == HIGH && data3 == LOW && data4 == LOW)
{
//kanan
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}

else if (data1 == LOW && data2 == LOW && data3 == HIGH && data4 == HIGH)
{
//kiri
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
digitalWrite(motor4, HIGH);
}

else if (data1 == HIGH && data2 == LOW && data3 == LOW && data4 == LOW)
{
//kanan
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}

else if (data1 == LOW && data2 == LOW && data3 == LOW && data4 == HIGH)
{
//kiri
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
digitalWrite(motor4, HIGH);
}

else if (data1 == HIGH && data2 == LOW && data3 == HIGH && data4 == LOW)
{
//kanan
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}

else if (data1 == LOW && data2 == HIGH && data3 == LOW && data4 == HIGH)
{
//kiri
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
digitalWrite(motor4, HIGH);
}

else if (data1 == LOW && data2 == HIGH && data3 == LOW && data4 == LOW)
{
//kanan
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}

else if (data1 == LOW && data2 == LOW && data3 == HIGH && data4 == LOW)
{
//kiri
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
digitalWrite(motor4, HIGH);
}

else if (data1 == LOW && data2 == HIGH && data3 == HIGH && data4 == LOW)
{
//maju
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}

else if (data1 == HIGH && data2 == HIGH && data3 == HIGH && data4 == HIGH)
{
//maju
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, HIGH);
digitalWrite(motor4, LOW);
}
}

If you post your code as described in the how to use this forum sticky, more members will read it.

Also, use CTRL-T in the IDE frequently to make the indenting consistent.

okay sir thanks alot

You can edit your post to include code tags. Try not using a while loop. Annotate youโ€™re code. Doe the compiler not give you errors? Use serial.print to debug. It looks like you have stuff outside your while loop that is needed inside it. Best to work out how not to use it at all. Look at tutorials on buttons and how to read the edges and record states.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your buttons wired?
What model Arduino?

Thanks.. Tom.... :slight_smile: