• Same story, different day...........year ie more of the same fiat floods the world
  • There are no markets
  • "Spreading the ideas of freedom loving people on matters regarding high finance, politics, constructionist Constitution, and mental masturbation of all types"

Fun With the Arduino Microcontroller.

Joined
Nov 15, 2016
Messages
366
Likes
166
#1
Just got my Arduino in the mail the other day. I ordered the $35 dollar starter kit on ebay that has most of the parts needed to do several projects. Main application is gonna be home automation (automated security cameras) and micro drone control.

Since Hobby King didn't send me the receiver to my 2.4 ghz transmitter I was thinking about controlling projects with IR. So going down that route I also picked up an old HP IPAQ with IR capabilities for cheap on EBAY. Goal is to use the IRda port of the IPAQ to send commands to the project with home entertainment remote control software from here. http://www.wincesoft.de/html/remotecontrol_ii.html

Since I had trouble establishing the permissions to use the usb port on ubuntu linux I thought this might help any one trying to install the Arduino IDE. Here's how to grant read/write permission in UNIX.

Unix commands for root access to port

dmesg
ls -l /dev/ttyUSB0 //"port name maybe different"
sudo usermod -a -G dialout “username”
sudo chmod a+rw /dev/ttyUSB0 //"port name maybe different"
./arduino

Download the Arduino Software (IDE)
Get the latest version from the download page. You can choose between the 32, 64 and ARM versions

Extract the package
The file is compressed and you have to extract it in a suitable folder, remembering that it will be executed from there.

Run the install script
Open the arduino-1.6.x folder just created by the extraction process and spot the install.sh file. Right click on it and choose Run in Terminal from the contextual menu. The installation process will quickly end and you should find a new icon on your desktop.

If you don’t find the option to run the script from the contextual menu, you have to open a Terminal window and move into the arduino-1.6.x folder. Type the command ./install.sh and wait for the process to finish. You should find a new icon on your desktop.
The libraries I needed were the servo and IR libraries. So downloaded them through the Arduino IDE.
Open the IDE and click to the "Sketch" menu and then Include Library > Manage Libraries.
I did a few basic projects first. The Traffic light one is pretty simple.

Arduino Traffic Light Circuit
The circuit we need to setup is really simple and shouldn’t take you too long to do. Below is a diagram of what you will need to do. If you’re looking a great program to prototype and draw up diagrams, then be sure to check out Fritzing.

upload_2017-3-19_13-21-35.jpeg


Arduino Traffic Light Code
Now we will write some code to bring out lights to life. If you have programming experience, then you will find this code really basic. If you’re new to programming this is a great way to start learning.

First we will need to declare our variables. The GREEN variable represents the pin that the green LED is connected to.

Whilst the DELAY_GREEN variable is the amount of time in milliseconds we will delay the program from moving forward.

// variables
int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 5000;
int DELAY_YELLOW = 2000;
int DELAY_RED = 5000;
Now we need to setup the pins so they act as an output and not as an input. The pinMode function accepts two parameters the pin number and the mode (Output, input). Pinmode(pin,mode).

// basic functions
void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}
This function creates a loop that program will run through so every time we call a function it will turn a light on and then we can set a delay so it doesn’t change until that time is up.

void loop()
{
green_light();
delay(DELAY_GREEN);
yellow_light();
delay(DELAY_YELLOW);
red_light();
delay(DELAY_RED);
}
Now for each light we will need to create a function. As you can see the green_light() function will turn the green light on whilst turning the yellow and red LEDS off.

void green_light()
{
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}

void yellow_light()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
}

void red_light()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
Once you are done writing your code simply upload it to your Arduino using the USB cable that should have come with your kit. The lights should start to blink in the pattern that we have defined using the function calls and the delays. You should always test your code before uploading it to the Arduino, you can do this by pressing verify button (Tick). This will let you know if there are any errors in your code.

I hope you have enjoyed this Arduino traffic light project if you have any questions, comments then please drop a comment below.
Starting to get into servo and motor control so far this is the sketch I have. The IR receiver is not reading any data in the serial monitor. Might not be hooked right in the circuit. So I have throttle control with a potentiometer to the 4 motors and the 1 servo I have (waiting on my 10 pack from Hong Kong). Goal is a micro IR quad copter with this project.


#include <Servo.h>
#include <IRremote.h>

Servo esc;
Servo myservo; // create servo object to control a servo

int throttlePin = 1;
int potpin = 1;
int potpin2 = 2;
int potpin3 = 3;// analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int analogInPin = A3;
int sensorValue = 0;
int outputValue = 0;

//define motors
int MOTOR1 = 2; //define motors = pin
int MOTOR2 = 3;
int MOTOR3 = 4;
int MOTOR4 = 5;

int RECV_PIN = 6; //infared receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
// put your setup code here, to run once:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
//motors
pinMode(MOTOR1, OUTPUT);
pinMode(MOTOR2, OUTPUT);
pinMode(MOTOR3, OUTPUT);
pinMode(MOTOR4, OUTPUT);

Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
// put your main code here, to run repeatedly:

digitalWrite(MOTOR1, LOW);
digitalWrite(MOTOR2, LOW);
digitalWrite(MOTOR3, LOW);
digitalWrite(MOTOR4, LOW);

int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 0, 179);
esc.write(throttle);

//throttle control
sensorValue = analogRead(analogInPin)/4;

outputValue = map(sensorValue, 0, 1023, 0, 255);

analogWrite(MOTOR1, sensorValue);
analogWrite(MOTOR2, sensorValue);
analogWrite(MOTOR3, sensorValue);
analogWrite(MOTOR4, sensorValue);


// servo 1 pot
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)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);


//ir receiver
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(100);


}


 
Last edited:
Joined
Nov 15, 2016
Messages
366
Likes
166
#2
Found this tutorial that's pretty straight forward on getting the IR receiver to get the hexadecimal code from the remote control.
http://www.instructables.com/id/Controlling-servo-motor-using-IR-remote-control/?ALLSTEPS

Home automation here I come.

Still working on this sketch. Just getting the grasp of the "IF" statements. When I add the motor control through IR. My servo control stops working and I can't read any of the IR data from receiver. Just motor works on the same bread board set up. Guess more debugging tomorrow.


// This code is for controlling servo motor with IR remote control
// When clicking at any of two buttons the motor is toggling between the rotation and stop

#include <IRremote.h> //must copy IRremote library to arduino libraries
#include <Servo.h>
#define plus 0x21035431 //clockwise rotation button
#define minus 0x983AB4C1 //counter clockwise rotation button
#define throttleplus 0xC20370A1 //throttle up
#define throtleminus 0x81930A09 //throttle down

int RECV_PIN = 10; //IR receiver pin
Servo servo;
int val; //rotation angle
bool cwRotation, ccwRotation; //the states of rotation

IRrecv irrecv(RECV_PIN);

decode_results results;

//define motors
int MOTOR1 = 2; //define motors = pin
int MOTOR2 = 3;
int MOTOR3 = 4;
int MOTOR4 = 5;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
servo.attach(6); //servo pin
//motors
pinMode(MOTOR1, OUTPUT);
pinMode(MOTOR2, OUTPUT);
pinMode(MOTOR3, OUTPUT);
pinMode(MOTOR4, OUTPUT);

}

void loop()
{
digitalWrite(MOTOR1, LOW);
digitalWrite(MOTOR2, LOW);
digitalWrite(MOTOR3, LOW);
digitalWrite(MOTOR4, LOW);


if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value

if (results.value == plus)
{
cwRotation = !cwRotation; //toggle the rotation value
ccwRotation = false; //no rotation in this direction
}

if (results.value == minus)
{
ccwRotation = !ccwRotation; //toggle the rotation value
cwRotation = false; //no rotation in this direction
}
}
if (cwRotation && (val != 175)) {
val++; //for colockwise button
}
if (ccwRotation && (val != 0)) {
val--; //for counter colockwise button
}
servo.write(val);
delay(20); //General speed


//throttle controll
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xC20370A1: // play button on my remote
analogWrite(2, 200); //pwm for lower speed
digitalWrite(2, HIGH);
delay(10000);
digitalWrite(2, HIGH);
break;
case 0x81930A09: // stop button on my remote
digitalWrite(2, HIGH);
analogWrite(2, 50); //pwm for lower speed
delay(3000);
digitalWrite(2, HIGH);
break;
}
irrecv.resume();
}



}