Hello, need urgent help. We are doing this project
But the coding does not work.
we are not using the ir sensor.
Hello, need urgent help. We are doing this project
But the coding does not work.
we are not using the ir sensor.
(deleted)
// Include Libraries
#include "Arduino.h"
#include "Button.h"
#include <Servo.h>
#define redpin 4
#define bluepin 2
#define greenpin 3
#define redpin_M 7
#define bluepin_M 5
#define greenpin_M 6
int plasticsensor=A4;
int metalsensor=A0;
int metalsensor_M=A2;
Servo myservo; // create servo object to control a servo
Servo myservo_M; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 165; // variable to store the servo position
int pos_M = 158; // variable to store the servo position
void setup() {
myservo.attach(11); // attaches the servo on pin 9 to the servo object
myservo_M.attach(12); // attaches the servo on pin 9 to the servo object
pinMode(redpin,OUTPUT);
pinMode(bluepin,OUTPUT);
pinMode(greenpin,OUTPUT);
//metal sensor
pinMode(redpin_M,OUTPUT);
pinMode(bluepin_M,OUTPUT);
pinMode(greenpin_M,OUTPUT);
pinMode(plasticsensor,INPUT_PULLUP);
pinMode(metalsensor,INPUT_PULLUP);
//pinMode(anothersensor,INPUT_PULLUP);
pinMode(metalsensor_M,INPUT_PULLUP);
Serial.begin(9600);
analogWrite(redpin,255);
analogWrite(bluepin,0255);
analogWrite(greenpin,255);
analogWrite(redpin_M,255);
analogWrite(bluepin_M,0255);
analogWrite(greenpin_M,255);
}
void loop() {
int sensor_read=digitalRead(plasticsensor);
int sensor_read_m=digitalRead(metalsensor);
int sensor_read_mm=digitalRead(metalsensor_M);
Serial.println("plastic sensor");
Serial.println(sensor_read);
Serial.println("metal sensor");
Serial.println(sensor_read_m);
Serial.println(sensor_read_mm);
//Plastic bin
if((sensor_read==0)&&(sensor_read_m!=1)){
for (pos = 160; pos >= 90; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
analogWrite(redpin,255);
analogWrite(bluepin,0);
analogWrite(greenpin,255);
delay(2500);
for (pos = 90; pos <= 160; pos += 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
analogWrite(redpin,0);
analogWrite(bluepin,0);
analogWrite(greenpin,0);
}
else{
// goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
analogWrite(redpin,255);
analogWrite(bluepin,255);
analogWrite(greenpin,255);
}
//metal sensor
if(sensor_read_mm==1){
for (pos_M = 160; pos_M >= 90; pos_M -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo_M.write(pos_M); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
analogWrite(redpin_M,255);
analogWrite(bluepin_M,0);
analogWrite(greenpin_M,255);
delay(2500);
for (pos_M = 90; pos_M <= 160; pos_M += 1) { // goes from 180 degrees to 0 degrees
myservo_M.write(pos_M); // tell servo to go to position in variable 'pos'
delay(1); // waits 15ms for the servo to reach the position
}
analogWrite(redpin_M,0);
analogWrite(bluepin_M,0);
analogWrite(greenpin_M,0);
}
else{
// goes from 180 degrees to 0 degrees
myservo_M.write(pos_M); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
analogWrite(redpin_M,255);
analogWrite(bluepin_M,255);
analogWrite(greenpin_M,255);
}
}
myservo.attach(11); // attaches the servo on pin 9 to the servo object
myservo_M.attach(12); // attaches the servo on pin 9 to the servo object
Did you read the comment to do the wiring?
Why does
your code
wander
around
the
page?
What can you see on the serial monitor?
Also 'coding does not work' is a really bad description of your 'fault'.
Posting your code was only part of what is required from you if we are to help. What does the code fail to do that you want it to do? What does the code do that you don't want it to do?