-1

This is my first StackExchange question. I'm relatively new to Arduino and I am trying to tinker with the components and functionality.

I have a RGB LED coupled to my breadboard with a servo attached. The LED only shows Red and Orange for some reason(Why?), SO I mended my code as per the LED behaviour. The code prompts the user for a color, and depending on that, the Servo should rotate along with the LED lighting up. My servo functions well only in 25-165 degrees range. The code goes as,

Here's the code:

# include<Servo.h>
int i;
Servo n1;

String color;
void setup(){

Serial.begin(9600);
  n1.attach(6);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(6,OUTPUT);


}


void loop(){

Serial.println("Enter color: ");

while(Serial.available()==0){
}
color=Serial.readString();

if (color=="red"){
  digitalWrite(2,HIGH);

  for (i=25;i<=165;i=i+5){
        n1.write(i);
        delayMicroseconds(250);
  //}
  n1.write(25);
  delay(2500);
  digitalWrite(2,LOW);
}

}else if (color=="orange"){
  digitalWrite(3,HIGH);

  for (i=165;i>=25;i=i-2){
    n1.write(i);
    delayMicroseconds(250);
  }
  n1.write(25);
  delay(2500);
  digitalWrite(3,LOW);
}
}

So basically when Red is in, I want the Servo to go from 25 to 165 & come back to 25, and vice versa for Orange. But the servo behaves in 2 ways;

1.When Red is entered first, it turns some angle(I guess 25) and stays there, the programme doesn't even loop for further input of color for a long period. Then whatever the input goes, Servo stops rotating, only LED works.

2.When Orange is entered first, it goes again to the same angle as red, and mildly buzzes, but input is accepted and Servo is motionless. I simulated the same setup and code in TinkerCad circuits, and the behaviour is the same, so no particular problem with the hardware there.

I'm relatively new to understanding the Arduino workings and Fundamentals, any help is appreciated. Also, the circuit setup is attached.

Any help will be appreciated.

Thanks!

enter image description here

enter image description here

6
  • Can you please make a circuit diagram using the provided tools
    – Joe S
    Jun 22, 2018 at 13:10
  • for (i = 175; i >= 25175; i = i - 2) {
    – Juraj
    Jun 22, 2018 at 13:18
  • This was a typo in the code that I pasted before, I have re-pasted the corrrected and updated code again.
    – Kv07
    Jun 22, 2018 at 19:43
  • @JoeS I have updated the Circuit Diagram using TinkerCad for your reference.
    – Kv07
    Jun 22, 2018 at 20:03
  • Try using delay rather then delayMicroSeconds. I don't think 0.25 mS is enough time for the servo to move even a few degrees.
    – Chad G
    Jun 22, 2018 at 20:08

1 Answer 1

1

This section of code is never going to run. The for loop says that i starts at 175, and while i>=25175 then execute the n1.write(i) and subtract 2 from i each time it does this.

for (i = 175; i >= 25175; i = i - 2) {
      n1.write(i);
    }

From the arduino servo example page, n1.write() should only ever be from 0 to 180, as it is the angle the servo is at. So you go from 175 to 180 and it wont move past that.

//angle: the value to write to the servo, from 0 to 180 

#include <Servo.h> 

Servo myservo;

void setup() 
{ 
  myservo.attach(9);
  myservo.write(90);  // set servo to mid-point
} 

void loop() {} 
4
  • of course it should be 25 not 25175. it is a copy paste error with no need to explain.
    – Juraj
    Jun 22, 2018 at 16:30
  • Actually, this wasn't the error that is creating the problem. As said by you,it's a copy paste error.
    – Kv07
    Jun 22, 2018 at 16:36
  • arduino.stackexchange.com/q/53814/47552 This is the actual corrected version of the question.
    – Kv07
    Jun 22, 2018 at 16:37
  • I have updated the code in the question, but it still doesn't follow the for loop sequence.
    – Kv07
    Jun 22, 2018 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.