/* Fab Adademy 2018 * Fab Lab Oulu * * Code for ATmega328P-AU * In Final Project: Remote Game Controller * * Author: Jari Uusitalo * Licensed under MIT license * * Uses Wire library for communicating with acceleration sensor through I2C protocol. * Sends x,y,z axis' data through serial line, along with button mode information. * Mode is also displyed on the device by RGB led. */ #include #include SoftwareSerial mySerial(1, 0); // RX, TX // constants won't change. They're used here to set pin numbers: const int buttonPin = 10; // the number of the pushbutton pin const int buttonPin2 = 6; // the number of the pushbutton pin const int ledPin = 5; // the number of the LED pin const int RGBPin1 = 7; const int RGBPin2 = 8; const int RGBPin3 = 9; // variables will change: int buttonState = 0; // variable for reading the pushbutton status int buttonState2 = 0; // variable for reading the pushbutton status int led_status = 1; // Status of the LED. This is for switch mechanic. int button_lock = 1; // Button press/release lock. This is to disable serialized button activation. int button_lock2 = 1; // Button press/release lock. This is to disable serialized button activation. int modeA = 0; int modeB = 0; #define accel_module (0x53) byte values[6] ; char output[512]; void setup() { pinMode(RGBPin1, OUTPUT); pinMode(RGBPin2, OUTPUT); pinMode(RGBPin3, OUTPUT); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); pinMode(buttonPin2, INPUT); Wire.begin(); mySerial.begin(9600); Wire.beginTransmission(accel_module); Wire.write(0x2D); Wire.write(0); Wire.endTransmission(); Wire.beginTransmission(accel_module); Wire.write(0x2D); Wire.write(16); Wire.endTransmission(); Wire.beginTransmission(accel_module); Wire.write(0x2D); Wire.write(8); Wire.endTransmission(); } void loop() { buttonState = digitalRead(buttonPin); buttonState2 = digitalRead(buttonPin2); int xyzregister = 0x32; int x, y, z; Wire.beginTransmission(accel_module); Wire.write(xyzregister); Wire.endTransmission(); Wire.beginTransmission(accel_module); Wire.requestFrom(accel_module, 6); int i = 0; while(Wire.available()){ values[i] = Wire.read(); i++; } Wire.endTransmission(); x = (((int)values[1]) << 8) | values[0]; y = (((int)values[3])<< 8) | values[2]; z = (((int)values[5]) << 8) | values[4]; sprintf(output, "%d,%d,%d,%d,%d", x, y, z, modeA, modeB); mySerial.print(output); mySerial.println(""); if (buttonState == LOW) { if (button_lock) { if (modeA <3) { modeA += 1; } else { modeA = 0; } } button_lock = 0; // Button press - locked } else { button_lock = 1; // Button release - unlocked } if (buttonState2 == LOW) { if (button_lock2) { if (modeB == 0) { modeB = 1; } else { modeB = 0; } } button_lock2 = 0; // Button press - locked } else { button_lock2 = 1; // Button release - unlocked } if (modeA == 0) { digitalWrite(RGBPin1,HIGH); digitalWrite(RGBPin2,HIGH); digitalWrite(RGBPin3,HIGH); } if (modeA == 1) { digitalWrite(RGBPin1,LOW); digitalWrite(RGBPin2,HIGH); digitalWrite(RGBPin3,HIGH); } if (modeA == 2) { digitalWrite(RGBPin1,HIGH); digitalWrite(RGBPin2,LOW); digitalWrite(RGBPin3,HIGH); } if (modeA == 3) { digitalWrite(RGBPin1,HIGH); digitalWrite(RGBPin2,HIGH); digitalWrite(RGBPin3,LOW); } if (modeB == 1) { digitalWrite(ledPin,HIGH); } else { digitalWrite(ledPin,LOW); } delay(10); }