วันพฤหัสบดีที่ 13 กรกฎาคม พ.ศ. 2560

Modify toy wire control to be wireless control by Arduino + PS2 wireless control EP 1/2



Equipment


Arduino UNO
 PS2 reciever





PS2 transmitter


Wiring

 


Pin 1 - 9  from left to right 

PS2 connect to Arduino
1 >>> 12
2 >>> 11
4 >>> GND
5 >>> 3.3 Volt  ****
6 >>> 10
7 >>> 13



Library


Code

#include <PS2X_lib.h>



PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning

//you must always either restart your Arduino after you conect the controller,

//or call config_gamepad(pins) again after connecting the controller.

int error = 0;

byte type = 0;

byte vibrate = 0;

void setup(){

 Serial.begin(57600);

 //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

 error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error

 if(error == 0){

   Serial.println("Found Controller, configured successful");

   Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");

  Serial.println("holding L1 or R1 will print out the analog stick values.");

  Serial.println("Go to www.billporter.info for updates and to report bugs.");

 }

  else if(error == 1)

   Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");

  else if(error == 2)

   Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  else if(error == 3)

   Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

   //Serial.print(ps2x.Analog(1), HEX);

   type = ps2x.readType();

     switch(type) {

       case 0:

        Serial.println("Unknown Controller type");

       break;

       case 1:

        Serial.println("DualShock Controller Found");

       break;

       case 2:

         Serial.println("GuitarHero Controller Found");

       break;

     }

}

void loop(){

   /* You must Read Gamepad to get new values

   Read GamePad and set vibration values

   ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)

   if you don't enable the rumble, use ps2x.read_gamepad(); with no values

   you should call this at least once a second

   */

 if(error == 1) //skip loop if no controller found

  return;

 if(type == 2){ //Guitar Hero Controller

   ps2x.read_gamepad();          //read controller

   if(ps2x.ButtonPressed(GREEN_FRET))

     Serial.println("Green Fret Pressed");

   if(ps2x.ButtonPressed(RED_FRET))

     Serial.println("Red Fret Pressed");

   if(ps2x.ButtonPressed(YELLOW_FRET))

     Serial.println("Yellow Fret Pressed");

   if(ps2x.ButtonPressed(BLUE_FRET))

     Serial.println("Blue Fret Pressed");

   if(ps2x.ButtonPressed(ORANGE_FRET))

     Serial.println("Orange Fret Pressed");

    if(ps2x.ButtonPressed(STAR_POWER))

     Serial.println("Star Power Command");

    if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed

     Serial.println("Up Strum");

    if(ps2x.Button(DOWN_STRUM))

     Serial.println("DOWN Strum");

    if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed

         Serial.println("Start is being held");

    if(ps2x.Button(PSB_SELECT))

         Serial.println("Select is being held");

    if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE

    {

        Serial.print("Wammy Bar Position:");

        Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);

    }

 }

 else { //DualShock Controller

    ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed

    if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed

         Serial.println("Start is being held");

    if(ps2x.Button(PSB_SELECT))

         Serial.println("Select is being held");

   

     if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed

       Serial.print("Up held this hard: ");

       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);

      }

      if(ps2x.Button(PSB_PAD_RIGHT)){

       Serial.print("Right held this hard: ");

        Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);

      }

      if(ps2x.Button(PSB_PAD_LEFT)){

       Serial.print("LEFT held this hard: ");

        Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);

      }

      if(ps2x.Button(PSB_PAD_DOWN)){

       Serial.print("DOWN held this hard: ");

     Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);

      } 

      vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on

                                              //how hard you press the blue (X) button   

  

    if (ps2x.NewButtonState())               //will be TRUE if any button changes state (on to off, or off to on)

    {

       

        if(ps2x.Button(PSB_L3))

         Serial.println("L3 pressed");

        if(ps2x.Button(PSB_R3))

         Serial.println("R3 pressed");

        if(ps2x.Button(PSB_L2))

         Serial.println("L2 pressed");

        if(ps2x.Button(PSB_R2))

         Serial.println("R2 pressed");

        if(ps2x.Button(PSB_GREEN))

         Serial.println("Triangle pressed");

        

    } 



    if(ps2x.ButtonPressed(PSB_RED))             //will be TRUE if button was JUST pressed

         Serial.println("Circle just pressed");

    if(ps2x.ButtonReleased(PSB_PINK))             //will be TRUE if button was JUST released

         Serial.println("Square just released");   

    if(ps2x.NewButtonState(PSB_BLUE))            //will be TRUE if button was JUST pressed OR released

         Serial.println("X just changed");   

    if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE

    {

        Serial.print("Stick Values:");

        Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX 

        Serial.print(",");

        Serial.print(ps2x.Analog(PSS_LX), DEC);

        Serial.print(",");

        Serial.print(ps2x.Analog(PSS_RY), DEC);

        Serial.print(",");

        Serial.println(ps2x.Analog(PSS_RX), DEC);

    }

 }

 delay(50);

}







