Blockly Arduino Control Interface
Project Overview
This project implements a visual programming interface using Blockly to create Arduino control programs. The application allows users to create programs for LED control and button interactions using a drag-and-drop interface, which then generates the corresponding Python code.
Features
Custom Blocks
LED Control Block
- Allows toggling LED state on specified pins
- Parameters: PIN number and STATE (on/off)
- Generates corresponding Python code for digital pin control
Delay Block
- Implements timing delays in the program
- Parameter: Duration in seconds
- Generates Python time.sleep() commands
Button Control Block
- Creates button-LED interaction logic
- Parameters: LED_PIN and BUTTON_PIN
- Generates if-else logic for button state checking
Technical Implementation
- Built using SvelteKit for the frontend framework
- Integrates Blockly for visual programming
- Generates Python code compatible with Arduino control
- Implements workspace persistence using local storage
- Features a split-pane interface with live code preview
Setup Instructions
Prerequisites
- Node.js (v14 or higher)
- npm (v6 or higher)
Installation Steps
Clone the repository:
git clone [repository-url]
cd blockly-arduino-control
Install dependencies:
npm install
Start the development server:
npm start
Access the application at http://localhost:3000
Project Structure
src/
├── App.svelte # Main application component
├── main.js # Application entry point
├── components/
│ └── BlocklyComponent.svelte # Blockly workspace component
├── blocks/
│ └── text.js # Custom block definitions
├── generators/
│ └── javascript.js # Code generation logic
├── serialization.js # Workspace state management
└── toolbox.js # Toolbox configuration
Sample Program
Here's an example program that demonstrates button-controlled LED functionality:
- Place a "button_control" block in the workspace
- Set the LED pin to 13 and button pin to 2
- The generated code will look like this:
if board.digital[2].read() == 1: # Read button state
board.digital[13].write(1) # Turn LED on
else:
board.digital[13].write(0) # Turn LED off
Technical Details
Block Definitions
Custom blocks are defined in text.js
using Blockly's block definition format. Each block includes:
- Type identifier
- Input fields configuration
- Visual styling
- Connection types
- Tooltip information
Code Generation
The application uses Python code generation to create Arduino-compatible code:
- LED commands translate to digital pin operations
- Timing uses Python's time.sleep()
- Button logic generates conditional statements
Workspace Features
- Automatic code generation on block changes
- Workspace state persistence between sessions
- Real-time code preview
- Error handling and validation
Future Enhancements
Additional Hardware Support
- Support for analog inputs/outputs
- PWM control for LED brightness
- Multiple button combinations
Interface Improvements
- Custom block categories
- Code validation
- Download generated code
- Circuit visualization
Advanced Features
- Serial communication blocks
- Custom function definitions
- Variable management
- Loop constructs
Known Limitations
- Currently generates Python code only
- Limited to digital input/output operations
- No direct Arduino upload capability
- Basic error handling
Troubleshooting
Blank Screen
- Clear browser cache
- Verify all dependencies are installed
- Check browser console for errors
Code Generation Issues
- Verify block connections
- Check pin number validity
- Ensure proper block sequence
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request with detailed description
License
This project is licensed under the MIT License.