Al-Nassim مشروع اصدار نغمات صوتية بواسطة الاوردينو

Eng Mohamad

مدير عام
طاقم الإدارة
وصف المشروع
في هذا المشروع سنجعل الاوردينو يصدر نغمات صوتية مختلفة حسب شدة الإضاءة الساقطة على المقاومة الضوئية وذلك بخطوات بسيطة ولن نحتاج الا لمقاومة ضوئية وسماعة وبعض الأكواد البرمجية وبعد اتمام توصيل الدارة ما عليك الا ان تحرك يديك امام المقاومة الضوئية وتستمع إلى أصوات النغمات المختلفة.
do.php


الشرح
• ادخال المقاومة الضوئية على المشروع يفسر كالتالي : تعتبر المقاومة الضوئية عنصر إدخال للأردوينو وذلك على الطرف رقم A2، فعندما تعمل المقاومة الضوئية فإنها ستسمح بمرور جهد يتم قراءته بالاردوينو،على انه اشارة دخل ، اما إذا لم تتوفر إشارة دخل؟ لن يعرف الاردوينو أن هذه القيمة صفر إلا اذا وصلنا هذا الطرف A2 بالأرضى، وحتى يتحقق ذلك فاننا نقوم بوضع مقاومة طرفها الاول مشترك على طرف A2 مع المقاومة الضوئية، والطرف الآخر موصل بالأرضى، ونجعل قيمة المقاومة كبيرة ولتكن 15 كيلو أوم حتى نجبر الإشارة الخارجة من المقاومة الضوئية ان تمر إلى طرف الأردوينو A2 لأنه أقل مقاومة من المقاومة التي قمنا باضافتها (وهي المقاومة 15 كيلو) وهو ما يعرف علمياً باسم pull down resistor وحتى نفهم هذا الاصطراح انظر معي الى الصورة التالية:

do.php


نلاحظ من الشكل ان المفتاح هو عبارة عن مصدر دخل اشارة إذا تم وصله تمر الإشارة إلى طرف الدائرة وإذا لم عاد الى حالة الفصل فسيكون طرف الدائرة قيمته صفر (low) لانه موصل بالارضي فقط.
هذا ببساطة هو عمل الدارة ومع بساطتها سنجد اننا سنحتاجها في الكثير من التطبيقات
اما عناصر الدارة الدارة فهي كالاتي :

do.php


do.php


وهذه صورة الدارة على لوحة الاختبار

do.php


كود البرمجة:

PHP:
#define speaker 9 //speaker connected to digital pin 9
#define photoresistor A0 //photoresistor connected to analog pin A0
long val = 0; //to store value from photoresistor
long maxread = 0; //maximum value from calibration phase
long minread = 1000; //minimum value from calibration phase
double f = 0; //frequency of tone
double normf = 0; //normalized frequency
double log_f = 0; //logarithm of normalized frequency
int ilogf = 0; //rounded logarithm
int i = 0; //loop dummy variable
double factor = 0; //scaling factor for calibration
double shift = 0; //shift for calibration
long maxfreq = 1048; //maximum desired frequency after calibration
long minfreq = 131; //minimum desired frequency after calibration
//magic numbers that make the intervals sound pleasing
double gap = 1.148698355; //ratio of consecutive notes (pentatonic)
// it's the 5th root of 2
//double gap = 1.059463094; //ratio of consecutive notes (chromatic)
// its the 12th root of 2
void setup()
{
pinMode(speaker, OUTPUT); // sets the digital pin as output
// calibration loop to determine a rasonable range of light levels (minread to maxread)
// and map that to frequencies between minfreq and maxfreq
for (i = 0; i < 500; i++) { // calibration loop runs for 5 seconds val = analogRead(photoresistor); tone(speaker, val); // play raw tone to guide calibration if (val > maxread)
{
maxread = val; //store the largest value
}
if (val < minread)
{
minread = val; //store the smallest value
}
delay(10); // reasonable delay
}
//Now we use the calibration to calculate scale and shift parameters
factor = (double)(maxfreq - minfreq) / (double)(maxread - minread); // scale parameter
//it's like a slope
shift = factor * minread - minfreq; //shift parameter: it's like an offset
}
void loop()
{
val = analogRead(photoresistor); // read photocell
f = factor * val - shift; // this linearly maps the frequency to
// a value between minfreq and maxfreq
// according to the calibration result
normf = f / (double) minfreq; // Dividing an exponential function by the min value
log_f = log(normf) / log(gap); // allows us to take the log (base gap) and the result
ilogf = round(log_f); // is the number of notes above the lowest, once we round it.
f = minfreq * pow(gap, ilogf); // we better "unlog" it.
tone(speaker, f); // this produces the tone signal
}
 
التعديل الأخير بواسطة المشرف:
أعلى