Stock workshop Robotics and Modular Machines:: Difference between revisions
(→code) |
No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 18: | Line 18: | ||
2. the code. | 2. the code. | ||
a.without musical scale | '''a.without musical scale''' | ||
int myInts[8]; | |||
const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; | |||
int mySensVals[8]; | |||
int myMaxValues[8]; | |||
int myMinValues[8]; | |||
const int speakerPin = 9; | |||
void setup() | |||
{ | |||
Serial.begin(115200); | |||
// using those pins as output | |||
pinMode(speakerPin, OUTPUT); | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
pinMode(myPins[n], INPUT); | |||
digitalWrite(myPins[n], HIGH); | |||
myMinValues[n] = 1023; | |||
myMaxValues[n] = 0; | |||
} | |||
} | |||
void readSensor(int n) | |||
{ | |||
int val = analogRead(myPins[n]); | |||
if (val > myMaxValues[n]) | |||
{ | |||
myMaxValues[n] = val; | |||
} | |||
if (val < myMinValues[n]) | |||
{ | |||
myMinValues[n] = val; | |||
} | |||
mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); | |||
} | |||
void readSensors(void) | |||
{ | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
readSensor(n); | |||
} | |||
} | |||
void printSensors(void) | |||
{ | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
Serial.print(n, DEC); | |||
Serial.print(": "); | |||
Serial.print(mySensVals[n], DEC); | |||
Serial.print(", "); | |||
} | |||
Serial.println(); | |||
} | |||
void loop() | |||
{ | |||
readSensors(); | |||
printSensors(); | |||
} | |||
void makeTone(int freq){ | |||
digitalWrite(speakerPin, HIGH); | |||
delayMicroseconds(freq); | |||
digitalWrite(speakerPin, LOW); | |||
delayMicroseconds(freq); | |||
} | |||
'''with musical scale''' | |||
#include <stdint.h> | |||
#include <avr/io.h> | |||
#include <avr/interrupt.h> | |||
static uint8_t pin_bv[2]; | |||
volatile uint8_t * port[2]; | |||
volatile uint8_t * ddr[2]; | |||
static volatile uint16_t interval[2]; | |||
void setup_tone_pins(uint8_t pin_a, uint8_t pin_b) | |||
{ | |||
// setup output pins | |||
if (pin_a < 8) | |||
{ | |||
pin_bv[0] = _BV(pin_a); | |||
port[0] = &PORTD; | |||
ddr[0] = &DDRD; | |||
} | |||
else if (pin_a < 14) | |||
{ | |||
pin_bv[0] = _BV(pin_a - 8); | |||
port[0] = &PORTB; | |||
ddr[0] = &DDRB; | |||
} | |||
if (pin_b < 8) | |||
{ | |||
pin_bv[1] = _BV(pin_b); | |||
port[1] = &PORTD; | |||
ddr[1] = &DDRD; | |||
} | |||
else if (pin_b < 14) | |||
{ | |||
pin_bv[1] = _BV(pin_b - 8); | |||
port[1] = &PORTB; | |||
ddr[1] = &DDRB; | |||
} | |||
* port[0] &= (uint8_t)~pin_bv[0]; | |||
* port[1] &= (uint8_t)~pin_bv[1]; | |||
* ddr[0] |= pin_bv[0]; | |||
* ddr[1] |= pin_bv[1]; | |||
interval[0] = 0; | |||
interval[1] = 0; | |||
// setup Timer 1 | |||
TCCR1A = 0; | |||
TCCR1B = _BV(CS11); // prescaler at F_CPU / 8 = 2MHz | |||
TIMSK1 = _BV(OCIE1B) | _BV(OCIE1A); // enable both OCR interrupts | |||
} | |||
// Interrupt Service Routines | |||
ISR(TIMER1_COMPA_vect) | |||
{ | |||
if (interval[0]) | |||
{ | |||
* port[0] ^= pin_bv[0]; // toggle output pin polarity | |||
OCR1A = TCNT1 + interval[0]; // schedule next interrupt | |||
} else { | |||
* port[0] &= (uint8_t)~pin_bv[0]; // set output pin low | |||
} | |||
} | |||
ISR(TIMER1_COMPB_vect) | |||
{ | |||
if (interval[1]) | |||
{ | |||
* port[1] ^= pin_bv[1]; // toggle output pin polarity | |||
OCR1B = TCNT1 + interval[1]; // schedule next interrupt | |||
} else { | |||
* port[1] &= (uint8_t)~pin_bv[1]; // set output pin low | |||
} | |||
} | |||
void tone_a(uint16_t freq) | |||
{ | |||
// calculate interval from given freq | |||
if ((freq > 15) && (freq < 62500)) | |||
{ | |||
interval[0] = 1000000L / freq; | |||
OCR1A = TCNT1 + interval[0]; | |||
} else { | |||
interval[0] = 0; | |||
} | |||
} | |||
void tone_b(uint16_t freq) | |||
{ | |||
// calculate interval from given freq | |||
if ((freq > 15) && (freq < 62500)) | |||
{ | |||
interval[1] = 1000000L / freq; | |||
OCR1B = TCNT1 + interval[0]; | |||
} else { | |||
interval[1] = 0; | |||
} | |||
} | |||
int myInts[8]; | |||
const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; | |||
int mySensVals[8]; | |||
int myMaxValues[8]; | |||
int myMinValues[8]; | |||
const int speakerPin = 9; | |||
const int tones[] = {33, 37, 41, 46, 49, 55, 61}; | |||
void setup() | |||
{ | |||
setup_tone_pins(9, 10); | |||
// configure Timer2 for HIGH FREQUENCY PWM (62.5kHz) on pins 3 and 11 | |||
pinMode(3, OUTPUT); | |||
pinMode(11, OUTPUT); | |||
TCCR2B &= (uint8_t)~(_BV(CS20) | _BV(CS21) | _BV(CS22)); | |||
TCCR2B |= _BV(CS20); // run Timer2 at 16MHz | |||
Serial.begin(115200); | |||
// using those pins as output | |||
// pinMode(speakerPin, OUTPUT); | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
pinMode(myPins[n], INPUT); | |||
digitalWrite(myPins[n], HIGH); | |||
myMinValues[n] = 1023; | |||
myMaxValues[n] = 0; | |||
} | |||
} | |||
void readSensor(int n) | |||
{ | |||
int val = analogRead(myPins[n]); | |||
if (val > myMaxValues[n]) | |||
{ | |||
myMaxValues[n] = val; | |||
} | |||
if (val < myMinValues[n]) | |||
{ | |||
myMinValues[n] = val; | |||
} | |||
mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); | |||
} | |||
void readSensors(void) | |||
{ | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
readSensor(n); | |||
} | |||
} | |||
void printSensors(void) | |||
{ | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
Serial.print(n, DEC); | |||
Serial.print(": "); | |||
Serial.print(mySensVals[n], DEC); | |||
Serial.print(", "); | |||
} | |||
Serial.println(); | |||
} | |||
int calc_freq(int first_sens, int num_sens) | |||
{ | |||
int n, val, tone_nr, freq; | |||
//int oct = 0; | |||
int avg = 0; | |||
int count = 0; | |||
for (int i = 0; i < num_sens; i++) | |||
{ | |||
n = first_sens + i; | |||
val = mySensVals[n]; | |||
if (val > 16) | |||
{ | |||
// oct += 1 << i; | |||
avg += val; | |||
count++; | |||
} | |||
} | |||
if (count > 0) | |||
{ | |||
avg = avg / count; | |||
tone_nr = map(avg, 0, 255, 0, 5); | |||
// freq = tones[tone_nr] * (1 << oct); | |||
freq = tones[tone_nr] * (1 << count); | |||
return freq; | |||
} else { | |||
return 0; | |||
} | |||
} | |||
void loop() | |||
{ | |||
readSensors(); | |||
printSensors(); | |||
// set frequency on pin 9ß | |||
uint16_t freq = calc_freq(0, 3); | |||
tone_a(freq); | |||
// set volume on pin 3 | |||
if (freq > 15) | |||
{ | |||
analogWrite(3,255-mySensVals[3]); | |||
} else { | |||
analogWrite(3, 255); | |||
} | |||
// set frequency on pin 10 | |||
freq = calc_freq(4, 3); | |||
tone_b(freq); | |||
// set volume on pin 11 | |||
if (freq > 15) | |||
{ | |||
analogWrite(11,255-mySensVals[7]); | |||
} else { | |||
analogWrite(11, 255); | |||
} | |||
delay(133); | |||
} | |||
//void makeTone(int freq){ | |||
// digitalWrite(speakerPin, HIGH); | |||
//delayMicroseconds(freq); | |||
// digitalWrite(speakerPin, LOW); | |||
// delayMicroseconds(freq); | |||
//} | |||
3. c | 3. c | ||
Line 26: | Line 361: | ||
1. Arduino nano ( volume control. analog inputs,) | 1. Arduino nano ( volume control. analog inputs,) | ||
[[image:ememem.JPG|500px]] | |||
[[image:IMG 3194.JPG|500px]] | |||
[[image:nano.JPG|500px]] | |||
[[image:Nano3.JPG|500px]] | |||
[[image:sensors0.JPG|500px]] | |||
[[image:volumecontrol.JPG|500px]] | |||
[[image:IMG 3174.JPG|500px]] | |||
2. sensors | |||
== learning process== | == learning process== | ||
Line 94: | Line 438: | ||
5. http://farm9.staticflickr.com/8513/8533538707_91951352b4_m.jpg 6.http://farm9.staticflickr.com/8524/8534646722_55a7a6853d_m.jpg | 5. http://farm9.staticflickr.com/8513/8533538707_91951352b4_m.jpg 6.http://farm9.staticflickr.com/8524/8534646722_55a7a6853d_m.jpg | ||
7.http://farm9.staticflickr.com/8227/8534646606_f236361e0a_m.jpg | 7.http://farm9.staticflickr.com/8227/8534646606_f236361e0a_m.jpg | ||
'''First test of monolog machine! with Michaela's hacked TV''' | |||
http://farm9.staticflickr.com/8227/8570541176_10c17f7e8d_m.jpg | |||
http://farm9.staticflickr.com/8098/8569445317_1d95fc69f4_m.jpg | |||
http://farm9.staticflickr.com/8368/8570541630_3e660eeb12_m.jpg | |||
http://farm9.staticflickr.com/8383/8569445023_01d82c0990_m.jpg |
Latest revision as of 01:34, 19 March 2013
Robotics and Modular Machines
Hello Arduino!!!
Hello Mr.Stock!!!!
code
1. programming logic:
2. the code.
a.without musical scale
int myInts[8];
const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7};
int mySensVals[8];
int myMaxValues[8];
int myMinValues[8];
const int speakerPin = 9;
void setup() {
Serial.begin(115200); // using those pins as output
pinMode(speakerPin, OUTPUT); for (int n = 0; n < 8; n++) { pinMode(myPins[n], INPUT); digitalWrite(myPins[n], HIGH); myMinValues[n] = 1023; myMaxValues[n] = 0; }
}
void readSensor(int n) {
int val = analogRead(myPins[n]); if (val > myMaxValues[n]) { myMaxValues[n] = val; } if (val < myMinValues[n]) { myMinValues[n] = val; } mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255);
}
void readSensors(void) {
for (int n = 0; n < 8; n++) { readSensor(n); }
}
void printSensors(void) {
for (int n = 0; n < 8; n++) { Serial.print(n, DEC); Serial.print(": "); Serial.print(mySensVals[n], DEC); Serial.print(", "); } Serial.println();
}
void loop() {
readSensors(); printSensors();
}
void makeTone(int freq){
digitalWrite(speakerPin, HIGH); delayMicroseconds(freq); digitalWrite(speakerPin, LOW); delayMicroseconds(freq);
}
with musical scale
- include <stdint.h>
- include <avr/io.h>
- include <avr/interrupt.h>
static uint8_t pin_bv[2];
volatile uint8_t * port[2];
volatile uint8_t * ddr[2];
static volatile uint16_t interval[2];
void setup_tone_pins(uint8_t pin_a, uint8_t pin_b) { // setup output pins if (pin_a < 8) { pin_bv[0] = _BV(pin_a); port[0] = &PORTD; ddr[0] = &DDRD; } else if (pin_a < 14) { pin_bv[0] = _BV(pin_a - 8); port[0] = &PORTB; ddr[0] = &DDRB; }
if (pin_b < 8) { pin_bv[1] = _BV(pin_b); port[1] = &PORTD; ddr[1] = &DDRD; } else if (pin_b < 14) { pin_bv[1] = _BV(pin_b - 8); port[1] = &PORTB; ddr[1] = &DDRB; }
* port[0] &= (uint8_t)~pin_bv[0]; * port[1] &= (uint8_t)~pin_bv[1]; * ddr[0] |= pin_bv[0]; * ddr[1] |= pin_bv[1]; interval[0] = 0; interval[1] = 0;
// setup Timer 1 TCCR1A = 0; TCCR1B = _BV(CS11); // prescaler at F_CPU / 8 = 2MHz TIMSK1 = _BV(OCIE1B) | _BV(OCIE1A); // enable both OCR interrupts }
// Interrupt Service Routines
ISR(TIMER1_COMPA_vect)
{
if (interval[0])
{
* port[0] ^= pin_bv[0]; // toggle output pin polarity
OCR1A = TCNT1 + interval[0]; // schedule next interrupt
} else {
* port[0] &= (uint8_t)~pin_bv[0]; // set output pin low
}
}
ISR(TIMER1_COMPB_vect) { if (interval[1]) { * port[1] ^= pin_bv[1]; // toggle output pin polarity OCR1B = TCNT1 + interval[1]; // schedule next interrupt } else { * port[1] &= (uint8_t)~pin_bv[1]; // set output pin low } }
void tone_a(uint16_t freq)
{
// calculate interval from given freq
if ((freq > 15) && (freq < 62500))
{
interval[0] = 1000000L / freq;
OCR1A = TCNT1 + interval[0];
} else {
interval[0] = 0;
}
}
void tone_b(uint16_t freq) { // calculate interval from given freq if ((freq > 15) && (freq < 62500)) { interval[1] = 1000000L / freq; OCR1B = TCNT1 + interval[0]; } else { interval[1] = 0; } }
int myInts[8]; const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; int mySensVals[8]; int myMaxValues[8]; int myMinValues[8];
const int speakerPin = 9;
const int tones[] = {33, 37, 41, 46, 49, 55, 61};
void setup() {
setup_tone_pins(9, 10);
// configure Timer2 for HIGH FREQUENCY PWM (62.5kHz) on pins 3 and 11 pinMode(3, OUTPUT); pinMode(11, OUTPUT); TCCR2B &= (uint8_t)~(_BV(CS20) | _BV(CS21) | _BV(CS22)); TCCR2B |= _BV(CS20); // run Timer2 at 16MHz Serial.begin(115200); // using those pins as output
// pinMode(speakerPin, OUTPUT);
for (int n = 0; n < 8; n++) { pinMode(myPins[n], INPUT); digitalWrite(myPins[n], HIGH); myMinValues[n] = 1023; myMaxValues[n] = 0; }
}
void readSensor(int n) {
int val = analogRead(myPins[n]); if (val > myMaxValues[n]) { myMaxValues[n] = val; } if (val < myMinValues[n]) { myMinValues[n] = val; } mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255);
}
void readSensors(void) {
for (int n = 0; n < 8; n++) { readSensor(n); }
}
void printSensors(void) {
for (int n = 0; n < 8; n++) { Serial.print(n, DEC); Serial.print(": "); Serial.print(mySensVals[n], DEC); Serial.print(", "); }
Serial.println();
}
int calc_freq(int first_sens, int num_sens)
{
int n, val, tone_nr, freq; //int oct = 0; int avg = 0; int count = 0; for (int i = 0; i < num_sens; i++) { n = first_sens + i; val = mySensVals[n]; if (val > 16) {
// oct += 1 << i;
avg += val; count++; } } if (count > 0) { avg = avg / count; tone_nr = map(avg, 0, 255, 0, 5);
// freq = tones[tone_nr] * (1 << oct);
freq = tones[tone_nr] * (1 << count); return freq; } else { return 0; }
}
void loop()
{
readSensors(); printSensors(); // set frequency on pin 9ß uint16_t freq = calc_freq(0, 3); tone_a(freq); // set volume on pin 3 if (freq > 15) { analogWrite(3,255-mySensVals[3]); } else { analogWrite(3, 255); } // set frequency on pin 10 freq = calc_freq(4, 3); tone_b(freq); // set volume on pin 11 if (freq > 15) { analogWrite(11,255-mySensVals[7]); } else { analogWrite(11, 255); } delay(133);
}
//void makeTone(int freq){
// digitalWrite(speakerPin, HIGH); //delayMicroseconds(freq); // digitalWrite(speakerPin, LOW); // delayMicroseconds(freq);
//}
3. c
hardware
1. Arduino nano ( volume control. analog inputs,)
2. sensors
learning process
1. AC DC
2. H-bridge
document photos...
for optical instrument
code:
//no moter //sensor pin const int ldrPin = A1;
//light pin const int ledPin = 13; //speaker pin const int speakerPin = 9;
void setup(){
Serial.begin(115200); // using those pins as output
pinMode(ledPin, OUTPUT); pinMode(speakerPin, OUTPUT);
}
void loop() {
int ldrVal = analogRead(ldrPin); //map the value of the light sensor ldrVal = map(ldrVal, 170, 600, 0, 1023); //constrain the vaule from 0 to 1023 ldrVal = constrain(ldrVal, 0, 1023);
// turning on the led
digitalWrite(ledPin, HIGH);
Serial.println(ldrVal);
makeTone(ldrVal);
}
void makeTone(int freq){
digitalWrite(speakerPin, HIGH); delayMicroseconds(freq); digitalWrite(speakerPin, LOW); delayMicroseconds(freq);
}
Hardware:
white board :
1. 2. 3. 4.
5. 6. 7.
First test of monolog machine! with Michaela's hacked TV