Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

C Program Arduino Serial PIN control for Relay

Arduino Code for relay

  Turns given PIN (exmaple 12) on for a third of a second when receiving any input through serial connection
  Recommend not using PIN13 due to signals being sent to it durning startup


  To send signal to Arduino from computer (Linux):
  echo '1' > /dev/ttyUSB0 #as root

  Created By
  Kris Occhipinti
  June 19th, 2015
  http://www.filmsbykris.com

  License GPLv3
  http://www.gnu.org/licenses/gpl-3.0.txt

Solution:

C Program to Demonstrates changing the keypad size and key values.

How to write a C Program to Demonstrates changing the keypad size and key values ?

Solution:

/* C Program to Demonstrates changing the keypad size and key values.*/
#include <Keypad.h>
#include <stdlib.h>
#include <pitches.h>

double notes[89] = {31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978};
const byte ROWS = 8; //four rows
const byte COLS = 8; //four columns
//define the cymbols on the buttons of the keypads
byte hexaKeys[ROWS][COLS] = {
  {1, 1, 9, 9, 17, 17, 25, 25},
  {2, 2, 10, 10, 18, 18, 26, 26},
  {3, 3, 11, 11, 19, 19, 27, 27},
  {4, 4, 12, 12, 20, 20, 28, 28},
  {5, 5, 13, 13, 21, 21, 29, 29},
  {6, 6, 14, 14, 22, 22, 30, 30},
  {7, 7, 15, 15, 23, 23, 31, 31},
  {8, 8, 16, 16, 24, 24, 32, 32}

};
byte rowPins[ROWS] = {2, 3, 4, 5, 6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A8, A9, A10, A11, A12, A13, A14, A15}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad kpd = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

#define trigPin 53
#define echoPin 51

void setup(){
  Serial.begin(9600);
  pinMode(49, HIGH);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop(){
  if (kpd.getKeys()){
        for (int i=0; i<LIST_MAX; i++) {  // Scan the whole key list.
            if ( kpd.key[i].stateChanged ) {  // Only find keys that have changed state.
                char j = kpd.key[i].kchar + 40;
                switch (kpd.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
                    case PRESSED:
                    tone(13, (notes[j] * pitchBend()), 0);
                    Serial.print(pitchBend());
                    Serial.print("\n");
                    break;
             
                    case RELEASED:
                    tone(13, 0, 1);
                }
            }
        }
    }
}

double pitchBend(){
  double duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.print(distance);
  if (distance < 50){
    return distance/200+1;
  }
  else{
    return 1;
  }
}

C Program Turns on an LED on for one second, then off for one second, repeatedly

How to write a C Program Turns on an LED on for one second, then off for one second, repeatedly in C Programming Language ?


Solution For C Program :

[code]
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and  Leonardo, it is attached to digital pin 13. If you're unsure what pin the on-board LED is connected to on your Arduino model, check  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}
[/code]

C Program Duino Starter nRF24L01 Transmit JoyStick

//YD_nRF24L01_Transmit_JoyStick

/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
 - WHAT IT DOES: Reads Analog values on A0, A1 and transmits
   them over a nRF24L01 Radio Link to another transceiver.
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   -
   Analog Joystick or two 10K potentiometers:
   GND to Arduino GND
   VCC to Arduino +5V
   X Pot to Arduino A0
   Y Pot to Arduino A1
 
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A0
#define JOYSTICK_Y A1

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[2];  // 2 element array holding Joystick readings

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  joystick[0] = analogRead(JOYSTICK_X);
  joystick[1] = analogRead(JOYSTICK_Y);
 
  radio.write( joystick, sizeof(joystick) );

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE
//*********( THE END )***********




//YD_nRF24L01_Receive_JoyStick

/* YourDuinoStarter Example: nRF24L01 Receive Joystick values

 - WHAT IT DOES: Receives data from another transceiver with
   2 Analog values from a Joystick or 2 Potentiometers
   Displays received values on Serial Monitor
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
 
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[2];  // 2 element array holding Joystick readings

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    while (!done)
    {
      // Fetch the data payload
      done = radio.read( joystick, sizeof(joystick) );
      Serial.print("X = ");
      Serial.print(joystick[0]);
      Serial.print(" Y = ");    
      Serial.println(joystick[1]);
    }
  }
  else
  {  
      Serial.println("No radio available");
  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE
//*********( THE END )***********

C Program : 4x4 Matrix Keypad connected to Arduino. This code prints the key pressed on the keypad to the serial port.

C Program : 4x4 Matrix Keypad connected to Arduino. This code prints the key pressed on the keypad to the serial port.


Solution:

/*4x4 Matrix Keypad connected to Arduino
This code prints the key pressed on the keypad to the serial port*/

#include <Keypad.h>

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
Serial.begin(9600);
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}

Programa C para controlar una matriz de 8x8 de LEDs para mostrar una serie de 0-9

Cómo escribir un programa en C para controlar una matriz 8x8 de LEDs para mostrar una serie de 0-9 en C Programación Sistemas Embedded ?