วันศุกร์ที่ 12 พฤษภาคม พ.ศ. 2560

GSM Sim900 Module Send/Recieve Message




Hardware



SIM900A GSM/GPRS Module Specification



Power supply by 5V, can be powered by USB, for long term use it is better to still use 1A current power source. XH2.54-2P connector.TTL 5V and 3V3 interface (yellow pins marked on board), compatible with arduino, STM32, raspberry pi, etc.On board RS232 interface, easy for debugging. On the left, white pins.On board ring signal LED, on the left-top corner of SIM900A IC.On Board reset and restart pins, on the bottom of the SIM900A IC.Reserved IPX mini connector, but not yet soldered.Standard SMA antenna connector.Serial port is protected by TVS and beads circuit, to prevent any surging current or HV.SIM circuit has SMF05C electrostatics releasing IC.Package includes antenna and DC jack.Do NOT have audio power jack on board, which means you can not hearing or speaking through the call.Board size is 5×5 CM


Wiring

GSM   >>> Arduino pin

5VT(TX) >>> 2
5VR(RX) >>> 3
Gnd >>> Gnd

Power external ภายนอกเท่านั้นเพราะบอร์ดกระแสไม่พอ

Make sure all device common  Gnd .กราวน์ของบอร์ดและแหล่งจ่ายไฟต้องร่วมกัน




SIM900A True move AIS available (THAI)




POWER 5 Volt supply external recommend 




5 volt operate 










5VR connect to Pin3




 5VT connect to Pin 2



Make sure all ground connected






 Library Link



Copy library GSM900 to Arduino library








Video










CODE
#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"

//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;

//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

void setup()
{
     //Serial connection.
     Serial.begin(9600);
     Serial.println("GSM Shield testing.");
     //Start configuration of shield with baudrate.
     //For http uses is raccomanded to use 4800 or slower.
     if (gsm.begin(2400)) {
          Serial.println("\nstatus=READY");
          started=true;
     } else Serial.println("\nstatus=IDLE");

     if(started) {
          //Enable this two lines if you want to send an SMS.
          if (sms.SendSMS("0843545655", "Arduino SMS"))
          Serial.println("\nSMS sent OK");
     }

};

void loop()
{
     if(started) {
          //Read if there are messages on SIM card and print them.
        // deprecated method
        //if(gsm.readSMS(smsbuffer, 160, n, 20)) {
             //  Serial.println(n);
              // Serial.println(smsbuffer);
          //}
          
          //get 1st sms
         sms.GetSMS(1,n,20,smsbuffer,160);
         Serial.println(n);
         Serial.println(smsbuffer);
          
          delay(1000);
     }

};











วันเสาร์ที่ 29 เมษายน พ.ศ. 2560

GPS Data logger for google map

GPS Data logger for google map


Video






Hard ware

SD card to Arduino connect





CS        >>    4
SCK     >>   13
MOSI   >>   11
MISO   >>   12
VCC    >>     5V
GND    >>    GND

GPS




TX     >>  2
RX     >>  3
VCC  >>  3.3 V
GND >>  GND

Google fusion table



Code


