Gil H Arch
Create Your First Project
Start adding your projects to your portfolio. Click on "Manage Projects" to get started
Second exercise
A servo motor is operated via a joystick and lights controlled through a potentiometer
CODE 1:
#include <Servo.h>
Servo gilservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
gilservo.attach(3); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
gilservo.write(val); // sets the servo position according to the scaled value
delay(0); // waits for the servo to get there
}
CODE 2:
const int Red_led_pin = 2;
const int Yellow_led_pin = 3;
const int Green_led_pin = 4;
const int Poten_pin = A0;
int Poten_value;
const int Button_pin = 6;
bool button_state = false;
void setup() {
pinMode(Red_led_pin,OUTPUT);
pinMode(Yellow_led_pin,OUTPUT);
pinMode(Green_led_pin,OUTPUT);
pinMode(Button_pin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
// put your main code here, to run repeatedly:
if(digitalRead(Button_pin)==0){
button_state= !button_state;
}
Poten_value = analogRead(Poten_pin);
Serial.println(Poten_value);
if (button_state == true){
if (Poten_value<= 341){
digitalWrite(Red_led_pin, HIGH);
digitalWrite(Yellow_led_pin, LOW);
digitalWrite(Green_led_pin, LOW);
}
else if(Poten_value<= 682){
digitalWrite(Red_led_pin, LOW);
digitalWrite(Yellow_led_pin, HIGH);
digitalWrite(Green_led_pin, LOW);
}
else{
digitalWrite(Red_led_pin, LOW);
digitalWrite(Yellow_led_pin, LOW);
digitalWrite(Green_led_pin, HIGH);
}
}
else {
digitalWrite(Red_led_pin, LOW);
digitalWrite(Yellow_led_pin, LOW);
digitalWrite(Green_led_pin, LOW);
}
}








