LED چشمک زن با S4A

سلام در این جلسه قصد داریم با استفاده از نرم افزار S4A به صورت بلوکی روی آردوینو برنامه نویسی کنیم و پروژه LED چشمک زن ساده رو اجرا کنیم .
برای این کار اول از همه باید نرم افزار S4A رو از قسمت نرم افزار های سایت دانلود کنید و بعد کد زیر رو با استفاده از نرم افزار Arduino روی برد آپلود کنید بعد از آپلود دیگ کاری با نرم افزار Arduino نداریم و به سراغ نرم افزار S4A میریم و شروع به کد نویسی بلوکی میکنیم.
قطعات مورد نیاز:
۱- برد ArduinoUNO
۲- کابل دیتا برد Arduino
۳- یک عدد LED
توجه داشته باشین اگه برد آردوینو مخصوص نرم افزار S4A رو از فروشگاه سایت خریداری کردید به صورت پیشفرض کد روش آپلود شده و نیازی به آپلود کردن و استفاده از نرم افزار Arduino نیست و به صورت مستقیم میشه با S4A روی برد کد نویسی کرد.
typedef enum { input, servomotor, pwm, digital } pinType; typedef struct pin { pinType type; //Type of pin int state; //State of an output //byte value; //Value of an input. Not used by now. TODO }; pin arduinoPins[14]; //Array of struct holding 0-13 pins information unsigned long lastDataReceivedTime = millis(); void setup() { Serial.begin(38400); Serial.flush(); configurePins(); resetPins(); } void loop() { static unsigned long timerCheckUpdate = millis(); if (millis()-timerCheckUpdate>=20) { sendUpdateServomotors(); sendSensorValues(); timerCheckUpdate=millis(); } readSerialPort(); } void configurePins() { arduinoPins[0].type=input; arduinoPins[1].type=input; arduinoPins[2].type=input; arduinoPins[3].type=input; arduinoPins[4].type=servomotor; arduinoPins[5].type=pwm; arduinoPins[6].type=pwm; arduinoPins[7].type=servomotor; arduinoPins[8].type=servomotor; arduinoPins[9].type=pwm; arduinoPins[10].type=digital; arduinoPins[11].type=digital; arduinoPins[12].type=digital; arduinoPins[13].type=digital; } void resetPins() { for (byte index=0; index <=13; index++) { if (arduinoPins[index].type!=input) { pinMode(index, OUTPUT); if (arduinoPins[index].type==servomotor) { arduinoPins[index].state = 255; servo (index, 255); } else { arduinoPins[index].state=0; digitalWrite(index,LOW); } } } } void sendSensorValues() { unsigned int sensorValues[6], readings[5]; byte sensorIndex; for (sensorIndex = 0; sensorIndex < 6; sensorIndex++) //for analog sensors, calculate the median of 5 sensor readings in order to avoid variability and power surges { for (byte p = 0; p < 5; p++) readings[p] = analogRead(sensorIndex); insertionSort(readings, 5); //sort readings sensorValues[sensorIndex] = readings[2]; //select median reading } //send analog sensor values for (sensorIndex = 0; sensorIndex < 6; sensorIndex++) ScratchBoardSensorReport(sensorIndex, sensorValues[sensorIndex]); //send digital sensor values ScratchBoardSensorReport(6, digitalRead(2)?1023:0); ScratchBoardSensorReport(7, digitalRead(3)?1023:0); } void insertionSort(unsigned int* array, unsigned int n) { for (int i = 1; i < n; i++) for (int j = i; (j > 0) && ( array[j] < array[j-1] ); j--) swap( array, j, j-1 ); } void swap(unsigned int* array, unsigned int a, unsigned int b) { unsigned int temp = array[a]; array[a] = array[b]; array[b] = temp; } void ScratchBoardSensorReport(byte sensor, int value) //PicoBoard protocol, 2 bytes per sensor { Serial.write( B10000000 | ((sensor & B1111)<<3) | ((value>>7) & B111)); Serial.write( value & B1111111); } void readSerialPort() { byte pin; int newVal; static byte actuatorHighByte, actuatorLowByte; static byte readingSM = 0; if (Serial.available()) { if (readingSM == 0) { actuatorHighByte = Serial.read(); if (actuatorHighByte >= 128) readingSM = 1; } else if (readingSM == 1) { actuatorLowByte = Serial.read(); if (actuatorLowByte < 128) readingSM = 2; else readingSM = 0; } if (readingSM == 2) { lastDataReceivedTime = millis(); pin = ((actuatorHighByte >> 3) & 0x0F); newVal = ((actuatorHighByte & 0x07) << 7) | (actuatorLowByte & 0x7F); if(arduinoPins[pin].state != newVal) { arduinoPins[pin].state = newVal; updateActuator(pin); } readingSM = 0; } } else checkScratchDisconnection(); } void reset() //with xbee module, we need to simulate the setup execution that occurs when a usb connection is opened or closed without this module { resetPins(); // reset pins sendSensorValues(); // protocol handshaking lastDataReceivedTime = millis(); } void updateActuator(byte pinNumber) { if (arduinoPins[pinNumber].type==digital) digitalWrite(pinNumber, arduinoPins[pinNumber].state); else if (arduinoPins[pinNumber].type==pwm) analogWrite(pinNumber, arduinoPins[pinNumber].state); } void sendUpdateServomotors() { for (byte p = 0; p < 10; p++) if (arduinoPins[p].type == servomotor) servo(p, arduinoPins[p].state); } void servo (byte pinNumber, byte angle) { if (angle != 255) pulse(pinNumber, (angle * 10) + 600); } void pulse (byte pinNumber, unsigned int pulseWidth) { digitalWrite(pinNumber, HIGH); delayMicroseconds(pulseWidth); digitalWrite(pinNumber, LOW); } void checkScratchDisconnection() //the reset is necessary when using an wireless arduino board (because we need to ensure that arduino isn't waiting the actuators state from Scratch) or when scratch isn't sending information (because is how serial port close is detected) { if (millis() - lastDataReceivedTime > 1000) reset(); //reset state if actuators reception timeout = one second }
حالا که کد رو آپلود کردیم به سراغ نرم افزار S4A میریم و شروع به کد نویسی میکنیم.
شماتیک و اتصالات:
پایه بلند تر LED = پایه ۱۳ برد Arduino uno
پایه کوتاه تر LED= پایه GND برد Arduino uno
کد نویسی بلوکی در نرم افزار S4A: (LED چشمک زن با S4A)
از قسمت کنترل دستور وقتی پرچم کلیک شد رو برمیداریم و به صفحه اصلی اضافه میکنیم.
از قسمت کنترل دستور تکرار رو برداشته و زیر دستور کلیک پرچم میزاریم.
از قسمت Motion دستور روشن شدن پایه ۱۳ رو برداشته و داخل دستور تکرار میزاریم.
از قسمت کنترل دستور تاخیر ۱ ثانیه رو برداشته و در دستور تکرار زیر دستور روشن شدن پایه ۱۳ قرار میدیم.
به قسمت Motion رفته و دستور خاموش شدن پایه ۱۳ رو برداشته و در دستور تکرار زیر دستور تاخیر قرار میدیم.
از قسمت کنترل دستور تاخیر رو برداشته و زیر دستور خاموش شدن پایه ۱۳ میزاریم.
تموم شد حالا با کلیک روی پرچم سبز رنگ LED که به پایه ۱۳ وصل کردیم روشن و خاموش میشه.