#include <SoftwareSerial.h> #include <SPI.h> #include <SD.h> File dataFile; SoftwareSerial GPSModule(2, 3); // RX, TX // process and flow related variables int updates; int failedUpdates; int pos; int stringplace = 0; // // variable to hold out nmea data and labels. String nmea[12]; String labels[12] {"Time: ", "Lat: ", "N/S: ", "Lng: ", "E/W: ", "Fix: ", "Sats: ", "HDP: ", "Alt: ", "Metres: ", "MSL: ", "Metres: "}; // void setup() { Serial.begin(9600); GPSModule.begin(9600); if (!SD.begin(4)) { Serial.println("failed"); // don't do anything more } else{ Serial.println("ok"); } } void loop() { dataFile = SD.open("gps.txt", FILE_WRITE); //flushing out data at the start of each loop, not sure it is really neccessay Serial.flush(); GPSModule.flush(); while (GPSModule.available() > 0) { GPSModule.read(); } // This is the meat of it really, we look for our nmea string and then split it into comma seperated values if (GPSModule.find("$GPGGA,")) { String tempMsg = GPSModule.readStringUntil('\n'); //Serial.println(tempMsg); for (int i = 0; i < tempMsg.length(); i++) { if (tempMsg.substring(i, i + 1) == ",") { nmea[pos] = tempMsg.substring(stringplace, i); stringplace = i + 1; pos++; } if (i == tempMsg.length() - 1) { nmea[pos] = tempMsg.substring(stringplace, i); } } updates++; // Here we convert our minutes and seconds latitute and longitute to decimal degrees nmea[1] = ConvertLat(); nmea[3] = ConvertLng(); // Test to see if GPS is fixed if(nmea[5].toInt() > 0){ // save data to SD if data is fixed if (dataFile) { Serial.println("writing"); // update number dataFile.print(updates); dataFile.print(","); // time dataFile.print(nmea[0]); dataFile.print(","); // latitude dataFile.print(nmea[1]); dataFile.print(","); // longitude dataFile.print(nmea[3]); dataFile.println(""); Serial.println("done"); } else{ Serial.println("error"); } } } else { failedUpdates++; Serial.println(failedUpdates); } // Print out data to the Serial Monitor for (int i = 0; i < 11; i++) { Serial.print(labels[i]); Serial.print(nmea[i]); Serial.println(""); } // Zero our flow control varibles stringplace = 0; pos = 0; // close file on SD card dataFile.close(); } // Here is the funky stuff - More details here https://www.youtube.com/watch?v=bgOZLgaLa0g String ConvertLat() { String posneg = ""; if (nmea[2] == "S") { posneg = "-"; } String latfirst; float latsecond; for (int i = 0; i < nmea[1].length(); i++) { if (nmea[1].substring(i, i + 1) == ".") { latfirst = nmea[1].substring(0, i - 2); latsecond = nmea[1].substring(i - 2).toFloat(); } } latsecond = latsecond / 60; String CalcLat = ""; char charVal[9]; dtostrf(latsecond, 4, 6, charVal); for (int i = 0; i < sizeof(charVal); i++) { CalcLat += charVal[i]; } latfirst += CalcLat.substring(1); latfirst = posneg += latfirst; return latfirst; } String ConvertLng() { String posneg = ""; if (nmea[4] == "W") { posneg = "-"; } String lngfirst; float lngsecond; for (int i = 0; i < nmea[3].length(); i++) { if (nmea[3].substring(i, i + 1) == ".") { lngfirst = nmea[3].substring(0, i - 2); //Serial.println(lngfirst); lngsecond = nmea[3].substring(i - 2).toFloat(); //Serial.println(lngsecond); } } lngsecond = lngsecond / 60; String CalcLng = ""; char charVal[9]; dtostrf(lngsecond, 4, 6, charVal); for (int i = 0; i < sizeof(charVal); i++) { CalcLng += charVal[i]; } lngfirst += CalcLng.substring(1); lngfirst = posneg += lngfirst; return lngfirst; }

วันเสาร์ที่ 1 เมษายน พ.ศ. 2560

HC06+Inventor 2 +Arduino











Code



#include<SoftwareSerial.h>

char receivedChar;

int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=9;//connected to Arduino's port 9


SoftwareSerial mySerial(10, 11); // RX, TX



//RX on Bluetooth to TCX on Arduino through a voltage divider 50ohm from arduino and 100ohm to ground. This is so we can drop the the voltage down to 3.3v (roughly)

// Use blueterm on android to get messages



void setup()



 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
  
  mySerial.begin(9600); 
}



void loop() // run over and over



{

  


  while (!mySerial.available());   // stay here so long as COM port is empty

  receivedChar = mySerial.read();

  if (receivedChar == 'A') {

            //digitalWrite(ENA,LOW);
            // digitalWrite(ENB,LOW);// High is start driving car
              
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,HIGH);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,HIGH);//setting motorB's directon
  }
              
  if (receivedChar == 'B') {

             // digitalWrite(ENA,LOW);
             // digitalWrite(ENB,LOW);// High is start driving car
              
              
                    
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,HIGH); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,HIGH);
             digitalWrite(IN4,LOW);//setting motorB's directon

                         
  }      
  if (receivedChar == 'C') 
  
              {

             // digitalWrite(ENA,LOW);
             // digitalWrite(ENB,LOW);// High is start driving car
  
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,HIGH); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,HIGH);//setting motorB's directon

             

  } // if it is a 3 flash the LED

  if (receivedChar == 'D') {
  
              //digitalWrite(ENA,LOW);
              //digitalWrite(ENB,LOW);// High is start driving car
              
                          
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,HIGH);//setting motorA's directon
             digitalWrite(IN3,HIGH);
             digitalWrite(IN4,LOW);//setting motorB's directon


  } // if it is a 4 print out lots of sensor data


  if (receivedChar == 'E') {
  
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,LOW);//setting motorB's directon
  
  }
  
}

วันศุกร์ที่ 31 มีนาคม พ.ศ. 2560

Arduino Push button Control LED






Circuit




Schematic





Code


/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}