Editing
Hack Notes CVA 090324
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==The Code== *Important: "On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11." *Given that analogWrite() takes a value 0-255 and that compass_angle is a value from -180 to 180. We thought: **vibro_strength = (255 - abs(compass_angle) * (255/180)) *But then it turned out the transistor was working the opposite of expected, and the above code made the vibration strongest towards south! So: **vibro_strength = abs(compass_angle) * (255/180) *Of course this has East and West not instanteneously distinguishable from the pager motor speed. <pre> /* Some Htachi HM55B Compass reading code copied from: kiilo kiilo@kiilo.org License: http://creativecommons.org/licenses/by-nc-sa/2.5/ch/ */ #include <LCD4Bit.h> // module for lcd #include <math.h> // #include <stdio.h> //// VARS byte VIB1_pin = 3; byte VIB2_pin = 5; byte CLK_pin = 6; byte EN_pin = 4; byte DIO_pin = 11; int X_Data = 0; int Y_Data = 0; int angle; float vibro_strength; //Create LCD object with 2 line display LCD4Bit lcd = LCD4Bit(2); char string[20]; int status; unsigned long serialTimer = millis(); //// FUNCTIONS void ShiftOut(int Value, int BitsCount) { for(int i = BitsCount; i >= 0; i--) { digitalWrite(CLK_pin, LOW); if ((Value >> i) & 1) { digitalWrite(DIO_pin, HIGH); //Serial.print("1"); } else { digitalWrite(DIO_pin, LOW); //Serial.print("0"); } digitalWrite(CLK_pin, HIGH); delayMicroseconds(1); } //Serial.print(" "); } int ShiftIn(int BitsCount) { int ShiftIn_result; ShiftIn_result = 0; pinMode(DIO_pin, INPUT); for(int i = BitsCount; i >= 0; i--) { digitalWrite(CLK_pin, HIGH); delayMicroseconds(1); if (digitalRead(DIO_pin) == HIGH) { ShiftIn_result = (ShiftIn_result << 1) + 1; //Serial.print("x"); } else { ShiftIn_result = (ShiftIn_result << 1) + 0; //Serial.print("_"); } digitalWrite(CLK_pin, LOW); delayMicroseconds(1); } //Serial.print(":"); // below is difficult to understand: // if bit 11 is Set the value is negative // the representation of negative values you // have to add B11111000 in the upper Byte of // the integer. // see: http://en.wikipedia.org/wiki/Two%27s_complement if ((ShiftIn_result >> 11) & 1) { ShiftIn_result = (B11111000 << 8) | ShiftIn_result; } return ShiftIn_result; } void HM55B_Reset() { pinMode(DIO_pin, OUTPUT); digitalWrite(EN_pin, LOW); ShiftOut(B0000, 3); digitalWrite(EN_pin, HIGH); } void HM55B_StartMeasurementCommand() { pinMode(DIO_pin, OUTPUT); digitalWrite(EN_pin, LOW); ShiftOut(B1000, 3); digitalWrite(EN_pin, HIGH); } int HM55B_ReadCommand() { int result = 0; pinMode(DIO_pin, OUTPUT); digitalWrite(EN_pin, LOW); ShiftOut(B1100, 3); result = ShiftIn(3); return result; } void setup() { Serial.begin(115200); pinMode(EN_pin, OUTPUT); pinMode(CLK_pin, OUTPUT); pinMode(DIO_pin, INPUT); lcd.init(); lcd.commandWrite(0x0E); //curson on, display on, blink off HM55B_Reset(); } void loop() { if (millis() - serialTimer > 50 ) { //read compass and print data to lcd and serial out lcd.cursorTo(2,0); HM55B_StartMeasurementCommand(); // necessary!! //delay(40); // the data is 40ms later ready status = HM55B_ReadCommand(); Serial.print(status); // read data and print Status sprintf(string, "%2d", status); lcd.printIn(string); Serial.print(" "); lcd.printIn(" "); X_Data = ShiftIn(11); // Field strength in X Y_Data = ShiftIn(11); // and Y direction Serial.print(X_Data); // print X strength sprintf(string, "%3d", X_Data); lcd.printIn(string); Serial.print(" "); lcd.printIn(" "); Serial.print(Y_Data); // print Y strength sprintf(string, "%3d", Y_Data); lcd.printIn(string); Serial.print(" "); lcd.printIn(" "); digitalWrite(EN_pin, HIGH); // ok deselect chip angle = 180 * (atan2(-Y_Data , X_Data) / M_PI); // angle is atan( -y/x) !!! Serial.print(angle); // print angle sprintf(string, "%4d", angle); lcd.printIn(string); Serial.println(" "); lcd.printIn(" "); //control motors vibro_strength = abs(angle) * (215.0/180.0); pinMode(VIB1_pin, OUTPUT); analogWrite(VIB1_pin, int(vibro_strength)); lcd.cursorTo(1,0); lcd.printIn("V1="); sprintf(string, "%3d", int(vibro_strength)); lcd.printIn(string); vibro_strength = 215 - (abs(angle) * (215.0/180.0)); pinMode(VIB2_pin, OUTPUT); analogWrite(VIB2_pin, int(vibro_strength)); lcd.printIn(" V2="); sprintf(string, "%3d", int(vibro_strength)); lcd.printIn(string); } } </pre>
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
- Status: MOVED
- Donate
- ABOUT
- Accessibility
- Vision
- Blog
Manual
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
Events
EVENTS
Guilds
GUILDS
- Meta
- Electronics
- Fabrication
- Games
- Music
- Library
- Neuro
- Philosophy
- Funding
- Art
- Crypto
- Documentation/Wiki
Wiki
Recent Changes
Random Page
Help
Categories
(Edit)
Tools
What links here
Related changes
Special pages
Page information