Programacion de sistemas embebidos

  1. // Programa Arduino
  2. // Control de una matriz de LEDs 8x8 para mostrar un numero de 0 a 9
  3. // Programacion de sistemas embebidos
  4.  
  5. // inicio de código
  6.  
  7. #include "LedControl.h"
  8. #define Pot A0
  9.  
  10. int DIN = 12;
  11. int CS =  10;
  12. int CLK = 11;
  13.  
  14.  
  15. byte cero[8]= {0x3C,0x7E,0x66,0x66,0x66,0x66,0x7E,0x3C,};
  16.  
  17.  
  18. byte numeros [10][8]= {{0x3C,0x7E,0x66,0x66,0x66,0x66,0x7E,0x3C,}, //0
  19.                       {0x38,0x38,0x18,0x18,0x18,0x18,0x3C,0x3C},  //1
  20.                       {0x1E,0x3F,0x77,0x66,0x0E,0x1C,0x3F,0x3F}, //2
  21.                       {0x7C,0x7C,0x0C,0x3C,0x3C,0x0C,0x7C,0x7C}, //3
  22.                       {0x1E,0x3E,0x76,0xE6,0xFF,0x7F,0x06,0x06},//4
  23.                       {0x3F,0x3F,0x30,0x3E,0x3F,0x03,0x3F,0x3E}, //5
  24.                       {0x3F,0x3F,0x30,0x3F,0x3F,0x33,0x3F,0x3F}, //6
  25.                       {0x3E,0x7E,0x66,0x06,0x06,0x06,0x06,0x06}, //7
  26.                       {0x3C,0x7E,0x66,0x66,0x7E,0x66,0x66,0x3C}, //8
  27.                       {0x3C,0x7E,0x66,0x66,0x7E,0x06,0x7E,0x3C}};//9
  28.  
  29. int pinPot = A0;    // Entrada para el potenciómetro.
  30. int valorPot = 0;   // variable para el valor del sensor.
  31. int pos = map (valorPot, 0, 1023, 0, 10);
  32.  
  33. #define NBR_MTX 2
  34. LedControl lc=LedControl(12,11,10, NBR_MTX);
  35. String digits= "1234567890";
  36. int digitCounter=0;
  37.  
  38. void setup() {
  39.   /*
  40.    The MAX72XX is in power-saving mode on startup,
  41.    we have to do a wakeup call
  42.    */
  43.    pinMode(pinPot, INPUT);
  44.   Serial.begin (9600);
  45.   Serial.println("Setup");
  46.   digitCounter=0;
  47.   for (int i=0; i< NBR_MTX; i++){
  48.     lc.shutdown(i,false);
  49.   /* Set the brightness to a medium values */
  50.     lc.setIntensity(i,1);
  51.   /* and clear the display */
  52.     lc.clearDisplay(i);
  53.   }
  54. }
  55.  
  56. void loop() {
  57.   // put your main code here, to run repeatedly:
  58. int Pos = analogRead(pinPot);
  59. Pos = map (Pos, 0, 1023, 0,10);
  60.   writeLetra(numeros[Pos]);
  61. Serial.println(pos);
  62.  
  63.  
  64. }
  65. void writeLetra (byte a[]){
  66.   for (int i = 0; i<8; i++){
  67.       lc.setRow(0,i,a[i]);
  68.  
  69.   }
  70.   delay(200);
  71.   for (int i = 0; i<8; i++){
  72.       lc.setRow(0,i,0);
  73.  
  74.   }
  75. }

Arduino Intro C Program


#include <UTFT.h>
#include <AVR/pgmspace.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

extern uint8_t BigFont[];
extern uint8_t SmallFont[];
extern uint8_t SevenSegNumFont[];


UTFT myGLCD(ILI9325D_8,38,39,40,41);

void setup() {
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  
  // Assign address manually. The addresses below will beed to be changed
  // to valid device addresses on your bus. Device address can be retrieved
  // by using either oneWire.search(deviceAddress) or individually via
  // sensors.getAddress(deviceAddress, index)
  // Note that you will need to use your specific address here
  //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };

  // Method 1:
  // Search for devices on the bus and assign based on an index. Ideally,
  // you would do this to initially discover addresses on the bus and then 
  // use those addresses and manually assign them (see above) once you know 
  // the devices on your bus (and assuming they don't change).
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
  
  // method 2: search()
  // search() looks for the next device. Returns 1 if a new address has been
  // returned. A zero might mean that the bus is shorted, there are no devices, 
  // or you have already retrieved all of them. It might be a good idea to 
  // check the CRC to make sure you didn't get garbage. The order is 
  // deterministic. You will always get the same devices in the same order
  //
  // Must be called before search()
  //oneWire.reset_search();
  // assigns the first address found to insideThermometer
  //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);
 
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC); 
  Serial.println();
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myGLCD.setColor(255,0,0);
  myGLCD.print("Motocyklowy", 200,40, 90);
  myGLCD.print("Komputer", 180,60, 90);
  myGLCD.print("Pokladowy", 160,50, 90);
  myGLCD.setColor(255,255,255);
  myGLCD.print("v 1.0a", 20,140,90);
  myGLCD.clrScr(); 

}
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  String mystring;
  mystring = String(tempC);
  //myGLCD.setFont(SevenSegNumFont);
  //myGLCD.InitLCD(0);
  myGLCD.print(mystring, 300, 60,90);
}
/*
 * Main function. It will request the tempC from the sensors and display on Serial.
 */
void loop() {
  myGLCD.setColor(255,0,0);
  myGLCD.setFont(BigFont);
  myGLCD.print("Temp.:", 319, 0 , 90);
  sensors.requestTemperatures(); // Send the command to get temperatures
  printTemperature(insideThermometer); // Use a simple function to print out the data

}

void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

C Program to Arduino rest [Mac/IP del shield]

How to write a C Program to Arduino rest [Mac/IP del shield] in C Programming Language ?



Solution:
/*C Program to Arduino rest [Mac/IP del shield]*/