jueves, 18 de mayo de 2017

Mega 2560 Projects


1. Mega 2560 And Visible Light Communication With a Laser

Diagram:
Code:
int lightPin = 0;  //define a pin for Photo resistor
int led=13;     //define a pin for LED

void setup()
{
    Serial.begin(9600);  //Begin serial communcation
    pinMode( led, OUTPUT );
}

void loop()
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);
  Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
  delay(1000);               // wait for a second
 }

_______________________________________________________________________

2. Mega 2560 And PCF8591 (Digital To Analog)

Diagram:
Code:
#include "Wire.h"
#define PCF8591 (0x90 >> 1) 

int Ain = 0;

int RawValue0 = 0;
int DACout = 0;

float DACoutVolt = 0.0;
float Voltage = 0.0;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
}
void loop()
{
  for (int i = 0; i < 256; i++)
  {
    Wire.beginTransmission(PCF8591); 
    Wire.write(0x40); // sets the PCF8591 into a DA mode
    Wire.write(i); // sets the outputn
    Wire.endTransmission();     
    
    delay(500);    
    
    DACout = i;
    RawValue0 = analogRead(Ain);
    Voltage = (RawValue0 * 5.0 ) / 1024.0;
    DACoutVolt = (DACout * 5.0)/256.0;
    Serial.print("DAC Out = ");
    Serial.print(DACout);
    Serial.print("\tDAC Target Voltage = ");
    Serial.print(DACoutVolt, 3);
    Serial.print("\tRaw ADC Value = ");
    Serial.print(RawValue0);
    Serial.print("\tVoltage = ");
    Serial.println(Voltage, 3);
  }
}

_______________________________________________________________________

3. Mega 2560 And PCF8574 Bus

Diagram:
Code:
#include <Wire.h>

#define PCF8574_I2C_ADDR 0x20

#define P0 0
#define P1 1
#define P2 2
#define P3 3
#define P4 4
#define P5 5
#define P6 6
#define P7 7

void setup() {
  Wire.begin();        // initialize the I2C/TWI interface
}

void loop() {
  // send the data to the LEDs
  turnPinLowOnPCF8574P(PCF8574_I2C_ADDR, P4);
  delay(300);
  turnPinLowOnPCF8574P(PCF8574_I2C_ADDR, P5);
  delay(300);
  turnPinLowOnPCF8574P(PCF8574_I2C_ADDR, P6);
  delay(300);
  turnPinLowOnPCF8574P(PCF8574_I2C_ADDR, P7);
  delay(300);
  turnPinLowOnPCF8574P(PCF8574_I2C_ADDR, P6);
  delay(300);
  turnPinLowOnPCF8574P(PCF8574_I2C_ADDR, P5);
  delay(300);  
}

void turnPinLowOnPCF8574P(int chipI2CAddress, int pinNumber) {
  Wire.beginTransmission(chipI2CAddress);
  Wire.write(~(1 << pinNumber));
  Wire.endTransmission();
}


_______________________________________________________________________

4. Mega 2560: Infrarred Receiver + TV Remote





Diagram
Code
#include <IRremote.h>
int redledState = LOW;
int greenledState = LOW;
int fanState = LOW;
int fanPin = 10;
int irpin = 8;
IRrecv irIN(irpin);
decode_results results;
void setup()
{
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  irIN.enableIRIn();
}
void loop()
{
  if(irIN.decode(&results))
  {
    Serial.println(results.value, HEX);
    irIN.resume();
  }


  if(results.value == 0xFEAC02E5)//this value was given back from the IR receiver and represents a key on the remote.//
  {
   fanState = !fanState;
   digitalWrite(fanPin, fanState);
   delay(15);
  }
}

_______________________________________________________________________

5. RF Communication With Mega 2560 And Arduino UNO


Diagrams:




Codes:


Sender:

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);    
    Serial.println("Tx RF");

    // Se inicializa el RF
    vw_setup(2000); // velocidad: Bits per segundo
    vw_set_tx_pin(2); //Pin 2 como salida para el RF 
}

