Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

arduino problem : #include #include #include #include #include Servo servo; / / Servo servo RF 2 4 radio ( 7 , 8 ) ; /

arduino problem :
#include
#include
#include
#include
#include
Servo servo; //Servo servo
RF24 radio(7,8); // CE 7, CSN 8
const byte address[6]="00001"; //
#define LmotorCW 5
#define LmotorCCW 6
#define RmotorCW 9
#define RmotorCCW 10
#define LmotorSpeed 3
#define RmotorSpeed 4
#define waterpump 2
int waterValue =0;
int state =0; //0: idle, 1: forward, 2: stop, 3: pumping, 4: backward
int value =0;
void setup(){
pinMode(LmotorCW, OUTPUT);
pinMode(LmotorCCW, OUTPUT);
pinMode(RmotorCW, OUTPUT);
pinMode(RmotorCCW, OUTPUT);
pinMode(waterpump, OUTPUT);
servo.attach(0);
analogWrite(LmotorSpeed,255); // Motor L (0~255)
analogWrite(RmotorSpeed,130); // Motor R (0~255)
setupRadio();
Serial.begin(9600);
Timer1.initialize(500000); //500ms =500000us
Timer1.attachInterrupt(watercheck); // Attach the service
}
void watercheck(){
receiveData();
Serial.print("Received Water Sensor Value: ");
Serial.println(waterValue);
Serial.print("motor : ");
Serial.println(value);
Serial.print("State : ");
Serial.println(state);
}
void setupRadio(){
radio.begin();
radio.openReadingPipe(1, address); //
radio.setPALevel(RF24_PA_MIN); //
radio.startListening(); //
}
void go(){
value =30;
servo.write(value);
delay(500);
digitalWrite(LmotorCW, LOW);
digitalWrite(LmotorCCW, HIGH);
digitalWrite(RmotorCW, HIGH);
digitalWrite(RmotorCCW, LOW);
delay(2000);
}
void stop(){
digitalWrite(LmotorCW, LOW);
digitalWrite(LmotorCCW, LOW);
digitalWrite(RmotorCW, LOW);
digitalWrite(RmotorCCW, LOW);
}
void backward(){
digitalWrite(LmotorCW, HIGH);
digitalWrite(LmotorCCW, LOW);
digitalWrite(RmotorCW, LOW);
digitalWrite(RmotorCCW, HIGH);
delay(2000);
}
void receiveData(){
if (radio.available()){
char waterValueStr[10]; //
radio.read(&waterValueStr, sizeof(waterValueStr)); //
waterValue = atoi(waterValueStr); //
}
}
void loop(){
switch (state){
case 0: // idle
if (waterValue >=690){
delay(3000);
go();
stop();
delay(3000);
state =1;
}
break;
case 1: // forward
digitalWrite(waterpump, HIGH);
delay(100);
//
state =2;
break;
case 2:
delay(3000);
digitalWrite(waterpump, LOW);
delay(1000);
state =3;
break;
case 3: // stop
backward();
stop();
state=0;
break;
}
}
problem :
If you write servo.write in the setup here, it works well, but if you write servo.write in the loop function, it doesn't work
It works well when operating with the code below, so I don't think it's a final question
What's wrong with the code above?
#include //Adding the Servo library
Create a servo object with a Servo service; //Servo class
int value =0; // variable value to adjust the angle
void setup(){
servo.attach(0); //connect to the servo servo motor pin 7
// Connect to a digital pin that supports PWM with ~ indications
value =0;
servo.write(value); //rotate at an angle of value. ex) rotate 90 degrees if value is 90 degrees
delay(500);
}
void loop(){
for (value =0; value <=180; value +=1){
//// in steps of 1 degree
servo.write(value);
delay(15);
}
for (value = value; value >=0; value -=1){
servo.write(value);
delay(15);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions