1. Have a great weekend!
    Dismiss Notice
  2. Gold and Silver finish the week down
    Dismiss Notice
  3. Week of 4/29/2017 Closing prices & Chg Over Last Wk---- Gold $1268.30-- DOWN 20.80 Silver $17.19-- down 61 Oil $49.33-- down 0.29 USD $98.89 -- down 0.99

Fun With the Arduino Microcontroller.

Discussion in 'Computers' started by MIavatar, Mar 19, 2017.



  1. MIavatar

    MIavatar Seeker

    Joined:
    Nov 15, 2016
    Messages:
    178
    Likes Received:
    67
    Trophy Points:
    28
    Gender:
    Male
    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

    The libraries I needed were the servo and IR libraries. So downloaded them through the Arduino IDE.
    I did a few basic projects first. The Traffic light one is pretty simple.

    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: Mar 19, 2017
    gringott likes this.
  2. MIavatar

    MIavatar Seeker

    Joined:
    Nov 15, 2016
    Messages:
    178
    Likes Received:
    67
    Trophy Points:
    28
    Gender:
    Male
    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();
    }



    }
     

Share This Page