Day 11 Oct 3

For todays lab I had to write code where a user would input an angle and a servo would turn to angle, if the angle was invalid a red LED would turn on. However I could not get the red light to turn off after I entered an invalid angle.

Here's the code

//Sending Multiple Variables at Once
//Define LED pins
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
Servo myServo;
const int RED =12;
const int GREEN =11;
const int BLUE =10;
//int data;
char data;
//Variables for RGB levels
int rval = 0;
int gval = 0;
int bval = 0;
int val = 0;
void setup()
{
 Serial.begin(9600); //Serial Port at 9600 baud
 //Set pins as outputs
 pinMode(RED, OUTPUT);
 pinMode(GREEN, OUTPUT);
 pinMode(BLUE, OUTPUT);
  myServo.attach(SERVO);
}
void loop()
{
 //Keep working as long as data is in the buffer
 while (Serial.available() > 0)
{

data=Serial.read();
//if(data<0 && data> 180)
if(data=='0')
{
 digitalWrite(RED,HIGH);
 Serial.println("Invalid input");
}
// if(data>=0 && data<=100)
else if(data=='1')
{
 val=Serial.parseInt();
 myServo.write(val);
 Serial.print("working");//sets the servo
 delay(15);
}





 //val = map(val, 0, 1023, 0, 179); //scale it to servo range
// val=Serial.parseInt();
// myServo.write(val); //sets the servo
// rval = Serial.parseInt(); //First valid integer
// gval = Serial.parseInt(); //Second valid integer
// bval = Serial.parseInt(); //Third valid integer
//
// delay(15); //waits for the servo
 if (Serial.read() == '\n') //Done transmitting
 {
 //set LED
 analogWrite(RED, rval);
 analogWrite(GREEN, gval);
 analogWrite(BLUE, bval);



 }
 }
}


here's a short vid of the servo working

Comments

Popular posts from this blog

Day 5

Day 22 Nov 14

Day 21 Nov 9