April 5, 2008
WikiSource Code for Arduino
#wiki
Source Code for Arduino
/*
Net connect
Language: Wiring/Arduino
This program enables an Arduino to connect to a chat server
*/
// Defines for the Lantronix device's status (used for staus variable):
#define disconnected 0
#define connected 1
#define connecting 2
// Defines for I/O pins:
#define exitPin 2
#define rightLED 3
#define leftLED 4
#define connectionLED 5
#define connectButtonLED 6
#define deviceResetPin 7
// variables:
int inByte= -1; // incoming byte from serial RX
int status = 0; // Lantronix device's connection status
// variables for the sensors:
byte connectButton = 0; // state of the exit button
byte lastConnectButton = 0; // previous state of the exit button
/*
when the exit button is pressed, or the accelerometer
passes the left or right threshold, the client should send a message
to the server. The next two variables get filled with a value
when either of those conditions is met. Otherwise, these
variables are set to 0.
*/
byte paddleMessage = 0; // message sent to make a paddle move
byte connectMessage = 0; // message sent to connect or disconnect
void setup() {
// set the modes of the various I/O pins:
pinMode(exitPin, INPUT);
pinMode(rightLED, OUTPUT);
pinMode(leftLED, OUTPUT);
pinMode(connectionLED, OUTPUT);
pinMode(connectButtonLED, OUTPUT);
pinMode(deviceResetPin, OUTPUT);
// start serial port, 9600 8-N-1:
Serial.begin(9600);
// reset the Lantronix device:
resetDevice();
// blink the exit button LED to signal that we're ready for action:
blink(3);
}
void loop() {
// read the inputs:
readSensors();
// set the indicator LEDS:
setLeds();
// check the state of the client and take appropriate action:
stateCheck();
}
void readSensors() {
// thresholds for the accelerometer values:
int leftThreshold = 400 ;
int rightThreshold = 400;
// read the X axis of the accelerometer:
int leftx = analogRead(0);
int rightx = analogRead(5);
//Serial.println (x);
//Serial.println (rightx);
// let the ADC settle:
delay(25);
// if the acceleromter has passed either threshold,
// set paddleMessage to the appropriate message, so it can
// be sent by the main loop:
if (leftx < leftThreshold) {
paddleMessage = 'l';
}
if (rightx < rightThreshold) {
paddleMessage = 'r';
}
// read the connectButton, look for a low-to-high change:
connectButton = digitalRead(exitPin);
if (connectButton == HIGH ) {
if (connectButton != lastConnectButton) {
// turn on the exit button LED to let the user
// know that they hit the button:
digitalWrite(connectButtonLED, HIGH);
connectMessage = 'x';
}
}
// save the state of the exit button for next time you check:
lastConnectButton = connectButton;
}
void setLeds() {
// this should happen no matter what state the client is in,
// to give local feedback every time a sensor senses a change
// set the L and R LEDs if the sensor passes the appropriate threshold:
switch (paddleMessage) {
case 'l':
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, LOW);
break;
case 'r':
digitalWrite(rightLED, HIGH);
digitalWrite(leftLED, LOW);
break;
case 0:
digitalWrite(rightLED, LOW);
digitalWrite(leftLED, LOW);
}
// set the connect button LED based on the connectMessage:
if (connectMessage !=0) {
digitalWrite(connectButtonLED, HIGH);
}
else {
digitalWrite(connectButtonLED, LOW);
}
// set the connection LED based on the client's status:
if (status == connected) {
// turn on the connection LED:
digitalWrite(connectionLED, HIGH);
}
else {
// turn off the connection LED:
digitalWrite(connectionLED, LOW);
}
}
void stateCheck() {
// Everything in this method depends on the client's status:
switch (status) {
case connected:
// if you're connected, listen for serial in:
while (Serial.available() > 0) {
// if you get a 'D', it's from the Lantronix device,
// telling you that it lost the connection:
if (Serial.read() == 'D') {
status = disconnected;
}
}
// if there's a paddle message to send, send it:
if (paddleMessage != 0) {
Serial.print(paddleMessage);
// reset paddleMessage to 0 once you've sent the message:
paddleMessage = 0;
}
// if there's a connect message to send, send it:
if (connectMessage != 0) {
// if you're connected, disconnect:
Serial.print(connectMessage);
// reset connectMessage to 0 once you've sent the message:
connectMessage = 0;
}
break;
case disconnected:
// if there's a connect message, try to connect:
if (connectMessage !=0 ) {
deviceConnect();
// reset connectMessage to 0 once you've sent the message:
connectMessage = 0;
}
break;
// if you sent a connect message but haven't connected yet,
// keep trying:
case connecting:
// read the serial port:
if (Serial.available ()) {
inByte = Serial.read();
// if you get a 'C' from the Lantronix device,
// then you're connected to the server:
if (inByte == 'C') {
status = connected;
}
else {
// if you got anything other than a C, try again:
deviceConnect();
}
}
break;
}
}
void deviceConnect() {
/*
send out the server address and
wait for a "C" byte to come back.
fill in your personal computer's numerical address below:
*/
Serial.print("C128.122.151.128/8080\n\r");
status = connecting;
}
// Take the Lantronix device's reset pin low to reset it:
void resetDevice() {
digitalWrite(deviceResetPin, LOW);
delay(50);
digitalWrite(deviceResetPin, HIGH);
// pause to let Lantronix device boot up:
delay(2000);
}
// Blink the connect button LED:
void blink(int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(connectButtonLED, HIGH);
delay(200);
digitalWrite(connectButtonLED, LOW);
delay(200);
}
}
Continue Reading
Back to Archive