Stock workshop Robotics and Modular Machines:
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