Assignment 14
Now
7)
to configure the other Bluetooth module as a master device. First we
will check the baud rate to make sure it’s the same 38400 as the
slave device. Then by typing “AT+ROLE=1” we will set the
Bluetooth module as a master device. After this using the
“AT+CMODE=0” we will set the connect mode to “fixed address”
and using the “AT+BIND=” command we will set the address of the
slave device that we previously wrote down.
//#define F_CPU 20000000UL #includechar control(); #define BUTTON PA3 #define x PA2 #define y PA3 #define btn PA4 #define val 0 #define tx PA0 #define rx PA1 SoftwareSerial ser(0, 1); void setup() { // put your setup code here, to run once: pinMode(btn,INPUT); pinMode(x,INPUT); pinMode(y,INPUT); ser.begin(9600); } int casse=0; int casse1=0; void loop() { // put your main code here, to run repeatedly: while(!(control() == '1')) {} ser.print("b"); while(!(control() == '2')) {} ser.print(digitalRead(btn)); casse = analogRead(x); casse1 = analogRead(y); casse = map(casse ,0,1024,0,20); casse1 = map(casse1 , 0,1024,0,20); //delay(200); while(!(control() == '1')) {} ser.print("x"); while(!(control() == '2')) {} ser.print(casse); while(!(control() == '1')) {} ser.print("y"); while(!(control() == '2')) {} ser.print(casse1); } char control () { while (!ser.available()) { } return ser.read(); }
#include#define x PA2 #define y PA3 #define btn PA4 #define tx PA0 #define rx PA1 SoftwareSerial ser(0, 1); void setup() { // put your setup code here, to run once: ser.begin(9600); pinMode(x,OUTPUT); pinMode(y,OUTPUT); pinMode(btn,OUTPUT); } void loop() { // put your main code here, to run repeatedly: ser.print("1"); if (ser.available()) { char val = ser.read(); if (val == 'x') { ser.print("2"); while(1) if (ser.available()) { int mov = (ser.parseInt()); if(calc(mov)==1) ser.print("a"); else ser.print("b"); delay(10); ser.print("1"); if (mov > 5) {digitalWrite(x,HIGH); break; } else { digitalWrite(x,LOW); break; } } } else if (val == 'y') { ser.print("2"); while(1) if (ser.available()) { int mov = (ser.parseInt()); if(calc(mov)==1) ser.print("c"); else ser.print("d"); delay(10); ser.print("1"); if (mov > 5) {digitalWrite(y,HIGH); break; } else { digitalWrite(y,LOW); break; } } } else if (val == 'b') { ser.print("2"); while(1) if (ser.available()) { int mov = (ser.parseInt()); ser.print("1"); if (mov == 1 ) {digitalWrite(btn,HIGH); ser.print("e"); break; } else { ser.print("f"); digitalWrite(btn,LOW); break; } } } } delay(100); } int calc (int mov) { if(mov>6) return 0; else return 1; }
x=100 y=100 import time import pyautogui import serial ser = serial.Serial('/dev/ttyUSB0') kk=0 line1=0 line2=0 line3=0 while 1 : line = ser.read() print line print "---------" if line == 'a' : if(x< 6<00): x=x+10 elif line == 'b' : if(x>0): x=x-10 elif line == 'c' : if(y< 600): y=y+10 elif line == 'd' : if(y>0): y=y-10 if line == 'e' : pyautogui.click(x,y) elif line == 'f' : print "nothing" pyautogui.moveTo(x, y) #time.sleep(1); #print "btn = " , line1 , " x = " , line2 , " y = " , line3
/** * Simple Read * * Read data from the serial port and change the color of a rectangle * when a switch connected to a Wiring or Arduino board is pressed and released. * This example works with the Wiring / Arduino program that follows below. */ import processing.serial.*; Serial myPort; // Create object from Serial class char val; // Data received from the serial port int x = 100, y=100 , btn=0; void setup() { size(200, 200); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; myPort = new Serial(this, "/dev/ttyUSB0", 9600); } void draw() { if ( myPort.available() > 0) { // If data is available, val = myPort.readChar(); // read it and store it in val if (val == 'a') { if(x<300) x=x+50; } else if (val == 'b') { if(x<50) x=x+50; } else if (val == 'c') { if(y<300) y=y+50; } else if (val == 'd') { if(x>50) x=x-50; } else if (val == 'e') { btn=1; } else if (val == 'f') { btn=0; } } background(x); // Set background to white if (btn == 1) { // If the serial value is 0, fill(y); // set fill to black } else { // If the serial value is not 0, fill(204-y); // set fill to light gray } rect(50, 50, 100, 100); }