i've got that error message ( expected unqualified-id before 'if' ) so what is the problem ?
this is the code :
// PCA Ship define
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50
// define Dc motor driver with joystick
#define enA 10
#define in1 2
#define in2 3
#define enB 11
#define in3 4
#define in4 5
int motorSpeedA = 0;
int motorSpeedB = 0;
// define powerscrew
#define enC 12
#define in5 6
#define in6 7
#define enD 13
#define in7 8
#define in8 9
#define switchA 53
int motorSpeedC = 0;
// define servos
uint8_t s1 = 0;
uint8_t s2 = 2;
uint8_t s3 = 4;
uint8_t s4 = 6;
//uint8_t s5 = 8;
//uint8_t s6 = 10;
// define buttons
int button1 = 41;
int button2 = 43;
int button3 = 45;
int button4 = 47;
int button5 = 49;
void setup()
{
// Servos
Serial.begin(9600);
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
pwm.setPWM(0, 0, 0);
pwm.setPWM(2, 0, 0);
// Dc motors
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
}
int pulseWidth(int angle)
{
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
Serial.println(analog_value);
return analog_value;
}
void loop() {
// Servos loop
if (digitalRead(button1) == HIGH) // position 1 for chair
{
pwm.setPWM(0, 0, pulseWidth(90));
pwm.setPWM(2, 0, pulseWidth(90));
pwm.setPWM(4, 0, pulseWidth(90));
pwm.setPWM(6, 0, pulseWidth(90));
//pwm.setPWM(8, 0, pulseWidth(90));
//pwm.setPWM(10, 0, pulseWidth(90));
digitalWrite(in5, LOW);
digitalWrite(in6, HIGH);
// Set Motor C forward
digitalWrite(in7, LOW);
digitalWrite(in8, HIGH);
motorSpeedC = 255;
delay(1000);
}
else if (digitalRead(button2) == HIGH) // position 2 for chair
{
pwm.setPWM(0, 0, pulseWidth(10));
pwm.setPWM(2, 0, pulseWidth(10));
pwm.setPWM(4, 0, pulseWidth(10));
pwm.setPWM(6, 0, pulseWidth(10));
//pwm.setPWM(8, 0, pulseWidth(0));
//pwm.setPWM(10, 0, pulseWidth(0));
digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
// Set Motor c backward
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
motorSpeedC = 255;
delay(1000);
}
else if (digitalRead(button3) == HIGH) // position 3 for chair
{
pwm.setPWM(0, 0, pulseWidth(170));
pwm.setPWM(2, 0, pulseWidth(170));
pwm.setPWM(4, 0, pulseWidth(170));
pwm.setPWM(6, 0, pulseWidth(170));
//pwm.setPWM(8, 0, pulseWidth(180));
//pwm.setPWM(10, 0, pulseWidth(180));
digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
// Set Motor c backward
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
motorSpeedC = 255;
delay(1000);
}
if (digitalRead(switchA) == LOW )
{
motorSpeedA = 0;
}
analogWrite(enC, motorSpeedC);
analogWrite(enD, motorSpeedC);
}
/*if (digitalRead(button2) == HIGH)
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
delay(1000);*/
// Dc motor loop
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}