void loop()
{

    
    while (Serial.available() > 0) 
    {
      char dato[1];
      dato[0] = Serial.read();
      //Enviamos el carácter recibido al RF
      vw_send((uint8_t*)dato,sizeof(dato));
      vw_wait_tx();         
    }
    delay(200);
}


Receiver:

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);  // Debugging only
    Serial.println("setup");

    // Se inicializa el RF
    vw_setup(2000);  // velocidad: Bits per segundo
    vw_set_rx_pin(2);  //Pin 2 como entrada del RF
    vw_rx_start();       // Se inicia como receptor
    
    pinMode(13, OUTPUT);    //Configuramos el pin del Led como entrada
    digitalWrite(13, false);
}

void loop()
{
    uint8_t dato;
    uint8_t datoleng=1;
    //verificamos si hay un dato valido en el RF
    if (vw_get_message(&dato,&datoleng))
    {
        if((char)dato=='y')
        {
            digitalWrite(13, true); //Encendemos el Led
        }
        else if((char)dato=='z')
        {
            digitalWrite(13, false); //Apagamos el Led
        }            
    }
} 
 
_______________________________________________________________________

6. Serial Communication With Mega 2560 And Arduino UNO

Diagram:
Codes:

Sender:

// Sender Information
unsigned char START_BYTE = 0x53; // ASCII "S"
unsigned char counterValue = 0;
unsigned char staticValue = 5;
unsigned char checksum = 0;
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  
  // Increment our counter
  counterValue = counterValue + 1;
  
  // Check for overflow, and loop
  if (counterValue > 250)
    counterValue = 0;
    
  // Calculate our checksum
  checksum = counterValue + staticValue;
  
  // Important: Serial.write must be used, not print
  Serial.write(START_BYTE);
  Serial.write(counterValue);
  Serial.write(staticValue);
  Serial.write(checksum);
  
  // We only need to send a packet every 250 ms. 
  // If your code starts to get complicated, 
  // consider using a timer instead of a delay
  delay(250); 

Receiver:

// Sender Information
unsigned char START_BYTE = 0x53; // ASCII "S"
unsigned char counterValue = 0;
unsigned char staticValue = 0;
unsigned char checksum = 0;
 
// Sync Byte flag
boolean syncByteFound = 0;
 
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}
 
void loop() {
  
  unsigned char rxByte = 0;
  unsigned char calculatedChecksum = 0;
  
  // Check to see if there's something to read
  if (Serial1.available() > 0 ) {
    
    // If we're waiting for a new packet, check for the sync byte
    if (syncByteFound == 0) {
      rxByte = Serial1.read();
      if (rxByte == 0x53)
        syncByteFound = 1;
    }
    
    // If we've found our sync byte, check for expected number of bytes
    if (Serial1.available() > 2) {
      counterValue = Serial1.read();
      staticValue = Serial1.read();
      checksum = Serial1.read();
        
      calculatedChecksum =  counterValue + staticValue;
      
      // Print out our serial information to debug
      Serial.print("["); Serial.print("S"); Serial.print("]");
      Serial.print("["); Serial.print(counterValue); Serial.print("]");
      Serial.print("["); Serial.print(staticValue); Serial.print("]");
      Serial.print("["); Serial.print(checksum); Serial.print("]");
      
      if (calculatedChecksum == checksum) 
        Serial.println("[Checksum Passed]");


      else
        Serial.println("[Checksum FAILED]");
            
      syncByteFound = 0;
    }
  }
}


_______________________________________________________________________

7. Projects With Arduino UNO

http://guillengap.blogspot.com/2016/07/ebook-projects-with-arduino-uno.html


https://www.amazon.com/dp/B01EZO7DBA

https://www.amazon.com/Guillermo%20Perez/e/B01EZOTUVQ/

guillengap@gmail.com

No hay comentarios:

Publicar un comentario