Commit 2a92e9a6 authored by Beck-Sisyphus's avatar Beck-Sisyphus
Browse files

Science station communication code created by Akira Wong

parent 4f5d1f92
No preview for this file type
No preview for this file type
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
char Message[] = "";
#include <Wire.h>
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop()
{
Message = ""
//Humidity Reading
// read the value from the pin
double humReading = 0;
humReading = analogRead(humPin);
// calculate the sensor humidity
if(humcalibrated){
double sensorRH = humReading/humcalibratedValue*100.0;
Serial.print(sensorRH);
Serial.println("%");
}
//End Humidity Reading
//pH Reading
float Count;
float Voltage;
float pH;
Count = analogRead(pHPin);
Voltage = Count / 1023 * 5.0;// convert from count to raw voltage
pH = Voltage/0.36;
Serial.print("Voltage: ");
Serial.println(Voltage);
Serial.print("pH: ");
Serial.println(pH);
//End pH Reading
//CURRENT MESSAGE LENGTH IS 12
Message = (("p:" + pH).substring(0,3)) + (("h:" + sensorRH).substring(0,3));
delay(TimeBetweenReadings);// delay in between reads for stability
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.write(Message); // respond with message of 6 bytes
// as expected by master
}
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// PWM output ports
const int LEFT_MOTOR_PWM = 3;
const int RIGHT_MOTOR_PWM = 5;
// Port to listen for UDP connections on
const int UDP_PORT = 8888;
// ROBOTEQ constants
const int ROBOTEQ_MIN_FREQUENCY = 1250, ROBOTEQ_MAX_FREQUENCY = 1750;
Servo leftMotor;
Servo rightMotor;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// Initialize drive motors
leftMotor.attach(LEFT_MOTOR_PWM);
rightMotor.attach(RIGHT_MOTOR_PWM);
leftMotor.writeMicroseconds(0);
rightMotor.writeMicroseconds(0);
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(UDP_PORT);
Serial.begin(9600);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
int *buffer;
for (int i = 0; i < packetSize; i++) {
readByteAsInt(packetBuffer[i], buffer, 1);
}
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println("Converted:");
for (int i = 0; i < packetSize; i++) {
Serial.println(buffer[i]);
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
const int humPin = A0;
boolean humcalibrated = true;
double humcalibratedValue = 640.0;
int TimeBetweenReadings = 500;
const int pHPin = A1;
void setup() {
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
//Humidity Reading
// read the value from the pin
double humReading = 0;
humReading = analogRead(humPin);
// calculate the sensor humidity
if(humcalibrated){
double sensorRH = humReading/humcalibratedValue*100.0;
Serial.print(sensorRH);
Serial.println("%");
}
//End Humidity Reading
//pH Reading
float Count;
float Voltage;
float pH;
Count = analogRead(pHPin);
Voltage = Count / 1023 * 5.0;// convert from count to raw voltage
pH = Voltage/0.36;
Serial.print("Voltage: ");
Serial.println(Voltage);
Serial.print("pH: ");
Serial.println(pH);
//End pH Reading
delay(TimeBetweenReadings);// delay in between reads for stability
}
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void readByteAsInt(byte data, int* buffer, byte bufferSize) {
byte mask = 0x03;
for(byte i = 0; i < bufferSize; i++)
{
*buffer = data & mask;
data >>= 2;
buffer++;
}
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
int *buffer;
for (int i = 0; i < packetSize; i++) {
readByteAsInt(packetBuffer[i], buffer, 1);
}
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println("Converted:");
for (int i = 0; i < packetSize; i++) {
Serial.println(buffer[i]);
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
/*
sonar_distance.ino
Created by Taylor Cramer for the Husky Robotics Team
Made for use with an HC-SR04 sound-based distance center.
*/
const uint8_t ANALOG_PIN_OFFSET = 14;
const uint8_t TRIG_PIN = 2, ECHO_PIN = 3;
const int NUM_SMOOTHING_FRAMES = 5;
/*
Triggers a short burst of sound
*/
void triggerSound() {
// Start with a short LOW pulse to ensure a clean HIGH pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, LOW);
}
/*
Converts sound to distance
@param microseconds length of time before echo has heard
@return the distance sound has traveled (inches)
*/
int timeToInches(long microseconds) {
long microseconds_per_inch = 74;
return (int) (microseconds / microseconds_per_inch / 2);
}
void setup() {
// Starts serial input
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Trigger a burst of sound and
triggerSound();
// Read in the duration of the echo and convert to inches
long ping_duration = pulseIn(ECHO_PIN, HIGH);
// This is the distance!!! USE THIS!!!!!
int inches = timeToInches(ping_duration);
Serial.println(inches);
delay(100);
}
#include <Wire.h>
void setup()
{
Wire.begin(4);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
delay(10);
}
void receiveEvent(int howMany)
{
boolean c = Wire.read();
if (c)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}
#include <Wire.h>
//With two arduino unos: Connect A4 to A4, A5 to A5, and share common ground.
void setup()
{
Wire.begin();
}
boolean message = 1;
byte x=0;
void loop()
{
Wire.beginTransmission(4);
Wire.write(message);
Wire.endTransmission();
delay(1000);
x++;
message = !message;
}
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment