instruction
stringlengths 40
100
| input
stringlengths 136
25.5k
| output
stringlengths 207
10.3k
|
---|---|---|
Generate a diagram.json code for Simon says game | #include "pitches.h"
/* Constants - define pin numbers for LEDs,
buttons and speaker, and also the game tones: */
const uint8_t ledPins[] = {9, 10, 11, 12};
const uint8_t buttonPins[] = {2, 3, 4, 5};
#define SPEAKER_PIN 8
// These are connected to 74HC595 shift register (used to show game score):
const int LATCH_PIN = A1; // 74HC595 pin 12
const int DATA_PIN = A0; // 74HC595pin 14
const int CLOCK_PIN = A2; // 74HC595 pin 11
#define MAX_GAME_LENGTH 100
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
/* Global variables - store the game state */
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
/**
Set up the Arduino board and initialize Serial communication
*/
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
// The following line primes the random number generator.
// It assumes pin A3 is floating (disconnected):
randomSeed(analogRead(A3));
}
/* Digit table for the 7-segment display */
const uint8_t digitTable[] = {
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10010000,
};
const uint8_t DASH = 0b10111111;
void sendScore(uint8_t high, uint8_t low) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, low);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, high);
digitalWrite(LATCH_PIN, HIGH);
}
void displayScore() {
int high = gameIndex % 100 / 10;
int low = gameIndex % 10;
sendScore(high ? digitTable[high] : 0xff, digitTable[low]);
}
/**
Lights the given LED and plays a suitable tone
*/
void lightLedAndPlayTone(byte ledIndex) {
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(300);
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
}
/**
Plays the current sequence of notes that the user has to repeat
*/
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlayTone(currentLed);
delay(50);
}
}
/**
Waits until the user pressed one of the buttons,
and returns the index of that button
*/
byte readButtons() {
while (true) {
for (byte i = 0; i < 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
delay(1);
}
}
/**
Play the game over sequence, and report the game score
*/
void gameOver() {
Serial.print("Game over! your score: ");
Serial.println(gameIndex - 1);
gameIndex = 0;
delay(200);
// Play a Wah-Wah-Wah-Wah sound
tone(SPEAKER_PIN, NOTE_DS5);
delay(300);
tone(SPEAKER_PIN, NOTE_D5);
delay(300);
tone(SPEAKER_PIN, NOTE_CS5);
delay(300);
for (byte i = 0; i < 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) {
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(5);
}
}
noTone(SPEAKER_PIN);
sendScore(DASH, DASH);
delay(500);
}
/**
Get the user's input and compare it with the expected sequence.
*/
bool checkUserSequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gameSequence[i];
byte actualButton = readButtons();
lightLedAndPlayTone(actualButton);
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
/**
Plays a hooray sound whenever the user finishes a level
*/
void playLevelUpSound() {
tone(SPEAKER_PIN, NOTE_E4);
delay(150);
tone(SPEAKER_PIN, NOTE_G4);
delay(150);
tone(SPEAKER_PIN, NOTE_E5);
delay(150);
tone(SPEAKER_PIN, NOTE_C5);
delay(150);
tone(SPEAKER_PIN, NOTE_D5);
delay(150);
tone(SPEAKER_PIN, NOTE_G5);
delay(150);
noTone(SPEAKER_PIN);
}
/**
The main game loop
*/
void loop() {
displayScore();
// Add a random color to the end of the sequence
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH - 1;
}
playSequence();
if (!checkUserSequence()) {
gameOver();
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
delay(300);
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 183, "left": 18.6, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "buzzer",
"top": 16,
"left": 124,
"attrs": { "volume": "0.1" }
},
{ "type": "wokwi-led", "id": "led-red", "top": 10, "left": 6, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led-green",
"top": 73,
"left": 6,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led-blue",
"top": 10,
"left": 270,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led-yellow",
"top": 73,
"left": 270,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-red",
"top": 10,
"left": 46,
"attrs": { "color": "red", "key": "1", "label": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-green",
"top": 76,
"left": 46,
"attrs": { "color": "green", "key": "2", "label": "2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-blue",
"top": 10,
"left": 200,
"attrs": { "color": "blue", "key": "3", "label": "3" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-yellow",
"top": 76,
"left": 200,
"attrs": { "color": "yellow", "key": "4", "label": "4" }
},
{
"type": "wokwi-74hc595",
"id": "sr1",
"top": 171.8,
"left": 361.16,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-74hc595",
"id": "sr2",
"top": 171.8,
"left": 457.16,
"rotate": 180,
"attrs": {}
},
{ "type": "wokwi-7segment", "id": "sevseg1", "top": 47.16, "left": 379.48, "attrs": {} },
{ "type": "wokwi-7segment", "id": "sevseg2", "top": 47.16, "left": 446.68, "attrs": {} }
],
"connections": [
[ "uno:GND.1", "buzzer:1", "black", [ "v-12", "*", "h0" ] ],
[ "uno:2", "btn-yellow:1.l", "gold", [ "v-48", "*", "h-6" ] ],
[ "uno:GND.1", "btn-yellow:2.r", "black", [ "v-12", "*", "h6" ] ],
[ "uno:3", "btn-blue:1.l", "blue", [ "v-44", "*", "h-10" ] ],
[ "uno:GND.1", "btn-blue:2.r", "black", [ "v-12", "*", "h6" ] ],
[ "uno:4", "btn-green:2.r", "green", [ "v-40", "*", "h6" ] ],
[ "uno:GND.1", "btn-green:1.l", "black", [ "v-12", "*", "h-6" ] ],
[ "uno:5", "btn-red:2.r", "orange", [ "v-36", "*", "h10" ] ],
[ "uno:GND.1", "btn-red:1.l", "black", [ "v-12", "*", "h-6" ] ],
[ "uno:8", "buzzer:2", "purple", [ "v-32", "*", "h0" ] ],
[ "uno:9", "led-yellow:A", "gold", [ "v-28", "*", "h0" ] ],
[ "uno:GND.1", "led-yellow:C", "black", [ "v-12", "*", "h-15", "v4" ] ],
[ "uno:10", "led-blue:A", "blue", [ "v-24", "*", "h8" ] ],
[ "uno:GND.1", "led-blue:C", "black", [ "v-12", "*", "h-15", "v4" ] ],
[ "uno:11", "led-green:A", "green", [ "v-20", "*", "h0" ] ],
[ "uno:GND.1", "led-green:C", "black", [ "v-12", "*", "h-8", "v4" ] ],
[ "uno:12", "led-red:A", "orange", [ "v-16", "*", "h6" ] ],
[ "uno:GND.1", "led-red:C", "black", [ "v-12", "*", "h-8", "v4" ] ],
[ "uno:5V", "sr1:VCC", "red", [ "v57.5", "h253.4" ] ],
[ "uno:A2", "sr1:SHCP", "gray", [ "v19.1", "h138.4" ] ],
[ "uno:A1", "sr1:STCP", "purple", [ "v28.7", "h157.5" ] ],
[ "uno:A0", "sr1:DS", "blue", [ "v38.3", "h186.2" ] ],
[ "sr1:SHCP", "sr2:SHCP", "gray", [ "v47", "h106.12" ] ],
[ "sr1:STCP", "sr2:STCP", "purple", [ "v37.4", "h96.52" ] ],
[ "sr1:Q7S", "sr2:DS", "blue", [ "h0.52", "v56.6", "h144" ] ],
[ "sr1:VCC", "sr1:MR", "red", [ "v17", "h-57.6" ] ],
[ "sr1:VCC", "sr2:MR", "red", [ "v17", "h38.4" ] ],
[ "sr1:VCC", "sr2:VCC", "red", [ "v17", "h96" ] ],
[ "sr1:OE", "sr2:OE", "black", [ "v26.6", "h96" ] ],
[ "sr1:MR", "sevseg1:COM.1", "red", [ "v17", "h-57.6", "v-96", "h76.8" ] ],
[ "sevseg1:COM.1", "sevseg2:COM.1", "red", [ "h0", "v9.6", "h57.6" ] ],
[ "sr2:Q0", "sevseg2:A", "green", [ "v7.4", "h28.8", "v-182.4", "h-67.2" ] ],
[ "sr2:Q1", "sevseg2:B", "green", [ "v0", "h9.6", "v-134.4", "h-48" ] ],
[ "sr2:Q2", "sevseg2:C", "green", [ "v-38.4", "h-38.4" ] ],
[ "sr2:Q3", "sevseg2:D", "green", [ "v-33.6", "h-33.6", "v-9.6", "h-14.4" ] ],
[ "sr2:Q4", "sevseg2:E", "green", [ "v-28.8", "h-28.8", "v-9.6", "h-14.4" ] ],
[ "sr2:Q5", "sevseg2:F", "green", [ "v-24", "h-24", "v-9.6", "h-24", "v-110.4", "h19.2" ] ],
[ "sr2:Q6", "sevseg2:G", "green", [ "v-19.2", "h-43.2", "v-115.2", "h14.4" ] ],
[ "sr1:GND", "sr2:GND", "black", [ "v-9.6", "h96" ] ],
[ "sr1:Q1", "sevseg1:B", "green", [ "v-134.4", "h-19.2" ] ],
[ "sr1:Q2", "sevseg1:C", "green", [ "v-38.4", "h-19.2" ] ],
[ "sr1:Q3", "sevseg1:D", "green", [ "v-33.6", "h-24" ] ],
[ "sr1:Q4", "sevseg1:E", "green", [ "v-28.8", "h-28.8" ] ],
[ "uno:GND.3", "sr1:GND", "black", [ "v47.9", "h157.6", "v-259.2", "h9.6" ] ],
[ "sr1:GND", "sr1:OE", "black", [ "v-9.6", "h-9.6", "v67.2", "h172.8" ] ],
[ "sr1:Q0", "sevseg1:A", "green", [ "v65", "h-76.8", "v-240", "h57.6" ] ],
[ "sr1:Q5", "sevseg1:F", "green", [ "v-24", "h-19.2", "v-110.4", "h19.2" ] ],
[ "sr1:Q6", "sevseg1:G", "green", [ "v-19.2", "h-14.4", "v-110.4", "h14.4" ] ]
]
} |
Generate a diagram.json code for servos dancing | #include <Servo.h>
#define NUM_SERVOS 32
Servo myServo[NUM_SERVOS];
void setup()
{
// Attach pins from the Arduino Mega board to the Servo objects.
// Starting from pin 22, there happen to be exactly 32 pins on the double row pins.
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].attach( i + 22); // pin 22 up to 53 is 32 pins
}
}
void loop()
{
// Sequence one.
// All servo motor are set to a random angle.
for( int a=0; a<15; a++)
{
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].write( random( 0, 181));
delay( 2);
}
delay( 150);
}
// Sequence two.
// All servo motors move with the same angle.
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].write( 0); // set to begin position (horn is rotated left)
}
delay( 1000); // wait to let the viewer get used to it
for( int a=0; a<3; a++)
{
for( int r=0; r<=180; r++) // move horns to the right
{
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].write( r);
}
delay( 6);
}
for( int r=180; r>=0; r--)
{
for( int i=0; i<NUM_SERVOS; i++) // move horns to the left
{
myServo[i].write( r);
}
delay( 6);
}
}
// Sequence three.
// A rotating wave.
for( int a=0; a<6; a++)
{
for( int i=0; i<NUM_SERVOS; i++)
{
for( int j=0; j<NUM_SERVOS; j++)
{
// Calculate distance to active servo
int d = j - i;
if( d < 0)
d = -d;
if( d > (NUM_SERVOS / 2))
d = NUM_SERVOS - d;
int angle = 90 - (10 * d);
if( angle < 0)
angle = 0;
myServo[j].write( angle);
}
delay(40);
}
}
// Sequence four.
// A "compass"
// Start by pointing upwards
int pointer = NUM_SERVOS * 3 / 4;
showPointer( pointer);
delay( 1000); // let the viewer get used to new pattern
for( int i=0; i<5; i++)
{
showPointer( --pointer);
delay( 150);
}
delay( 200);
for( int i=0; i<9; i++)
{
showPointer( ++pointer);
delay( 150);
}
delay( 200);
for( int i=0; i<5; i++)
{
showPointer( --pointer);
delay( 150);
}
delay( 200);
for( int i=0; i<4; i++)
{
showPointer( ++pointer);
delay( 150);
}
delay( 160);
for( int i=0; i<2; i++)
{
showPointer( --pointer);
delay( 150);
}
delay( 80);
for( int i=0; i<1; i++)
{
showPointer( ++pointer);
delay( 150);
}
delay( 2000);
}
// This function makes a "pointer" with the servos.
// It is used to create the "compass".
// The parameter 's' is the servo motor that has the pointer.
// It is allowed that 's' is below zero or larger than the numbers of servo motors.
void showPointer( int s)
{
int pointerA = s % NUM_SERVOS; // Using the '%' (remainder) for valid number
int pointerB = (s + 1) % NUM_SERVOS; // pointer is made with the next servo motor
int tailA = (s + 16) % NUM_SERVOS;
int tailB = (s + 17) % NUM_SERVOS;
// make pointer with servo motor s and s+1.
myServo[pointerA].write(180-56);
myServo[pointerB].write(56);
// make tail with servo motor s+16 and s+17.
myServo[tailA].write(95);
myServo[tailB].write(85);
// Set servos right of pointer
int n = (NUM_SERVOS / 2) - 2;
int start = pointerB + 1;
for( int i=0; i<n; i++)
{
int j = (start + i) % NUM_SERVOS;
myServo[j].write( 2);
}
// Set servos left of pointer
start = tailB + 1;
for( int i=0; i<n; i++)
{
int j = (start + i) % NUM_SERVOS;
myServo[j].write( 178);
}
}
// The function GenerateDiagram() can be used to generate
// the diagram.json file for Wokwi.
// To use it, call it from the setup() function, and the
// serial output can be copied into the diagram.json file.
void GenerateDiagram()
{
Serial.begin(115200);
Serial.print( "{\n");
Serial.print( " \"version\": 1,\n");
Serial.print( " \"author\": \"Generated\",\n");
Serial.print( " \"editor\": \"wokwi\",\n");
Serial.print( " \"parts\": [\n");
Serial.print( " {\n");
Serial.print( " \"type\": \"wokwi-arduino-mega\",\n");
Serial.print( " \"id\": \"mega\",\n");
Serial.print( " \"top\": 270,\n");
Serial.print( " \"left\": 185,\n");
Serial.print( " \"attrs\": {}\n");
Serial.print( " },\n");
// Put the servo motor in reverse order in the diagram.json
// I think that is visually better.
// The horn now overlaps the next servo when the horn moves to the right.
for( int i=NUM_SERVOS-1; i>=0; i--)
{
float rotate = float( i) * (360.0 / float( NUM_SERVOS));
float rad = rotate / 360.0 * 2.0 * M_PI;
float top = (300.0 * sin( rad)) + 300.0;
float left = (300.0 * cos( rad)) + 300.0;
Serial.print( " {\n");
Serial.print( " \"type\": \"wokwi-servo\",\n");
Serial.print( " \"id\": \"servo");
Serial.print( i);
Serial.print( "\",\n");
Serial.print( " \"top\": ");
Serial.print( top);
Serial.print( ",\n");
Serial.print( " \"left\": ");
Serial.print( left);
Serial.print( ",\n");
Serial.print( " \"rotate\": ");
Serial.print( rotate);
Serial.print( ",\n");
Serial.print( " \"attrs\": { \"hornColor\": \"Red\" }\n");
Serial.print( " }");
if( i != 0)
Serial.print( ",");
Serial.print( "\n");
}
Serial.print( " ],\n");
Serial.print( " \"connections\": [\n");
for( int i=0; i<NUM_SERVOS; i++)
{
int j = i + 1;
if( j == NUM_SERVOS)
j = 0;
Serial.print( " [ \"servo");
Serial.print( i);
Serial.print( ":V+\", \"servo");
Serial.print( j);
Serial.print( ":V+\", \"Red\", [] ],\n");
Serial.print( " [ \"servo");
Serial.print( i);
Serial.print( ":GND\", \"servo");
Serial.print( j);
Serial.print( ":GND\", \"Black\", [] ],\n");
Serial.print( " [ \"mega:");
Serial.print( i + 22);
Serial.print( "\", \"servo");
Serial.print( i);
Serial.print( ":PWM\", \"Green\", [ ] ],\n");
}
Serial.print( " [ \"mega:GND.2\", \"servo9:GND\", \"Black\", [ ] ],\n");
Serial.print( " [ \"mega:5V\", \"servo9:V+\", \"Red\", [ ] ]\n");
Serial.print( " ]\n");
Serial.print( "}\n");
} | {
"version": 1,
"author": "Generated",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-mega",
"id": "mega",
"top": 270,
"left": 185,
"attrs": {}
},
{
"type": "wokwi-servo",
"id": "servo31",
"top": 241.47,
"left": 594.24,
"rotate": 348.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo30",
"top": 185.19,
"left": 577.16,
"rotate": 337.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo29",
"top": 133.33,
"left": 549.44,
"rotate": 326.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo28",
"top": 87.87,
"left": 512.13,
"rotate": 315.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo27",
"top": 50.56,
"left": 466.67,
"rotate": 303.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo26",
"top": 22.84,
"left": 414.81,
"rotate": 292.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo25",
"top": 5.76,
"left": 358.53,
"rotate": 281.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo24",
"top": 0.00,
"left": 300.00,
"rotate": 270.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo23",
"top": 5.76,
"left": 241.47,
"rotate": 258.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo22",
"top": 22.84,
"left": 185.20,
"rotate": 247.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo21",
"top": 50.56,
"left": 133.33,
"rotate": 236.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo20",
"top": 87.87,
"left": 87.87,
"rotate": 225.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo19",
"top": 133.33,
"left": 50.56,
"rotate": 213.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo18",
"top": 185.19,
"left": 22.84,
"rotate": 202.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo17",
"top": 241.47,
"left": 5.76,
"rotate": 191.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo16",
"top": 300.00,
"left": 0.00,
"rotate": 180.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo15",
"top": 358.53,
"left": 5.76,
"rotate": 168.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo14",
"top": 414.80,
"left": 22.84,
"rotate": 157.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo13",
"top": 466.67,
"left": 50.56,
"rotate": 146.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo12",
"top": 512.13,
"left": 87.87,
"rotate": 135.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo11",
"top": 549.44,
"left": 133.33,
"rotate": 123.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo10",
"top": 577.16,
"left": 185.19,
"rotate": 112.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo9",
"top": 594.24,
"left": 241.47,
"rotate": 101.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo8",
"top": 600.00,
"left": 300.00,
"rotate": 90.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo7",
"top": 594.24,
"left": 358.53,
"rotate": 78.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo6",
"top": 577.16,
"left": 414.81,
"rotate": 67.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo5",
"top": 549.44,
"left": 466.67,
"rotate": 56.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo4",
"top": 512.13,
"left": 512.13,
"rotate": 45.00,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo3",
"top": 466.67,
"left": 549.44,
"rotate": 33.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo2",
"top": 414.81,
"left": 577.16,
"rotate": 22.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo1",
"top": 358.53,
"left": 594.24,
"rotate": 11.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo0",
"top": 300.00,
"left": 600.00,
"rotate": 0.00,
"attrs": { "hornColor": "Red" }
}
],
"connections": [
[ "servo0:V+", "servo1:V+", "Red", [] ],
[ "servo0:GND", "servo1:GND", "Black", [] ],
[ "mega:22", "servo0:PWM", "Green", [ ] ],
[ "servo1:V+", "servo2:V+", "Red", [] ],
[ "servo1:GND", "servo2:GND", "Black", [] ],
[ "mega:23", "servo1:PWM", "Green", [ ] ],
[ "servo2:V+", "servo3:V+", "Red", [] ],
[ "servo2:GND", "servo3:GND", "Black", [] ],
[ "mega:24", "servo2:PWM", "Green", [ ] ],
[ "servo3:V+", "servo4:V+", "Red", [] ],
[ "servo3:GND", "servo4:GND", "Black", [] ],
[ "mega:25", "servo3:PWM", "Green", [ ] ],
[ "servo4:V+", "servo5:V+", "Red", [] ],
[ "servo4:GND", "servo5:GND", "Black", [] ],
[ "mega:26", "servo4:PWM", "Green", [ ] ],
[ "servo5:V+", "servo6:V+", "Red", [] ],
[ "servo5:GND", "servo6:GND", "Black", [] ],
[ "mega:27", "servo5:PWM", "Green", [ ] ],
[ "servo6:V+", "servo7:V+", "Red", [] ],
[ "servo6:GND", "servo7:GND", "Black", [] ],
[ "mega:28", "servo6:PWM", "Green", [ ] ],
[ "servo7:V+", "servo8:V+", "Red", [] ],
[ "servo7:GND", "servo8:GND", "Black", [] ],
[ "mega:29", "servo7:PWM", "Green", [ ] ],
[ "servo8:V+", "servo9:V+", "Red", [] ],
[ "servo8:GND", "servo9:GND", "Black", [] ],
[ "mega:30", "servo8:PWM", "Green", [ ] ],
[ "servo9:V+", "servo10:V+", "Red", [] ],
[ "servo9:GND", "servo10:GND", "Black", [] ],
[ "mega:31", "servo9:PWM", "Green", [ ] ],
[ "servo10:V+", "servo11:V+", "Red", [] ],
[ "servo10:GND", "servo11:GND", "Black", [] ],
[ "mega:32", "servo10:PWM", "Green", [ ] ],
[ "servo11:V+", "servo12:V+", "Red", [] ],
[ "servo11:GND", "servo12:GND", "Black", [] ],
[ "mega:33", "servo11:PWM", "Green", [ ] ],
[ "servo12:V+", "servo13:V+", "Red", [] ],
[ "servo12:GND", "servo13:GND", "Black", [] ],
[ "mega:34", "servo12:PWM", "Green", [ ] ],
[ "servo13:V+", "servo14:V+", "Red", [] ],
[ "servo13:GND", "servo14:GND", "Black", [] ],
[ "mega:35", "servo13:PWM", "Green", [ ] ],
[ "servo14:V+", "servo15:V+", "Red", [] ],
[ "servo14:GND", "servo15:GND", "Black", [] ],
[ "mega:36", "servo14:PWM", "Green", [ ] ],
[ "servo15:V+", "servo16:V+", "Red", [] ],
[ "servo15:GND", "servo16:GND", "Black", [] ],
[ "mega:37", "servo15:PWM", "Green", [ ] ],
[ "servo16:V+", "servo17:V+", "Red", [] ],
[ "servo16:GND", "servo17:GND", "Black", [] ],
[ "mega:38", "servo16:PWM", "Green", [ ] ],
[ "servo17:V+", "servo18:V+", "Red", [] ],
[ "servo17:GND", "servo18:GND", "Black", [] ],
[ "mega:39", "servo17:PWM", "Green", [ ] ],
[ "servo18:V+", "servo19:V+", "Red", [] ],
[ "servo18:GND", "servo19:GND", "Black", [] ],
[ "mega:40", "servo18:PWM", "Green", [ ] ],
[ "servo19:V+", "servo20:V+", "Red", [] ],
[ "servo19:GND", "servo20:GND", "Black", [] ],
[ "mega:41", "servo19:PWM", "Green", [ ] ],
[ "servo20:V+", "servo21:V+", "Red", [] ],
[ "servo20:GND", "servo21:GND", "Black", [] ],
[ "mega:42", "servo20:PWM", "Green", [ ] ],
[ "servo21:V+", "servo22:V+", "Red", [] ],
[ "servo21:GND", "servo22:GND", "Black", [] ],
[ "mega:43", "servo21:PWM", "Green", [ ] ],
[ "servo22:V+", "servo23:V+", "Red", [] ],
[ "servo22:GND", "servo23:GND", "Black", [] ],
[ "mega:44", "servo22:PWM", "Green", [ ] ],
[ "servo23:V+", "servo24:V+", "Red", [] ],
[ "servo23:GND", "servo24:GND", "Black", [] ],
[ "mega:45", "servo23:PWM", "Green", [ ] ],
[ "servo24:V+", "servo25:V+", "Red", [] ],
[ "servo24:GND", "servo25:GND", "Black", [] ],
[ "mega:46", "servo24:PWM", "Green", [ ] ],
[ "servo25:V+", "servo26:V+", "Red", [] ],
[ "servo25:GND", "servo26:GND", "Black", [] ],
[ "mega:47", "servo25:PWM", "Green", [ ] ],
[ "servo26:V+", "servo27:V+", "Red", [] ],
[ "servo26:GND", "servo27:GND", "Black", [] ],
[ "mega:48", "servo26:PWM", "Green", [ ] ],
[ "servo27:V+", "servo28:V+", "Red", [] ],
[ "servo27:GND", "servo28:GND", "Black", [] ],
[ "mega:49", "servo27:PWM", "Green", [ ] ],
[ "servo28:V+", "servo29:V+", "Red", [] ],
[ "servo28:GND", "servo29:GND", "Black", [] ],
[ "mega:50", "servo28:PWM", "Green", [ ] ],
[ "servo29:V+", "servo30:V+", "Red", [] ],
[ "servo29:GND", "servo30:GND", "Black", [] ],
[ "mega:51", "servo29:PWM", "Green", [ ] ],
[ "servo30:V+", "servo31:V+", "Red", [] ],
[ "servo30:GND", "servo31:GND", "Black", [] ],
[ "mega:52", "servo30:PWM", "Green", [ ] ],
[ "servo31:V+", "servo0:V+", "Red", [] ],
[ "servo31:GND", "servo0:GND", "Black", [] ],
[ "mega:53", "servo31:PWM", "Green", [ ] ],
[ "mega:GND.2", "servo9:GND", "Black", [ ] ],
[ "mega:5V", "servo9:V+", "Red", [ ] ]
]
} |
Generate a diagram.json code for LED matrix tunnel | // This version uses bit-banged SPI.
// If you see tearing (jagged edges on the circles) try the version
// which uses AVR's hardware SPI peripheral:
// https://wokwi.com/arduino/projects/318868939929027156
#define CLK 13
#define DIN 11
#define CS 10
#define X_SEGMENTS 4
#define Y_SEGMENTS 4
#define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
// a framebuffer to hold the state of the entire matrix of LEDs
// laid out in raster order, with (0, 0) at the top-left
byte fb[8 * NUM_SEGMENTS];
void shiftAll(byte send_to_address, byte send_this_data)
{
digitalWrite(CS, LOW);
for (int i = 0; i < NUM_SEGMENTS; i++) {
shiftOut(DIN, CLK, MSBFIRST, send_to_address);
shiftOut(DIN, CLK, MSBFIRST, send_this_data);
}
digitalWrite(CS, HIGH);
}
void setup() {
Serial.begin(115200);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
// Setup each MAX7219
shiftAll(0x0f, 0x00); //display test register - test mode off
shiftAll(0x0b, 0x07); //scan limit register - display digits 0 thru 7
shiftAll(0x0c, 0x01); //shutdown register - normal operation
shiftAll(0x0a, 0x0f); //intensity register - max brightness
shiftAll(0x09, 0x00); //decode mode register - No decode
}
void loop() {
static int16_t sx1 = 15 << 8, sx2 = sx1, sy1, sy2;
sx1 = sx1 - (sy1 >> 6);
sy1 = sy1 + (sx1 >> 6);
sx2 = sx2 - (sy2 >> 5);
sy2 = sy2 + (sx2 >> 5);
static byte travel = 0;
travel--;
byte *dst = fb;
byte output = 0;
int8_t x_offset = (sx1 >> 8) - X_SEGMENTS * 4;
int8_t y_offset = (sx2 >> 8) - Y_SEGMENTS * 4;
uint8_t screenx, screeny, xroot, yroot;
uint16_t xsumsquares, ysumsquares, xnextsquare, ynextsquare;
int8_t x, y;
// offset the origin in screen space
x = x_offset;
y = y_offset;
ysumsquares = x_offset * x_offset + y * y;
yroot = int(sqrtf(ysumsquares));
ynextsquare = yroot*yroot;
// Quadrant II (top-left)
screeny = Y_SEGMENTS * 8;
while (y < 0 && screeny) {
x = x_offset;
screenx = X_SEGMENTS * 8;
xsumsquares = ysumsquares;
xroot = yroot;
if (x < 0) {
xnextsquare = xroot * xroot;
while (x < 0 && screenx) {
screenx--;
output <<= 1;
output |= ((xroot + travel) & 8) >> 3;
if (!(screenx & 7))
*dst++ = output;
xsumsquares += 2 * x++ + 1;
if (xsumsquares < xnextsquare)
xnextsquare -= 2 * xroot-- - 1;
}
}
// Quadrant I (top right)
if (screenx) {
xnextsquare = (xroot + 1) * (xroot + 1);
while (screenx) {
screenx--;
output <<= 1;
output |= ((xroot + travel) & 8) >> 3;
if (!(screenx & 7))
*dst++ = output;
xsumsquares += 2 * x++ + 1;
if (xsumsquares >= xnextsquare)
xnextsquare += 2 * ++xroot + 1;
}
}
ysumsquares += 2 * y++ + 1;
if (ysumsquares < ynextsquare)
ynextsquare -= 2 * yroot-- - 1;
screeny--;
}
// Quadrant III (bottom left)
ynextsquare = (yroot + 1) * (yroot + 1);
while (screeny) {
x = x_offset;
screenx = X_SEGMENTS * 8;
xsumsquares = ysumsquares;
xroot = yroot;
if (x < 0) {
xnextsquare = xroot * xroot;
while (x < 0 && screenx) {
screenx--;
output <<= 1;
output |= ((xroot + travel) & 8) >> 3;
if (!(screenx & 7))
*dst++ = output;
xsumsquares += 2 * x++ + 1;
if (xsumsquares < xnextsquare)
xnextsquare -= 2 * xroot-- - 1;
}
}
// Quadrant IV (bottom right)
if (screenx) {
xnextsquare = (xroot + 1) * (xroot + 1);
while (screenx--) {
output <<= 1;
output |= ((xroot + travel) & 8) >> 3;
if (!(screenx & 7))
*dst++ = output;
xsumsquares += 2 * x++ + 1;
if (xsumsquares >= xnextsquare)
xnextsquare += 2 * ++xroot + 1;
}
}
ysumsquares += 2 * y++ + 1;
if (ysumsquares >= ynextsquare)
ynextsquare += 2 * ++yroot + 1;
screeny--;
}
show();
}
void set_pixel(uint8_t x, uint8_t y, uint8_t mode) {
byte *addr = &fb[x / 8 + y * X_SEGMENTS];
byte mask = 128 >> (x % 8);
switch (mode) {
case 0: // clear pixel
*addr &= ~mask;
break;
case 1: // plot pixel
*addr |= mask;
break;
case 2: // XOR pixel
*addr ^= mask;
break;
}
}
void safe_pixel(uint8_t x, uint8_t y, uint8_t mode) {
if ((x >= X_SEGMENTS * 8) || (y >= Y_SEGMENTS * 8))
return;
set_pixel(x, y, mode);
}
// turn off every LED in the framebuffer
void clear() {
byte *addr = fb;
for (byte i = 0; i < 8 * NUM_SEGMENTS; i++)
*addr++ = 0;
}
// send the raster order framebuffer in the correct order
// for the boustrophedon layout of daisy-chained MAX7219s
void show() {
for (byte row = 0; row < 8; row++) {
digitalWrite(CS, LOW);
byte segment = NUM_SEGMENTS;
while (segment--) {
byte x = segment % X_SEGMENTS;
byte y = segment / X_SEGMENTS * 8;
byte addr = (row + y) * X_SEGMENTS;
if (segment & X_SEGMENTS) { // odd rows of segments
shiftOut(DIN, CLK, MSBFIRST, 8 - row);
shiftOut(DIN, CLK, LSBFIRST, fb[addr + x]);
} else { // even rows of segments
shiftOut(DIN, CLK, MSBFIRST, 1 + row);
shiftOut(DIN, CLK, MSBFIRST, fb[addr - x + X_SEGMENTS - 1]);
}
}
digitalWrite(CS, HIGH);
}
} | {
"version": 1,
"author": "sutaburosu",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-mega",
"id": "mega",
"top": -177.58,
"left": 252.33,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-max7219-matrix",
"id": "matrix1",
"top": -126.7,
"left": -79.5,
"attrs": { "chain": "4", "layout": "fc16" }
},
{
"type": "wokwi-max7219-matrix",
"id": "matrix2",
"top": -52.5,
"left": -79.5,
"rotate": 180,
"attrs": { "chain": "4", "layout": "fc16" }
},
{
"type": "wokwi-max7219-matrix",
"id": "matrix3",
"top": 22.46,
"left": -78.96,
"attrs": { "chain": "4", "layout": "fc16" }
},
{
"type": "wokwi-max7219-matrix",
"id": "matrix4",
"top": 96.8,
"left": -79.5,
"rotate": 180,
"attrs": { "chain": "4", "layout": "fc16" }
}
],
"connections": [
[ "matrix1:GND.2", "matrix2:GND", "black", [ "h-47.8", "v89.07" ] ],
[ "matrix1:DOUT", "matrix2:DIN", "blue", [ "h-38.2", "v69.87" ] ],
[ "matrix1:CS.2", "matrix2:CS", "green", [ "h-28.6", "v50.67" ] ],
[ "matrix1:CLK.2", "matrix2:CLK", "orange", [ "h-19", "v31.47" ] ],
[ "matrix2:CLK.2", "matrix3:CLK", "orange", [ "h58.3", "v110.3" ] ],
[ "matrix2:CS.2", "matrix3:CS", "green", [ "h48.7", "v91.1" ] ],
[ "matrix2:DOUT", "matrix3:DIN", "blue", [ "h39.1", "v71.9" ] ],
[ "matrix2:GND.2", "matrix3:GND", "black", [ "h29.5", "v52.7" ] ],
[ "matrix3:V+", "matrix2:V+.2", "red", [ "h19.2", "v-31.46" ] ],
[ "matrix2:V+", "matrix1:V+.2", "red", [ "h-56.74", "v-110.5" ] ],
[ "matrix4:V+", "matrix3:V+.2", "red", [ "h-56.74", "v-115.8" ] ],
[ "matrix3:GND.2", "matrix4:GND", "black", [ "h-47.84", "v93.34" ] ],
[ "matrix4:DIN", "matrix3:DOUT", "blue", [ "h-37.54", "v-67.8" ] ],
[ "matrix4:CS", "matrix3:CS.2", "green", [ "h-27.94", "v-58.2" ] ],
[ "matrix4:CLK", "matrix3:CLK.2", "orange", [ "h-18.34", "v-39" ] ],
[ "mega:5V", "matrix1:V+", "red", [ "h-33.66", "v-77.39" ] ],
[ "mega:GND.2", "matrix1:GND", "black", [ "h-42.41", "v-79.48" ] ],
[ "mega:11", "matrix1:DIN", "blue", [ "h20.97", "v249.88", "h-211.2", "v-211.2" ] ],
[ "mega:10", "matrix1:CS", "green", [ "h30.57", "v249.98", "h-230.4", "v-220.8" ] ],
[ "mega:13", "matrix1:CLK", "orange", [ "h40.17", "v288.08", "h-249.6", "v-220.8" ] ]
]
} |
Generate a diagram.json code for mini piano | #include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
pitch = buttonTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 185, "left": 71, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "buzzer",
"top": 220,
"left": 380,
"rotate": 90,
"attrs": { "volume": "0.2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 70,
"left": 0,
"rotate": 90,
"attrs": { "color": "red", "key": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 70,
"left": 50,
"rotate": 90,
"attrs": { "color": "orange", "key": "2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": 70,
"left": 100,
"rotate": 90,
"attrs": { "color": "yellow", "key": "3" }
},
{
"type": "wokwi-pushbutton",
"id": "btn4",
"top": 70,
"left": 150,
"rotate": 90,
"attrs": { "color": "green", "key": "4" }
},
{
"type": "wokwi-pushbutton",
"id": "btn5",
"top": 70,
"left": 200,
"rotate": 90,
"attrs": { "color": "cyan", "key": "5" }
},
{
"type": "wokwi-pushbutton",
"id": "btn6",
"top": 70,
"left": 250,
"rotate": 90,
"attrs": { "color": "blue", "key": "6" }
},
{
"type": "wokwi-pushbutton",
"id": "btn7",
"top": 70,
"left": 300,
"rotate": 90,
"attrs": { "color": "purple", "key": "7" }
},
{
"type": "wokwi-pushbutton",
"id": "btn8",
"top": 70,
"left": 350,
"rotate": 90,
"attrs": { "color": "red", "key": "8" }
}
],
"connections": [
[ "uno:GND.1", "buzzer:1", "black", [ "v-16", "*", "h-18" ] ],
[ "uno:8", "buzzer:2", "gold", [ "v-24", "*", "h-10" ] ],
[ "uno:12", "btn1:1.r", "red", [ "v-24", "h0" ] ],
[ "uno:GND.1", "btn1:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:11", "btn2:1.r", "orange", [ "v-32", "h0" ] ],
[ "uno:GND.1", "btn2:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:10", "btn3:1.r", "yellow", [ "v-38", "h0" ] ],
[ "uno:GND.1", "btn3:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:9", "btn4:1.r", "green", [ "v-46", "h20" ] ],
[ "uno:GND.1", "btn4:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:7", "btn5:1.r", "cyan", [ "v-56", "h0" ] ],
[ "uno:GND.1", "btn5:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:6", "btn6:1.r", "blue", [ "v-48", "h0" ] ],
[ "uno:GND.1", "btn6:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:5", "btn7:1.r", "purple", [ "v-40", "h0" ] ],
[ "uno:GND.1", "btn7:2.r", "black", [ "v-16", "h0" ] ],
[ "uno:4", "btn8:1.r", "red", [ "v-32", "h0" ] ],
[ "uno:GND.1", "btn8:2.r", "black", [ "v-16", "h0" ] ]
]
} |
Generate a diagram.json code for alarm clock with RTC | /**
Arduino Digital Alarm Clock
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <SevSeg.h>
#include "Button.h"
#include "AlarmTone.h"
#include "Clock.h"
#include "config.h"
const int COLON_PIN = 13;
const int SPEAKER_PIN = A3;
Button hourButton(A0);
Button minuteButton(A1);
Button alarmButton(A2);
AlarmTone alarmTone;
Clock clock;
SevSeg sevseg;
enum DisplayState {
DisplayClock,
DisplayAlarmStatus,
DisplayAlarmTime,
DisplayAlarmActive,
DisplaySnooze,
};
DisplayState displayState = DisplayClock;
long lastStateChange = 0;
void changeDisplayState(DisplayState newValue) {
displayState = newValue;
lastStateChange = millis();
}
long millisSinceStateChange() {
return millis() - lastStateChange;
}
void setColon(bool value) {
digitalWrite(COLON_PIN, value ? LOW : HIGH);
}
void displayTime() {
DateTime now = clock.now();
bool blinkState = now.second() % 2 == 0;
sevseg.setNumber(now.hour() * 100 + now.minute());
setColon(blinkState);
}
void clockState() {
displayTime();
if (alarmButton.read() == Button::RELEASED && clock.alarmActive()) {
// Read alarmButton has_changed() to clear its state
alarmButton.has_changed();
changeDisplayState(DisplayAlarmActive);
return;
}
if (hourButton.pressed()) {
clock.incrementHour();
}
if (minuteButton.pressed()) {
clock.incrementMinute();
}
if (alarmButton.pressed()) {
clock.toggleAlarm();
changeDisplayState(DisplayAlarmStatus);
}
}
void alarmStatusState() {
setColon(false);
sevseg.setChars(clock.alarmEnabled() ? " on" : " off");
if (millisSinceStateChange() > ALARM_STATUS_DISPLAY_TIME) {
changeDisplayState(clock.alarmEnabled() ? DisplayAlarmTime : DisplayClock);
return;
}
}
void alarmTimeState() {
DateTime alarm = clock.alarmTime();
sevseg.setNumber(alarm.hour() * 100 + alarm.minute(), -1);
if (millisSinceStateChange() > ALARM_HOUR_DISPLAY_TIME || alarmButton.pressed()) {
changeDisplayState(DisplayClock);
return;
}
if (hourButton.pressed()) {
clock.incrementAlarmHour();
lastStateChange = millis();
}
if (minuteButton.pressed()) {
clock.incrementAlarmMinute();
lastStateChange = millis();
}
if (alarmButton.pressed()) {
changeDisplayState(DisplayClock);
}
}
void alarmState() {
displayTime();
if (alarmButton.read() == Button::RELEASED) {
alarmTone.play();
}
if (alarmButton.pressed()) {
alarmTone.stop();
}
if (alarmButton.released()) {
alarmTone.stop();
bool longPress = alarmButton.repeat_count() > 0;
if (longPress) {
clock.stopAlarm();
changeDisplayState(DisplayClock);
} else {
clock.snooze();
changeDisplayState(DisplaySnooze);
}
}
}
void snoozeState() {
sevseg.setChars("****");
if (millisSinceStateChange() > SNOOZE_DISPLAY_TIME) {
changeDisplayState(DisplayClock);
return;
}
}
void setup() {
Serial.begin(115200);
clock.begin();
hourButton.begin();
hourButton.set_repeat(500, 200);
minuteButton.begin();
minuteButton.set_repeat(500, 200);
alarmButton.begin();
alarmButton.set_repeat(1000, -1);
alarmTone.begin(SPEAKER_PIN);
pinMode(COLON_PIN, OUTPUT);
byte digits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12};
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = true;
bool disableDecPoint = true;
sevseg.begin(DISPLAY_TYPE, digits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
sevseg.refreshDisplay();
switch (displayState) {
case DisplayClock:
clockState();
break;
case DisplayAlarmStatus:
alarmStatusState();
break;
case DisplayAlarmTime:
alarmTimeState();
break;
case DisplayAlarmActive:
alarmState();
break;
case DisplaySnooze:
snoozeState();
break;
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-7segment",
"id": "7segment",
"top": -29.04,
"left": 330.64,
"rotate": 0,
"hide": false,
"attrs": { "commonPin": "anode", "digits": "4", "colon": "1" }
},
{
"type": "wokwi-buzzer",
"id": "buzzer1",
"top": 190.17,
"left": 466.82,
"rotate": 90,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-pushbutton",
"id": "pushbutton1",
"top": 110,
"left": 300,
"rotate": 0,
"hide": false,
"attrs": { "label": "Hours", "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "pushbutton2",
"top": 110,
"left": 380,
"rotate": 0,
"hide": false,
"attrs": { "label": "Minutes", "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "pushbutton3",
"top": 110,
"left": 460,
"rotate": 0,
"hide": false,
"attrs": { "label": "Alarm" }
},
{
"type": "wokwi-ds1307",
"id": "ds1307",
"top": 235.82,
"left": 283.79,
"rotate": 0,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-arduino-uno",
"id": "arduino",
"top": 0,
"left": 0,
"rotate": 0,
"hide": false,
"attrs": {}
}
],
"connections": [
[ "7segment:DIG1", "arduino:2", "gold", [ "v-20", "h0" ] ],
[ "7segment:DIG2", "arduino:3", "green", [ "v-24", "h0" ] ],
[ "7segment:DIG3", "arduino:4", "orange", [ "v-28", "h0" ] ],
[ "7segment:DIG4", "arduino:5", "purple", [ "v28", "h-150", "*", "v-16" ] ],
[ "7segment:A", "arduino:6", "gray", [ "v-16", "h0" ] ],
[ "7segment:B", "arduino:7", "purple", [ "v-36", "h0" ] ],
[ "7segment:C", "arduino:8", "blue", ["v20", "h-120", "*", "v-20"] ],
[ "7segment:D", "arduino:9", "cyan", [ "v16", "h-94", "*", "v-24" ] ],
[ "7segment:E", "arduino:10", "green", ["v12", "h-80", "*", "v-28"] ],
[ "7segment:F", "arduino:11", "brown", [ "v-40", "h0" ] ],
[ "7segment:G", "arduino:12", "orange", ["v24", "h-134", "*", "v-32"] ],
[ "7segment:CLN", "arduino:13", "cyan", [ "v-44", "h0" ] ],
[ "7segment:COM", "arduino:5V", "red", [ "v300", "h0" ] ],
[ "buzzer1:1", "arduino:GND.2", "black", [] ],
[ "buzzer1:2", "arduino:A3", "orange", [] ],
[ "pushbutton1:1.l", "arduino:A0", "green", [ "h-4", "v0", "*", "v20" ] ],
[ "pushbutton1:2.l", "arduino:GND.2", "black", [ "*", "v34" ] ],
[ "pushbutton2:1.l", "arduino:A1", "gray", [ "h-4", "v0", "*", "v24" ] ],
[ "pushbutton2:2.l", "arduino:GND.2", "black", [ "*", "v34" ] ],
[ "pushbutton3:1.l", "arduino:A2", "purple", [ "h-4", "v0", "*", "v28" ] ],
[ "pushbutton3:2.l", "arduino:GND.2", "black", [ "*", "v34" ] ],
[ "ds1307:GND", "arduino:GND.2", "black", [] ],
[ "ds1307:5V", "arduino:5V", "red", [] ],
[ "ds1307:SDA", "arduino:A4", "blue", [] ],
[ "ds1307:SCL", "arduino:A5", "gold", [] ]
]
} |
Generate a diagram.json code for electronic safe | /**
Arduino Electronic Safe
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
/* Locking mechanism definitions */
#define SERVO_PIN 6
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
Servo lockServo;
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
/* SafeState stores the secret code in EEPROM */
SafeState safeState;
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
void showStartupMessage() {
lcd.setCursor(4, 0);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(0, 2);
String message = "ArduinoSafe v1.0";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
String inputSecretCode() {
lcd.setCursor(5, 1);
lcd.print("[____]");
lcd.setCursor(6, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
}
}
return result;
}
void showWaitScreen(int delayMillis) {
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("=");
}
}
bool setNewCode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new code:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new code");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("Unlocked!");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
delay(1000);
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(2, 0);
lcd.print(" # to lock");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A = new code");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
}
if (readyToLock) {
lcd.clear();
lcd.setCursor(5, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.print(" ");
lcd.write(ICON_RIGHT_ARROW);
lcd.print(" ");
lcd.write(ICON_LOCKED_CHAR);
safeState.lock();
lock();
showWaitScreen(100);
}
}
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Safe Locked! ");
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
showWaitScreen(1000);
}
}
void setup() {
lcd.begin(16, 2);
init_icons(lcd);
lockServo.attach(SERVO_PIN);
/* Make sure the physical lock is sync with the EEPROM state */
Serial.begin(115200);
if (safeState.locked()) {
lock();
} else {
unlock();
}
showStartupMessage();
}
void loop() {
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "id": "uno", "type": "wokwi-arduino-uno", "top": 200, "left": 20 },
{
"id": "keypad",
"type": "wokwi-membrane-keypad",
"left": 360,
"top": 140
},
{
"id": "servo",
"type": "wokwi-servo",
"left": 400,
"top": 20,
"attrs": { "hornColor": "black" }
},
{ "id": "lcd", "type": "wokwi-lcd1602", "top": 8, "left": 20 },
{
"id": "r1",
"type": "wokwi-resistor",
"top": 140,
"left": 220,
"attrs": { "value": "220" }
}
],
"connections": [
["uno:GND.1", "lcd:VSS", "black", ["v-51", "*", "h0", "v18"]],
["uno:GND.1", "lcd:K", "black", ["v-51", "*", "h0", "v18"]],
["uno:GND.1", "lcd:RW", "black", ["v-51", "*", "h0", "v18"]],
["uno:5V", "lcd:VDD", "red", ["v16", "h-16"]],
["uno:5V", "r1:2", "red", ["v16", "h-118", "v-244", "h50"]],
["r1:1", "lcd:A", "pink", []],
["uno:12", "lcd:RS", "blue", ["v-16", "*", "h0", "v20"]],
["uno:11", "lcd:E", "purple", ["v-20", "*", "h0", "v20"]],
["uno:10", "lcd:D4", "green", ["v-24", "*", "h0", "v20"]],
["uno:9", "lcd:D5", "brown", ["v-28", "*", "h0", "v20"]],
["uno:8", "lcd:D6", "gold", ["v-32", "*", "h0", "v20"]],
["uno:7", "lcd:D7", "gray", ["v-36", "*", "h0", "v20"]],
["uno:6", "servo:PWM", "orange", ["v-40", "*", "h0", "h-52"]],
["uno:5V", "servo:V+", "red", ["v16", "h-118", "v-244", "*", "h-56"]],
["uno:GND.1", "servo:GND", "black", ["v-46", "*", "v88", "h-60"]],
["uno:A3", "keypad:C1", "brown", ["v116", "*", "h0", "v0"]],
["uno:A2", "keypad:C2", "gray", ["v120", "*", "h0", "v0"]],
["uno:A1", "keypad:C3", "orange", ["v124", "*", "h0", "v0"]],
["uno:A0", "keypad:C4", "pink", ["v128", "*", "h0", "v0"]],
["uno:5", "keypad:R1", "blue", ["v-34", "h96", "*", "v12"]],
["uno:4", "keypad:R2", "green", ["v-30", "h80", "*", "v16"]],
["uno:3", "keypad:R3", "purple", ["v-26", "h64", "*", "v20"]],
["uno:2", "keypad:R4", "gold", ["v-22", "h48", "*", "v24"]]
]
} |
Generate a diagram.json code for nano pong | /*
A simple Pong game.
https://notabug.org/Maverick/WokwiPong
Based on Arduino Pong by eholk
https://github.com/eholk/Arduino-Pong
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define UP_BUTTON 2
#define DOWN_BUTTON 3
const unsigned long PADDLE_RATE = 64;
const unsigned long BALL_RATE = 16;
const uint8_t PADDLE_HEIGHT = 12;
const uint8_t SCORE_LIMIT = 9;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
bool game_over, win;
uint8_t player_score, mcu_score;
uint8_t ball_x = 53, ball_y = 26;
uint8_t ball_dir_x = 1, ball_dir_y = 1;
unsigned long ball_update;
unsigned long paddle_update;
const uint8_t MCU_X = 12;
uint8_t mcu_y = 16;
const uint8_t PLAYER_X = 115;
uint8_t player_y = 16;
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Display the splash screen (we're legally required to do so)
display.display();
unsigned long start = millis();
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
display.clearDisplay();
drawCourt();
while(millis() - start < 2000);
display.display();
ball_update = millis();
paddle_update = ball_update;
}
void loop()
{
bool update_needed = false;
unsigned long time = millis();
static bool up_state = false;
static bool down_state = false;
up_state |= (digitalRead(UP_BUTTON) == LOW);
down_state |= (digitalRead(DOWN_BUTTON) == LOW);
if(time > ball_update)
{
uint8_t new_x = ball_x + ball_dir_x;
uint8_t new_y = ball_y + ball_dir_y;
// Check if we hit the vertical walls
if(new_x == 0 || new_x == 127)
{
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x + ball_dir_x;
if (new_x < 64)
{
player_scoreTone();
player_score++;
}
else
{
mcu_scoreTone();
mcu_score++;
}
if (player_score == SCORE_LIMIT || mcu_score == SCORE_LIMIT)
{
win = player_score > mcu_score;
game_over = true;
}
}
// Check if we hit the horizontal walls.
if(new_y == 0 || new_y == 53)
{
wallTone();
ball_dir_y = -ball_dir_y;
new_y += ball_dir_y + ball_dir_y;
}
// Check if we hit the CPU paddle
if(new_x == MCU_X && new_y >= mcu_y && new_y <= mcu_y + PADDLE_HEIGHT)
{
mcuPaddleTone();
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x + ball_dir_x;
}
// Check if we hit the player paddle
if(new_x == PLAYER_X && new_y >= player_y && new_y <= player_y + PADDLE_HEIGHT)
{
playerPaddleTone();
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x + ball_dir_x;
}
display.drawPixel(ball_x, ball_y, BLACK);
display.drawPixel(new_x, new_y, WHITE);
ball_x = new_x;
ball_y = new_y;
ball_update += BALL_RATE;
update_needed = true;
}
if(time > paddle_update)
{
paddle_update += PADDLE_RATE;
// CPU paddle
display.drawFastVLine(MCU_X, mcu_y, PADDLE_HEIGHT, BLACK);
const uint8_t half_paddle = PADDLE_HEIGHT >> 1;
if(mcu_y + half_paddle > ball_y)
{
int8_t dir = ball_x > MCU_X ? -1 : 1;
mcu_y += dir;
}
if(mcu_y + half_paddle < ball_y)
{
int8_t dir = ball_x > MCU_X ? 1 : -1;
mcu_y += dir;
}
if(mcu_y < 1)
{
mcu_y = 1;
}
if(mcu_y + PADDLE_HEIGHT > 53)
{
mcu_y = 53 - PADDLE_HEIGHT;
}
// Player paddle
display.drawFastVLine(MCU_X, mcu_y, PADDLE_HEIGHT, WHITE);
display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, BLACK);
if(up_state)
{
player_y -= 1;
}
if(down_state)
{
player_y += 1;
}
up_state = down_state = false;
if(player_y < 1)
{
player_y = 1;
}
if(player_y + PADDLE_HEIGHT > 53)
{
player_y = 53 - PADDLE_HEIGHT;
}
display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, WHITE);
update_needed = true;
}
if(update_needed)
{
if (game_over)
{
const char* text = win ? "YOU WIN!!" : "YOU LOSE!";
display.clearDisplay();
display.setCursor(40, 28);
display.print(text);
display.display();
delay(5000);
display.clearDisplay();
ball_x = 53;
ball_y = 26;
ball_dir_x = 1;
ball_dir_y = 1;
mcu_y = 16;
player_y = 16;
mcu_score = 0;
player_score = 0;
game_over = false;
drawCourt();
}
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 56);
display.print(mcu_score);
display.setCursor(122, 56);
display.print(player_score);
display.display();
}
}
void playerPaddleTone()
{
tone(11, 250, 25);
delay(25);
noTone(11);
}
void mcuPaddleTone()
{
tone(11, 225, 25);
delay(25);
noTone(11);
}
void wallTone()
{
tone(11, 200, 25);
delay(25);
noTone(11);
}
void player_scoreTone()
{
tone(11, 200, 25);
delay(50);
noTone(11);
delay(25);
tone(11, 250, 25);
delay(25);
noTone(11);
}
void mcu_scoreTone()
{
tone(11, 250, 25);
delay(25);
noTone(11);
delay(25);
tone(11, 200, 25);
delay(25);
noTone(11);
}
void drawCourt()
{
display.drawRect(0, 0, 128, 54, WHITE);
} | {
"version": 1,
"author": "Maverick",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
{ "type": "board-ssd1306", "id": "oled1", "top": 89.5, "left": 19.05, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 87.7,
"left": 136.24,
"attrs": { "color": "green", "key": "ArrowUp" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 134.3,
"left": 135.54,
"attrs": { "color": "green", "key": "ArrowDown" }
},
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -103.22,
"left": 59.98,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": -22.03,
"left": 29.82,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "nano:A4", "oled1:SDA", "gold", [ "v0" ] ],
[ "oled1:SCL", "nano:A5", "cyan", [ "v-20.36", "h20.38" ] ],
[ "oled1:VCC", "nano:3.3V", "red", [ "v-20.07", "h-37.37" ] ],
[ "oled1:GND", "nano:GND.1", "black", [ "v-12.82", "h87.28" ] ],
[ "btn1:2.l", "btn2:2.l", "black", [ "h-6.06", "v46.8" ] ],
[ "btn1:2.l", "nano:GND.1", "black", [ "h-6.51", "v-36.84", "h14.77" ] ],
[ "btn1:1.r", "nano:2", "black", [ "h13.06", "v-114.15", "h-100.6" ] ],
[ "btn2:1.r", "nano:3", "black", [ "v-0.29", "h14.22", "v-160.46", "h-110.66" ] ],
[ "bz1:2", "r1:2", "red", [ "v0" ] ],
[ "r1:1", "nano:11", "red", [ "h-0.55", "v50.86" ] ],
[ "bz1:1", "nano:GND.2", "black", [ "h34.91", "v64.63" ] ]
]
} |
Generate a diagram.json code for touch lcd breakout game | #include <SD.h>
#include <adafruit_1947_Obj.h>
#include <screen.h>
#include "ourOSObj.h"
ourOSObj ourOS;
void setup() {
Serial.begin(57600); // Fire up serial for debugging.
if (!initScreen(ADAFRUIT_1947,ADA_1947_SHIELD_CS,PORTRAIT)) { // Init screen.
Serial.println("NO SCREEN!"); // Screen init failed. Tell user.
Serial.flush(); // Make sure the message gets out.
while(true); // Lock the process here.
}
if (!SD.begin(ADA_1947_SHIELD_SDCS)) { // With icons, we now MUST have an SD card.
Serial.println("NO SD CARD!"); // Tell user we have no SD card.
Serial.flush(); // Make sure the message gets out.
while(true); // Lock the process here.
}
ourEventMgr.begin(); // Startup our event manager.
ourOS.begin(); // Boot OS manager.
nextPanel = breakoutApp; // <<-- URISH LOOK HERE!!
}
void loop() { // During loop..
idle(); // Idlers get their time.
ourOS.loop(); // ourOS gets time to pass on to the current panel.
} | {
"version": 1,
"author": "Jim Lee",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-mega",
"id": "mega",
"top": 272.86,
"left": -34.52,
"attrs": { "__fakeRamSize": "65000" }
},
{
"type": "board-ili9341-cap-touch",
"id": "lcd1",
"top": -149.52,
"left": 75.06,
"attrs": {}
},
{
"type": "wokwi-microsd-card",
"id": "sd1",
"top": 39.23,
"left": 395.41,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": 481.7,
"left": 153.47,
"attrs": { "volume": "0.025" }
}
],
"connections": [
[ "mega:GND.1", "lcd1:GND", "black", [ "v-53", "h-5" ] ],
[ "mega:52", "lcd1:SCK", "white", [ "v-54", "h19" ] ],
[ "mega:50", "lcd1:MISO", "green", [ "v-43", "h35" ] ],
[ "mega:51", "lcd1:MOSI", "blue", [ "v0", "h-197", "v-226", "h112" ] ],
[ "mega:53", "lcd1:D/C", "yellow", [ "v-67", "h-28" ] ],
[ "mega:5V", "lcd1:VCC", "red", [ "v26", "h-197", "v-226", "h112" ] ],
[ "mega:10", "lcd1:CS", "red", [ "v-90", "h-31" ] ],
[ "mega:20", "lcd1:SDA", "blue", [ "v24" ] ],
[ "mega:52", "sd1:SCK", "white", [ "v-54", "h19" ] ],
[ "mega:50", "sd1:DO", "green", [ "v-43", "h35" ] ],
[ "mega:53", "sd1:CD", "yellow", [ "v-67", "h-28" ] ],
[ "mega:4", "sd1:CS", "green", [ "v-87", "h63" ] ],
[ "mega:5V", "sd1:VCC", "red", [ "v26.06", "h273", "v-325.06", "h27.3" ] ],
[ "mega:GND.1", "sd1:GND", "black", [ "v-53", "h152" ] ],
[ "mega:51", "sd1:DI", "blue", [ "v-2.49", "h31.94", "v-286.49", "h50.95" ] ],
[ "mega:21", "lcd1:SCL", "white", [ "v-120.21", "h-4.54" ] ],
[ "mega:GND.2", "bz1:1", "black", [ "v0" ] ],
[ "mega:14", "bz1:2", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for dino game | // A simple T-rex Run game based on the 30DLIS kit
// Developed by Ishani and Arijit Sengupta
// Required libraries
#include <U8glib.h>
#include "TM1637Display.h"
// HW Pins required
#define SW 3 // RotaryEncoder SW Pin
#define DT 4 // RotaryEncoder DT Pin
#define CLK 2 // RotaryEncoder CLK Pin
// Define the display connections pins:
#define DCLK 6
#define DIO 5
// pin 10 drives the buzzer
#define buzzer 10
// In Addition, this sketch uses the I2C Pins for the U8G Panel
// A1+A2
// Create display object of type TM1637Display
// The display shows the current score
TM1637Display OurDisplay = TM1637Display(DCLK, DIO);
// Create array that turns all segments off:
const uint8_t blank[] = {0x00, 0x00, 0x00, 0x00}; // 0xff is a hexidecimal number whose binary
// representation is all zeros
// Create the oled display object - this shows gameplay
// and welcome/win/loss messages
U8GLIB_SH1106_128X64 My_u8g_Panel(U8G_I2C_OPT_NONE); // I2C / TWI
/*
For documentation on u8glib functions:
https://github.com/olikraus/u8glib/wiki/userreference
*/
// Sounds we use for the hit effects
#define jumpSound 700
#define blahSound 125
#define speedSound 1000
#define DBOUNCE 180
// Game states
#define gameStart 0
#define gameEnd 1
#define gamePlaying 2
volatile int gameStatus = gameStart;
// PRE-SAVED BITMAP CONSTANTS
//20 x 21
static unsigned char dinoJump [] U8G_PROGMEM = {
0x00, 0xFC, 0x07, 0x00, 0xFE, 0x07, 0x00, 0xEE, 0x0F, 0x00, 0xFE, 0x0F,
0x00, 0xFE, 0x0F, 0x00, 0xFE, 0x0F, 0x00, 0xFE, 0x07, 0x06, 0xFF, 0x03,
0xC3, 0xFF, 0x00, 0xE7, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00,
0xFF, 0x3F, 0x00, 0xFE, 0x3F, 0x00, 0xFC, 0x1F, 0x00, 0xF8, 0x1F, 0x00,
0xF0, 0x1F, 0x00, 0xF0, 0x0E, 0x00, 0x60, 0x0E, 0x00, 0xE0, 0x0E, 0x00,
0xE0, 0x1E, 0x00,
};
//20 x 21
static unsigned char dinoLeft [] U8G_PROGMEM = {
0x00, 0xFC, 0x07, 0x00, 0xFE, 0x07, 0x00, 0xEE, 0x0F, 0x00, 0xFE, 0x0F,
0x00, 0xFE, 0x0F, 0x00, 0x7E, 0x08, 0x00, 0x7E, 0x00, 0x06, 0xFF, 0x03,
0x87, 0x3F, 0x00, 0xE7, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00,
0xFF, 0x3F, 0x00, 0xFE, 0x3F, 0x00, 0xFC, 0x1F, 0x00, 0xF8, 0x1F, 0x00,
0xF0, 0x1F, 0x00, 0xE0, 0x1E, 0x00, 0x60, 0x00, 0x00, 0xE0, 0x00, 0x00,
0xE0, 0x00, 0x00,
};
//20 x 21
static unsigned char dinoRight [] U8G_PROGMEM = {
0x00, 0xFC, 0x07, 0x00, 0xEE, 0x07, 0x00, 0xE6, 0x0F, 0x00, 0xFE, 0x0F,
0x00, 0xFE, 0x0F, 0x00, 0xFE, 0x0F, 0x00, 0x7C, 0x00, 0x06, 0xFF, 0x03,
0xC3, 0xFF, 0x00, 0xE7, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00,
0xFF, 0x3F, 0x00, 0xFE, 0x3F, 0x00, 0xFC, 0x1F, 0x00, 0xF8, 0x1F, 0x00,
0xF0, 0x1F, 0x00, 0xF0, 0x0F, 0x00, 0xE0, 0x0E, 0x00, 0x00, 0x0E, 0x00,
0x00, 0x1E, 0x00,
};
//shape 0: 39 x 12
static unsigned char cloud [] U8G_PROGMEM = {
0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xBE, 0x03, 0x00, 0x00, 0x00,
0x03, 0x06, 0x00, 0x00, 0x80, 0x01, 0x04, 0x00, 0x00, 0x40, 0x00, 0x1C,
0x00, 0x00, 0x40, 0x00, 0xE4, 0x03, 0x00, 0x18, 0x00, 0x00, 0x02, 0xE0,
0x0F, 0x00, 0x00, 0x0E, 0x30, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,
0x00, 0x20, 0x12, 0x00, 0x00, 0x00, 0x40, 0x03, 0xFF, 0xFF, 0xFF, 0x7F,
};
//shape 1: 10 x 20
static unsigned char oneCactus [] U8G_PROGMEM = {
0x30, 0x00, 0x78, 0x00, 0x78, 0x00, 0x78, 0x00, 0x78, 0x01, 0xFB, 0x03,
0xFF, 0x03, 0xFF, 0x03, 0xFF, 0x03, 0xFF, 0x03, 0xFF, 0x03, 0xFF, 0x01,
0xFE, 0x00, 0x78, 0x00, 0x78, 0x00, 0x78, 0x00, 0x78, 0x00, 0x78, 0x00,
0x78, 0x00, 0x00, 0x00,
};
//shape 2: 20 x 20
static unsigned char twoCactus [] U8G_PROGMEM = {
0x30, 0xC0, 0x00, 0x38, 0xE0, 0x00, 0x38, 0xE8, 0x00, 0x38, 0xEC, 0x00,
0x38, 0xED, 0x04, 0xBB, 0xED, 0x0E, 0xBB, 0xED, 0x0E, 0xBB, 0xFD, 0x0E,
0xBB, 0xFD, 0x0E, 0xBB, 0xF9, 0x0E, 0xFF, 0xF1, 0x0F, 0xFF, 0xE0, 0x07,
0x7E, 0xE0, 0x01, 0x38, 0xE0, 0x00, 0x38, 0xE0, 0x00, 0x38, 0xE0, 0x00,
0x38, 0xE0, 0x00, 0x38, 0xE0, 0x00, 0x38, 0xE0, 0x00, 0x00, 0x00, 0x00,
};
//shape 3: 20 x 20
static unsigned char threeCactus [] U8G_PROGMEM = {
0x00, 0xC0, 0x00, 0x18, 0xC0, 0x01, 0x18, 0xC0, 0x01, 0x58, 0xD8, 0x01,
0x58, 0xFC, 0x01, 0x58, 0xFC, 0x0F, 0x78, 0xDC, 0x0F, 0x7F, 0xFC, 0x0F,
0x3B, 0xFD, 0x0D, 0x1B, 0xF9, 0x0C, 0x5B, 0xF5, 0x0F, 0x5B, 0xC5, 0x07,
0x5F, 0xE7, 0x03, 0xDE, 0xE7, 0x01, 0xD8, 0xC3, 0x01, 0x98, 0xC1, 0x01,
0x18, 0xC1, 0x01, 0x18, 0xC1, 0x01, 0x18, 0xE1, 0x01, 0x00, 0x00, 0x00,
};
//shape 4: 6 x 12
static unsigned char oneCactusSmall [] U8G_PROGMEM = {
0x0C, 0x0C, 0x3C, 0x3D, 0x2D, 0x3D, 0x1D, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C,
};
//shape 5: 12 x 12
static unsigned char twoCactusSmall [] U8G_PROGMEM = {
0x0C, 0x03, 0x0C, 0x03, 0x6C, 0x0B, 0x6D, 0x0B, 0x6D, 0x0B, 0xBD, 0x0B,
0x1F, 0x0F, 0x0E, 0x03, 0x0C, 0x03, 0x0C, 0x03, 0x0C, 0x03, 0x0C, 0x03,
};
//shape 6: 17 x 12
static unsigned char threeCactusSmall [] U8G_PROGMEM = {
0x04, 0x41, 0x00, 0x0C, 0x61, 0x00, 0xFC, 0x79, 0x01, 0xFD, 0x7D, 0x01,
0x7D, 0x6D, 0x01, 0x7D, 0x7D, 0x01, 0xCF, 0xE5, 0x01, 0xCE, 0x67, 0x00,
0x8C, 0x67, 0x00, 0x0C, 0x63, 0x00, 0x0C, 0x61, 0x00, 0x0C, 0x61, 0x00,
};
static unsigned char dinoBlah [] U8G_PROGMEM = {
0x00, 0xFC, 0x07, 0x00, 0xFE, 0x07, 0x00, 0xC6, 0x0F, 0x00, 0xC6, 0x0F,
0x00, 0xCE, 0x0F, 0x00, 0xFE, 0x0F, 0x00, 0xFE, 0x0F, 0x06, 0xFF, 0x03,
0x87, 0x7F, 0x00, 0xE7, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00,
0xFF, 0x3F, 0x00, 0xFE, 0x3F, 0x00, 0xFC, 0x1F, 0x00, 0xF8, 0x1F, 0x00,
0xF0, 0x1F, 0x00, 0xF0, 0x0E, 0x00, 0x60, 0x0E, 0x00, 0x60, 0x0E, 0x00,
0xE0, 0x1E, 0x00,
};
static unsigned char gameOver [] U8G_PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xF8, 0x01, 0x02, 0x0C, 0x0C, 0x3E, 0x00, 0x78, 0x60,
0x30, 0x7C, 0xF0, 0x01, 0x0C, 0x01, 0x07, 0x14, 0x0A, 0x02, 0x00, 0x84,
0x40, 0x10, 0x04, 0x10, 0x02, 0x04, 0x00, 0x05, 0x14, 0x0A, 0x02, 0x00,
0x02, 0x41, 0x10, 0x04, 0x10, 0x02, 0x04, 0x00, 0x05, 0x14, 0x0A, 0x02,
0x00, 0x02, 0xC1, 0x18, 0x04, 0x10, 0x02, 0xC4, 0x81, 0x0D, 0x34, 0x0B,
0x3E, 0x00, 0x02, 0x81, 0x08, 0x7C, 0xF0, 0x01, 0x04, 0x81, 0x08, 0x24,
0x09, 0x02, 0x00, 0x02, 0x81, 0x0D, 0x04, 0x10, 0x01, 0x04, 0x81, 0x0F,
0x64, 0x09, 0x02, 0x00, 0x02, 0x01, 0x05, 0x04, 0x10, 0x02, 0x0C, 0xC1,
0x18, 0xC4, 0x08, 0x02, 0x00, 0x84, 0x00, 0x05, 0x04, 0x10, 0x02, 0xF8,
0x41, 0x10, 0xC4, 0x08, 0x3E, 0x00, 0x78, 0x00, 0x07, 0x7C, 0x10, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
};
// Other Game attributes
// various variables
int currentStateCLK;
int lastStateCLK;
int MyScore = 0;
int dinoMove = 0;
volatile int jumping = 0;
int cloudx = 128;
int obstacles [2] = {1, 4};
int obstaclex [2] = {128, 200};
int speed = 8;
unsigned long startTime = millis(), curTime;
int lastBeep = 0;
// Display the score on the 7seg display
void ShowScore () {
if (gameStatus == gamePlaying) {
curTime = millis ();
MyScore = (curTime - startTime) * speed / 1000;
OurDisplay.showNumberDecEx(MyScore);
if (MyScore / 100 > lastBeep) {
tone (buzzer, 1000, 100);
delay (150);
tone (buzzer, 1250, 100);
lastBeep = MyScore / 100;
}
}
}
void StartStopGame () {
static unsigned long last_interrupt = 0;
if (millis() - last_interrupt > DBOUNCE) {
if (gameStatus == gamePlaying) {
if (jumping == 0) {
jumping = 1;
tone (buzzer, jumpSound, 100);
}
}
else if (gameStatus == gameStart) gameStatus = gamePlaying;
else gameStatus = gameStart;
}
last_interrupt = millis(); //note the last time the ISR was called
}
void resetGame () {
MyScore = 0;
startTime = millis();
obstaclex[0] = 128;
obstaclex [1] = 200;
dinoMove = 0;
}
void setup() {
OurDisplay.setBrightness(7);
OurDisplay.clear();
resetGame();
ShowScore ();
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
attachInterrupt(digitalPinToInterrupt(SW), StartStopGame, FALLING);
}
// ************************************************
void loop(void) {
ShowScore();
My_u8g_Panel.firstPage();
do {
draw();
} while ( My_u8g_Panel.nextPage() );
if (gameStatus == gamePlaying) {
moveDino();
moveCloud();
moveObstacles();
}
}
void moveDino () {
if (jumping == 0) dinoMove = (dinoMove + 1) % 3;
else {
if (jumping == 1) {
dinoMove = dinoMove + 8;
if (dinoMove > 32) jumping = 2;
} else {
dinoMove = dinoMove - 8;
if (dinoMove < 8) {
jumping = 0;
dinoMove = 0;
}
}
}
checkCollision ();
}
void moveCloud () {
cloudx --;
if (cloudx < -38) cloudx = 128;
}
void moveObstacles() {
int obx = obstaclex [0];
obx = obx - speed;
if (obx < -20) {
obstaclex[0] = obstaclex[1];
obstaclex[1] = obstaclex[0] + random(80, 125);
obstacles[0] = obstacles[1];
obstacles[1] = random (1, 6);
}
else {
obstaclex[0] = obx;
obstaclex[1] -= speed;
}
}
// ************************************************
void draw(void) {
u8g_prepare();
if (gameStatus == gamePlaying) {
drawDino ();
drawShape (0, cloudx);
drawObsticles ();
}
else if (gameStatus == gameStart) {
My_u8g_Panel.drawStr( 0, 10, "Welcome to");
My_u8g_Panel.drawStr( 10, 30, "Dino!!");
My_u8g_Panel.drawStr( 0, 50, "Push to begin");
resetGame();
ShowScore();
}
else {
My_u8g_Panel.drawXBMP( 14, 12, 100, 15, gameOver);
drawDino ();
drawShape (0, cloudx);
drawObsticles ();
}
}
void drawDino (void) {
if (gameStatus == gameEnd) {
My_u8g_Panel.drawXBMP( 0, 43 - dinoMove, 20, 21, dinoBlah);
return;
}
switch (dinoMove) {
case -1: My_u8g_Panel.drawXBMP( 0, 43, 20, 21, dinoBlah); break;
case 0: My_u8g_Panel.drawXBMP( 0, 43, 20, 21, dinoJump); break;
case 1: My_u8g_Panel.drawXBMP( 0, 43, 20, 21, dinoLeft); break;
case 2: My_u8g_Panel.drawXBMP( 0, 43, 20, 21, dinoRight); break;
default: My_u8g_Panel.drawXBMP( 0, 43 - dinoMove, 20, 21, dinoJump); break;
}
}
void drawShape (int shape, int x) {
switch (shape) {
case 0: My_u8g_Panel.drawXBMP( x, 5, 39, 12, cloud); break;
case 1: My_u8g_Panel.drawXBMP( x, 44, 10, 20, oneCactus); break;
case 2: My_u8g_Panel.drawXBMP( x, 44, 20, 20, twoCactus); break;
case 3: My_u8g_Panel.drawXBMP( x, 44, 20, 20, threeCactus); break;
case 4: My_u8g_Panel.drawXBMP( x, 52, 6, 12, oneCactusSmall); break;
case 5: My_u8g_Panel.drawXBMP( x, 52, 12, 12, twoCactusSmall); break;
case 6: My_u8g_Panel.drawXBMP( x, 52, 17, 12, threeCactusSmall); break;
}
}
void checkCollision () {
int obx = obstaclex [0];
int obw, obh;
switch (obstacles[0]) {
case 0: obw = 39; obh = 10; break;
case 1: obw = 10; obh = 20; break;
case 2: obw = 17; obh = 20; break;
case 3: obw = 17; obh = 20; break;
case 4: obw = 6; obh = 12; break;
case 5: obw = 12; obh = 12; break;
case 6: obw = 17; obh = 12; break;
}
if (obx > 15 || obx + obw < 5 || dinoMove > obh - 3) {}
else {
gameStatus = gameEnd;
tone (buzzer, 125, 100);
delay(150);
tone (buzzer, 125, 100);
}
}
void drawObsticles () {
drawShape (obstacles[0], obstaclex[0]);
drawShape (obstacles[1], obstaclex[1]);
}
void u8g_prepare(void) {
My_u8g_Panel.setFont(u8g_font_6x10);
My_u8g_Panel.setFontRefHeightExtendedText();
My_u8g_Panel.setDefaultForegroundColor();
My_u8g_Panel.setFontPosTop();
} | {
"version": 1,
"author": "Ami Sung",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 90.93, "left": -190.84, "attrs": {} },
{ "type": "board-ssd1306", "id": "oled1", "top": 121.27, "left": 158.38, "attrs": {} },
{ "type": "wokwi-ky-040", "id": "encoder1", "top": 268.93, "left": 115.97, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -41.84,
"left": 252.89,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-tm1637-7segment",
"id": "sevseg1",
"top": -44.94,
"left": 37.09,
"attrs": { "color": "red" }
}
],
"connections": [
[ "sevseg1:VCC", "uno:5V", "red", [ "h29.56", "v70.22", "h-142.27", "v268.59", "h-125.71" ] ],
[ "encoder1:VCC", "uno:5V", "red", [ "h21.09", "v71.64", "h-283.92" ] ],
[ "oled1:VCC", "uno:5V", "red", [ "v-37.85", "h-100.73", "v260", "h-135.52" ] ],
[ "sevseg1:GND", "uno:GND.1", "black", [ "h-1.1", "v38.55", "h-286.37" ] ],
[ "bz1:1", "uno:GND.1", "black", [ "v16.61", "h-356.06" ] ],
[ "oled1:GND", "uno:GND.3", "black", [ "v-22.52", "h-102.93", "v205.43", "h-95.66" ] ],
[ "encoder1:GND", "uno:GND.2", "black", [ "h14.96", "v51.4", "h-263.68" ] ],
[ "encoder1:SW", "uno:3", "gray", [ "h14.47", "v-73.42", "h-102.32", "v-157.92", "h-97.87" ] ],
[ "encoder1:DT", "uno:4", "gold", [ "h5.58", "v-54.93", "h-100.09", "v-148.29", "h-110.47" ] ],
[ "encoder1:CLK", "uno:2", "orange", [ "h-1.1", "v-35.79", "h-103.06", "v-143.84" ] ],
[ "bz1:2", "uno:10", "purple", [ "v15.53", "h50.01", "v-147.54", "h-370.71" ] ],
[ "sevseg1:DIO", "uno:5", "green", [ "h28.05", "v-57.18", "h-218.72" ] ],
[ "sevseg1:CLK", "uno:6", "green", [ "h18.41", "v-56.48", "h-224.65" ] ],
[ "oled1:SCL", "uno:A5", "blue", [ "v-34.37", "h57.64", "v303.38", "h-198.32" ] ],
[ "oled1:SDA", "uno:A4", "green", [ "v-23.17", "h61.03", "v300.18", "h-229.18" ] ]
]
} |
Generate a diagram.json code for Four LED blink series | int led5=5;
int led6=6;
int led7=7;
int led8=8;
void setup() {
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(led5,HIGH);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
delay(500);
digitalWrite(led5,LOW);
digitalWrite(led6,HIGH);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
delay(500);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,HIGH);
digitalWrite(led8, LOW);
delay(500);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
digitalWrite(led8, HIGH);
delay(500);
} | {
"version": 1,
"author": "salwa nafisya9.3",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 106.2, "left": 18.6, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -42, "left": 263, "attrs": { "color": "red" } },
{ "type": "wokwi-led", "id": "led2", "top": -42, "left": 157.4, "attrs": { "color": "blue" } },
{
"type": "wokwi-led",
"id": "led3",
"top": -42,
"left": 61.4,
"attrs": { "color": "purple" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": -42,
"left": -34.6,
"attrs": { "color": "green" }
}
],
"connections": [
[ "led1:A", "uno:8", "green", [ "v76.8", "h-96" ] ],
[ "led2:A", "uno:7", "green", [ "v67.2", "h9.6" ] ],
[ "led3:A", "uno:6", "green", [ "v57.6", "h124.8" ] ],
[ "led4:A", "uno:5", "green", [ "v48", "h230.4" ] ],
[ "led3:C", "uno:GND.1", "green", [ "v86.4", "h58" ] ],
[ "led4:C", "uno:GND.1", "green", [ "v76.8", "h154", "v28.8" ] ],
[ "led2:C", "uno:GND.1", "green", [ "v76.8", "h-38" ] ],
[ "led1:C", "uno:GND.1", "green", [ "v86.4", "h-143.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for test Arduino | int TIME_INTERVAL = 0;
bool isReady = false;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
TIME_INTERVAL++;
digitalWrite(13, LOW);
if(TIME_INTERVAL == 500) {
isReady = true;
TIME_INTERVAL = 0;
}
else {
return;
}
if(isReady == true) {
digitalWrite(13, HIGH);
isReady = false;
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": 9, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -70.8,
"left": 109.4,
"attrs": { "color": "red" }
}
],
"connections": [ [ "uno:13", "led1:C", "green", [ "v0" ] ], [ "led1:A", "uno:8", "green", [ "v0" ] ] ],
"dependencies": {}
} |
Generate a diagram.json code for SUP_LCD | #include <Arduino.h>
#include "timer_System.h"
#include "inps_manager.h"
#include "asciitohex.h"
#define VIEW_MENU 1
#define INC_MENU 2
#define DEC_MENU 3
const int KEY_MENU = 7;
const int KEY_INC = 8;
const int KEY_DEC = 9;
const int DIGIT_1 = 6;
const int DIGIT_2 = 5;
const int DIGIT_3 = 4;const int DATA_SPI = 12;
const int CLOCK_SPI = 11;
const int LOAD_SPI = 10;
uint8_t count = 16;
typedef union _MENU_DEVICE
{
uint8_t value;
struct
{
unsigned edit:2;
unsigned param_sel:3;
unsigned peram_edit:1;
unsigned blinK_dsp:1;
unsigned extend:1;
}flags;
}MENU_DEVICE;
MENU_DEVICE info_menu;
const uint16_t thresoldLow[3] = {85, 125, 5};
const uint16_t thresoldHigh[3] = {115, 140, 300};
uint16_t thresoldValue[3] = {100, 140, 20};
uint16_t paramToView[3] = {0};
void checkInputKeys(void);
void vProcessKeys(uint8_t sel);
void decimal_to_bcd(uint16_t decimal);
void SetTimer1()
{
cli(); //stop interrupts
//set timer1 interrupt at 120Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 60hz increments
OCR1A = 10000; // = (16*10^6) / (120 * 8) - 1 (must be <65536) //60Hz 16683
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
//TCCR1B |= (1 << CS12) | (1 << CS10);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); //allow interrupts
}
ISR(TIMER1_COMPA_vect)
{
flagTickBase = 1;
TimersSys_Tick();
inpManager_Refresh();
}
void setup()
{
SetTimer1();
TimersSys_Init(); // Inicializa la estructuras de los 4 timers del sistema, en el header de la libreria
TimersSys_Start(0, 10); // Inicializa 0 de TimerSys, de calibración 100 ms
TimersSys_Start(1, 25); // Inicializa 1 de TimersSys: 250ms (25 * Tick de TMR2(10ms))
TimersSys_Start(2, 3); // Inicializa 2 de TimersSys: 30ms (3 * Tick de TMR2(10ms))
TimersSys_Start(3, 50); // Inicializa 2 de TimersSys: 30ms (3 * Tick de TMR2(10ms))
inpsManager_Init();
inpsManager_Cfg(0, KEY_MENU, INPUT_PULLUP);
inpsManager_Cfg(1, KEY_INC, INPUT_PULLUP);
inpsManager_Cfg(2, KEY_DEC, INPUT_PULLUP);
Serial.begin(115200);
// put your setup code here, to run once:
/*pinMode(KEY_MENU, INPUT_PULLUP);
pinMode(KEY_INC, INPUT_PULLUP);*/
pinMode(DIGIT_1, OUTPUT);
pinMode(DIGIT_2, OUTPUT);
pinMode(DIGIT_3, OUTPUT);
pinMode(LOAD_SPI, OUTPUT);
pinMode(CLOCK_SPI, OUTPUT);
pinMode(DATA_SPI, OUTPUT);
digitalWrite(DIGIT_1, HIGH);
digitalWrite(DIGIT_2, HIGH);
digitalWrite(DIGIT_3, HIGH);
info_menu.value = 0;
}
void loop()
{
if (TIMERSYS_CHECK_EVENT(0)) // 100 ms
{
TIMERSYS_CLEAR_EVENT(0);
checkInputKeys( );
}
//llevar esta a 50 ms
if (TIMERSYS_CHECK_EVENT(1)) // 250 ms
{
TIMERSYS_CLEAR_EVENT(1);
}
if (TIMERSYS_CHECK_EVENT(2)) //30ms
{
TIMERSYS_CLEAR_EVENT(2);
if(++count >= 3)
{
count = 0;
}
if(info_menu.flags.edit == 0)
{
decimal_to_bcd(120);
digitSelect(count, SevenSegmentASCII[paramToView[count]]);
}
else if(info_menu.flags.edit == 1)
{
digitSelect(count, MenutoDisplay[info_menu.flags.param_sel - 1][count]);
}
else if(info_menu.flags.edit == 2)
{
decimal_to_bcd(thresoldValue[info_menu.flags.param_sel - 1]);
digitSelect(count, SevenSegmentASCII[paramToView[count]]);
}
}
if (TIMERSYS_CHECK_EVENT(3)) //500s
{
TIMERSYS_CLEAR_EVENT(3);
if(info_menu.flags.blinK_dsp == 0)
{
info_menu.flags.blinK_dsp = 1;
}
else
{
info_menu.flags.blinK_dsp = 0;
}
}
}
void checkInputKeys( )
{
if(inpsInfo[0].b.change == 1)
{
inpsInfo[0].b.change = 0;
if(!GET_INP_STATE(0))
{
vProcessKeys(VIEW_MENU);
}
}
if(inpsInfo[1].b.change == 1)
{
inpsInfo[1].b.change = 0;
if(!GET_INP_STATE(1))
{
vProcessKeys(INC_MENU);
//Serial.println("INC");
}
}
if(inpsInfo[2].b.change == 1)
{
inpsInfo[2].b.change = 0;
if(!GET_INP_STATE(2))
{
vProcessKeys(DEC_MENU);
//Serial.println("DEC");
}
}
}
void digitSelect(uint8_t select, uint8_t value)
{
digitalWrite(DIGIT_1, HIGH);
digitalWrite(DIGIT_2, HIGH);
digitalWrite(DIGIT_3, HIGH);
if(info_menu.flags.blinK_dsp == 1 && info_menu.flags.edit == 2)
{
return;
}
digitalWrite(LOAD_SPI, LOW);
shiftOut(DATA_SPI, CLOCK_SPI, MSBFIRST, value);
digitalWrite(LOAD_SPI, HIGH);
switch(select)
{
case 0:
digitalWrite(DIGIT_3, LOW);
break;
case 1:
digitalWrite(DIGIT_2, LOW);
break;
case 2:
digitalWrite(DIGIT_1, LOW);
break;
default:
return;
}
}
void vProcessKeys(uint8_t sel)
{
if(sel == VIEW_MENU)
{
if(info_menu.flags.edit == 0)
{
info_menu.flags.edit = 1;
info_menu.flags.param_sel = 1;
}
else if(info_menu.flags.edit == 1)
{
info_menu.flags.edit = 2;
}
else
{
info_menu.flags.edit = 0;
}
return;
}
if(sel == INC_MENU)
{
if(info_menu.flags.edit == 1)
{
if(++info_menu.flags.param_sel > 3)
{
info_menu.flags.param_sel = 0;
info_menu.flags.edit = 0;
}
}
else if(info_menu.flags.edit == 2)
{
if(++thresoldValue[info_menu.flags.param_sel -1] > thresoldHigh[info_menu.flags.param_sel -1])
{
thresoldValue[info_menu.flags.param_sel -1] = thresoldLow[info_menu.flags.param_sel -1];
}
}
}
if(sel == DEC_MENU)
{
if(info_menu.flags.edit == 2)
{
if(--thresoldValue[info_menu.flags.param_sel -1] < thresoldLow[info_menu.flags.param_sel -1])
{
thresoldValue[info_menu.flags.param_sel -1] = thresoldHigh[info_menu.flags.param_sel -1];
}
}
}
}
// Función para convertir decimal a BCD sin usar divisiones
void decimal_to_bcd(uint16_t decimal)
{
// Limitar el rango a 0-999
if (decimal > 999) decimal = 999;
///uint8_t hundreds = 0, tens = 0, units = 0;
paramToView[2] = paramToView[1] = paramToView[0] = 16;
// Calcular centenas (resta iterativa)
while (decimal >= 100) {
decimal -= 100;
paramToView[2]++;
}
// Calcular decenas (resta iterativa)
while (decimal >= 10) {
decimal -= 10;
paramToView[1]++;
}
// El restante son las unidades
paramToView[0] += decimal;
///uint16_t bcd = (hundreds << 8) | (tens << 4) | units;
///return bcd;
} | {
"version": 1,
"author": "Mauricio Bautista",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 120, "left": 28.3, "attrs": {} },
{
"type": "wokwi-7segment",
"id": "sevseg1",
"top": -109.38,
"left": -104.76,
"attrs": { "digits": "3", "colon": "", "common": "cathode" }
},
{
"type": "wokwi-pushbutton-6mm",
"id": "btn1",
"top": 113,
"left": 249.6,
"attrs": { "color": "green" }
},
{
"type": "wokwi-pushbutton-6mm",
"id": "btn2",
"top": 141.8,
"left": 249.6,
"attrs": { "color": "green" }
},
{ "type": "wokwi-74hc595", "id": "sr1", "top": -30, "left": 178.24, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd1", "top": -134.4, "left": 306.6, "attrs": {} },
{
"type": "wokwi-pushbutton-6mm",
"id": "btn3",
"top": 84.2,
"left": 249.6,
"attrs": { "color": "green" }
}
],
"connections": [
[ "nano:GND.1", "btn2:2.r", "black", [ "v28.8", "h153.6", "v-76.8" ] ],
[ "nano:GND.1", "btn1:2.r", "black", [ "v28.8", "h153.6", "v-115.2" ] ],
[ "nano:8", "btn1:1.l", "green", [ "v-38.4", "h153.6", "v28.8" ] ],
[ "nano:7", "btn2:1.l", "green", [ "v-28.8", "h134.4", "v48" ] ],
[ "sr1:Q0", "sevseg1:A", "green", [ "v-114", "h-220.8" ] ],
[ "sr1:Q1", "sevseg1:B", "green", [ "v9.6", "h-19.2", "v-144", "h-153.6", "v28.8" ] ],
[ "sevseg1:C", "sr1:Q2", "green", [ "v56.76", "h9.6" ] ],
[ "sr1:Q3", "sevseg1:D", "green", [ "v28.8", "h-211.2" ] ],
[ "sevseg1:E", "sr1:Q4", "green", [ "v75.96", "h240.01" ] ],
[ "sevseg1:F", "sr1:Q5", "green", [ "v-48", "h172.8", "v201.6", "h57.6" ] ],
[ "sevseg1:G", "sr1:Q6", "green", [ "v95.16", "h230.4" ] ],
[ "sr1:Q7", "sevseg1:DP", "green", [ "v67.2", "h-249.6" ] ],
[ "sr1:GND", "sr1:OE", "black", [ "v9.6", "h28.8", "v-67.2", "h-67.2" ] ],
[ "gnd1:GND", "sr1:GND", "black", [ "v-19.2", "h-38.4", "v163.2", "h-28.8" ] ],
[ "nano:GND.1", "gnd1:GND", "black", [ "v28.8", "h153.6", "v-220.8", "h57.6", "v-48" ] ],
[ "nano:5V", "sr1:VCC", "red", [ "v38.4", "h182.4", "v-288", "h-134.4" ] ],
[ "nano:5V", "sr1:MR", "red", [ "v38.4", "h182.4", "v-268.8", "h-96" ] ],
[ "nano:10", "sr1:STCP", "yellow", [ "v-124.8", "h57.6", "v-76.8", "h48" ] ],
[ "nano:11", "sr1:SHCP", "yellow", [ "v-134.4", "h57.6", "v-76.8", "h115.2" ] ],
[ "sevseg1:DIG1", "nano:6", "blue", [ "v-38.4", "h-76.79", "v249.6", "h220.8" ] ],
[ "nano:5", "sevseg1:DIG2", "blue", [ "v-28.8", "h-240", "v-220.8", "h115.2" ] ],
[ "nano:4", "sevseg1:DIG3", "blue", [ "v-38.4", "h-259.2", "v-249.6", "h134.4" ] ],
[ "nano:3", "sevseg1:DIG4", "blue", [ "v-48", "h-115.2" ] ],
[ "nano:12", "sr1:DS", "yellow", [ "v-144", "h57.6", "v-28.8", "h96" ] ],
[ "nano:GND.1", "btn3:2.r", "black", [ "v28.8", "h153.6", "v-105.6" ] ],
[ "nano:9", "btn3:1.l", "green", [ "v-48", "h163.2" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for temperature control system using arduino | #include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
#include <Encoder.h>
// Pin Definitions
#define BLUE 7 // Start indecator Blue LED
#define RED 12 // Holding high temp indecator Red LED
#define YELOW 8 // Holding LOW temp indecator Yelow LED
#define DS18B20_PIN 2 // DS18B20 connected to digital pin 2
#define RELAY_PIN 6 // Relay to control the heater
#define STOP_BUTTON_PIN 9 // Stop button
#define ENCODER_PIN_A 3 // Rotary encoder A
#define ENCODER_PIN_B 4 // Rotary encoder B
#define ENCODER_BUTTON 5 // Rotary encoder button
// Initialize LCD (I2C address 0x27 for a 20x4 display)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Initialize OneWire and DS18B20
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
// Initialize RTC
RTC_DS1307 rtc;
// Initialize rotary encoder
Encoder encoder(ENCODER_PIN_A, ENCODER_PIN_B);
// Variables
float currentTemperature = 0.0;
float highTemp = 0.0; // Default high temperature
float lowTemp = 0.0; // Default low temperature
int holdTime = 0; // Default hold time in minutes
bool heatingStarted = false;
bool holdTimeTracking = false;
bool lowTempTracking = false;
bool heating = false;
bool tempering = false;
bool keep_low_temp = false;
DateTime holdStartTime;
DateTime lowTempStartTime;
// Button State Tracking for Debouncing
bool lastEncoderButtonState = HIGH;
bool lastStopButtonState = HIGH;
// Encoder menu states
int state = 0; // 0 = Set High Temp, 1 = Set Hold Time, 2 = Set Low Temp, 3 = Ready to Start
void setup() {
// Initialize serial monitor
Serial.begin(9600);
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize DS18B20
sensors.begin();
// Initialize RTC
if (!rtc.begin()) {
lcd.setCursor(0, 0);
lcd.print("RTC Not Found!");
while (1);
}
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set to compile time
}
// Initialize pins
pinMode(RED, OUTPUT);
pinMode(YELOW, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
pinMode(ENCODER_BUTTON, INPUT_PULLUP);
// Ensure heater is off at startup
digitalWrite(RELAY_PIN, LOW);
// Welcome Message
lcd.setCursor(6, 1);
lcd.print("STARTING");
lcd.setCursor(7, 2);
lcd.print("SYSTEM");
delay(2000);
lcd.clear();
}
void loop() {
// Request temperature from DS18B20
sensors.requestTemperatures();
currentTemperature = sensors.getTempCByIndex(0);
// Read rotary encoder position
long encoderPosition = encoder.read() / 4;
// Read buttons
bool encoderButtonPressed = digitalRead(ENCODER_BUTTON) == LOW;
bool stopButtonPressed = digitalRead(STOP_BUTTON_PIN) == LOW;
// Handle encoder button press
if (encoderButtonPressed && lastEncoderButtonState == HIGH) {
delay(50); // Debounce delay
if (digitalRead(ENCODER_BUTTON) == LOW) {
state = (state + 1) % 4; // Cycle through menu states
encoder.write(0); // Reset encoder position
lcd.clear();
}
}
lastEncoderButtonState = encoderButtonPressed;
// Handle stop button press
if (stopButtonPressed && lastStopButtonState == HIGH) {
delay(50); // Debounce delay
if (digitalRead(STOP_BUTTON_PIN) == LOW) {
heatingStarted = false;
holdTimeTracking = false;
lowTempTracking = false;
digitalWrite(RELAY_PIN, LOW); // Turn off heater
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Process Stopped");
lcd.setCursor(6, 1);
lcd.print("Restart");
lcd.setCursor(6, 2);
lcd.print(".......");
delay(200);
lcd.clear();
return; // Exit loop to ensure heating is off
}
}
lastStopButtonState = stopButtonPressed;
// Update LCD
updateLCD(encoderPosition);
// Start heating process if in "Ready to Start" state
if (state == 3 && !heatingStarted) {
heatingStarted = true;
heating = true;
lcd.clear();
lcd.setCursor(7, 1);
lcd.print("Start");
lcd.setCursor(6, 2);
lcd.print("Heating.... ");
delay(3000);
lcd.clear();
digitalWrite(RELAY_PIN, HIGH); // Turn on heater
lcd.setCursor(0, 2);
lcd.print("Go to High ");
}
// Control heating and time tracking
if (heatingStarted) {
controlHeating();
}
delay(50); // Short delay for stability
}
void updateLCD(long encoderPosition) {
lcd.setCursor(4, 0);
lcd.print("Temp: ");
lcd.print(currentTemperature);
lcd.print(" C ");
if (state == 0) { // Set High Temperature
highTemp = constrain(0 + encoderPosition * 5, 0, 400); // Adjust high temperature
lcd.setCursor(0, 2);
lcd.print("Set High Temp:");
lcd.setCursor(0, 3);
//lcd.print("High Temp: ");
lcd.print(highTemp);
lcd.print(" C ");
} else if (state == 1) { // Set Hold Time
holdTime = constrain(1 + encoderPosition , 0, 240); // Adjust hold time
lcd.setCursor(0, 2);
lcd.print("Set Hold Time: ");
lcd.setCursor(0, 3);
//lcd.print("Hold Time: ");
if(holdTime>60) {
lcd.print(holdTime/60);
lcd.print("h ");
lcd.print(holdTime%60);
lcd.print("min");
} else {
lcd.print(holdTime);
lcd.print(" min ");
}
} else if (state == 2) { // Set Low Temperature
lowTemp = constrain(0 + encoderPosition * 5, 0, highTemp - 1); // Adjust low temperature
lcd.setCursor(0, 2);
lcd.print("Set Low Temp:");
lcd.setCursor(0, 3);
//lcd.print("Low Temp: ");
lcd.print(lowTemp);
lcd.print(" C ");
} else if (state == 3) { // Ready to Start
lcd.setCursor(0, 1);
lcd.print("Starting ");
}
}
void controlHeating() {
if(heating && currentTemperature < highTemp) {
//continue heating to high temperature
} else if(heating && currentTemperature >= (highTemp+1)) {
heating = false;
tempering = true;
digitalWrite(RELAY_PIN, LOW); // Turn off heater
lcd.setCursor(0, 2);
lcd.print("Reached High ");
holdStartTime = rtc.now(); // Start hold time tracking
holdTimeTracking = true;
digitalWrite(RED, HIGH);
} else if(tempering) {
DateTime now = rtc.now();
TimeSpan elapsed = now - holdStartTime;
if(elapsed.minutes() >= holdTime) {
digitalWrite(RELAY_PIN, LOW); // Turn off heater
holdTimeTracking = false;
digitalWrite(RED, LOW);
tempering = false;
keep_low_temp = true;
lcd.setCursor(0, 3);
lcd.print("Time Over H ");
} else {
lcd.setCursor(0, 3);
lcd.print("Hold H temp: ");
lcd.print(elapsed.minutes());
lcd.print("m ");
lcd.print(elapsed.seconds());
lcd.print("s ");
if(currentTemperature >= (highTemp+1)) {
digitalWrite(RELAY_PIN, LOW); // Turn off heater
} else if(currentTemperature < (highTemp-1)) {
digitalWrite(RELAY_PIN, HIGH); // Turn on heater
}
}
} else if(keep_low_temp) {
if(currentTemperature>(lowTemp-3) && currentTemperature<(lowTemp+3) && !lowTempTracking) {
lowTempTracking = true;
digitalWrite(YELOW, HIGH);
holdStartTime = rtc.now(); // Start hold time tracking (Low)
} else if(lowTempTracking) {
DateTime now = rtc.now();
TimeSpan elapsed = now - holdStartTime;
lcd.setCursor(0, 3);
lcd.print("Hold L temp: ");
lcd.print(elapsed.minutes());
lcd.print("m ");
lcd.print(elapsed.seconds());
lcd.print("s ");
if(currentTemperature >= (lowTemp+1)) {
digitalWrite(RELAY_PIN, LOW); // Turn off heater
} else if(currentTemperature < (lowTemp-1)) {
digitalWrite(RELAY_PIN, HIGH); // Turn on heater
}
}
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{ "type": "board-ds18b20", "id": "temp1", "top": 75.67, "left": -159.12, "attrs": {} },
{
"type": "wokwi-lcd2004",
"id": "lcd1",
"top": 6.4,
"left": 322.4,
"attrs": { "pins": "i2c" }
},
{ "type": "wokwi-ky-040", "id": "encoder1", "top": -151.9, "left": -144.8, "attrs": {} },
{ "type": "wokwi-relay-module", "id": "relay1", "top": -163, "left": 460.8, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 73.2,
"left": 723.8,
"attrs": { "color": "limegreen" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": -15.8,
"left": 728.75,
"rotate": 270,
"attrs": { "value": "450000" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 227,
"left": 576,
"attrs": { "color": "red", "key": "2" }
},
{ "type": "wokwi-ds1307", "id": "rtc1", "top": 359.4, "left": 268.9, "attrs": {} },
{ "type": "wokwi-microsd-card", "id": "sd1", "top": 355.43, "left": -86.33, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 167.15,
"left": -124.8,
"attrs": { "value": "5000" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 140.4,
"left": 752.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": 140.4,
"left": 791,
"attrs": { "color": "orange" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": -70.8,
"left": 320.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 216,
"left": 748.25,
"rotate": 90,
"attrs": { "value": "450000" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 216,
"left": 786.65,
"rotate": 90,
"attrs": { "value": "450000" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": -53.35,
"left": 382.6,
"rotate": 180,
"attrs": { "value": "450000" }
}
],
"connections": [
[ "temp1:VCC", "uno:5V", "red", [ "v124.8", "h294.4" ] ],
[ "uno:GND.2", "temp1:GND", "black", [ "v38.9", "h-236.7" ] ],
[ "lcd1:GND", "uno:GND.1", "black", [ "h-48", "v-67.2", "h-163.2" ] ],
[ "lcd1:VCC", "uno:5V", "red", [ "h-48", "v172.9", "h-124.8" ] ],
[ "lcd1:SDA", "uno:A4", "green", [ "h-38.4", "v182.6", "h-38.4" ] ],
[ "lcd1:SCL", "uno:A5", "green", [ "h-19.2", "v182.7", "h-38.4" ] ],
[ "encoder1:GND", "uno:GND.1", "black", [ "h0" ] ],
[ "encoder1:VCC", "uno:5V", "red", [ "h48", "v58", "h-28.8", "v278.4", "h172.8" ] ],
[ "encoder1:SW", "uno:5", "green", [ "h0" ] ],
[ "relay1:VCC", "uno:5V", "red", [ "h-163.2", "v412.8", "h-137.6" ] ],
[ "relay1:GND", "uno:GND.1", "black", [ "h-201.6", "v86", "h-124.8" ] ],
[ "relay1:COM", "led1:C", "green", [ "h49.2", "v46.6", "h96", "v201.6" ] ],
[ "r2:1", "led1:A", "green", [ "v0" ] ],
[ "relay1:NO", "uno:GND.1", "green", [ "h30", "v55.8", "h-489.6", "v9.6" ] ],
[ "relay1:IN", "uno:6", "green", [ "h0" ] ],
[ "rtc1:5V", "uno:5V", "red", [ "h0" ] ],
[ "rtc1:GND", "uno:GND.2", "black", [ "h0" ] ],
[ "rtc1:SDA", "uno:A4", "green", [ "h0" ] ],
[ "rtc1:SCL", "uno:A5", "green", [ "h0" ] ],
[ "btn2:1.r", "uno:9", "green", [ "v0", "h57.8", "v-259.2", "h-537.6" ] ],
[ "temp1:DQ", "uno:2", "green", [ "v19.2", "h86.4", "v-182.4", "h307.2" ] ],
[ "r1:1", "temp1:DQ", "green", [ "v-19.2", "h-19.2" ] ],
[ "r1:2", "temp1:VCC", "green", [ "v0" ] ],
[ "uno:3", "encoder1:DT", "green", [ "v0" ] ],
[ "uno:4", "encoder1:CLK", "green", [ "v0" ] ],
[ "uno:GND.3", "btn2:2.l", "black", [ "v0" ] ],
[ "led2:A", "r3:1", "green", [ "v0" ] ],
[ "r3:2", "uno:12", "green", [ "h0", "v27.6", "h-460.8", "v-364.8", "h-153.6" ] ],
[ "led2:C", "uno:GND.3", "green", [ "v86.4", "h-95.6", "v38.4", "h-9.6" ] ],
[ "r4:1", "led3:A", "green", [ "h0" ] ],
[ "led3:C", "led2:C", "green", [ "v9.6", "h-18.8", "v-48", "h-28.8", "v38.4" ] ],
[ "r4:2", "uno:8", "green", [ "h0", "v75.6", "h-844.8", "v-403.2", "h211.2" ] ],
[ "led4:C", "uno:7", "green", [ "v0", "h-9.2", "v-48", "h-134.4" ] ],
[ "r5:2", "led4:A", "green", [ "v0", "h-8.4", "v19.2" ] ],
[ "r5:1", "lcd1:VCC", "green", [ "v0", "h9.6", "v19.2", "h-67.2", "v28.8", "h-96", "v48" ] ],
[ "relay1:VCC", "r2:2", "red", [ "h-9.6", "v-28.8", "h182.4", "v67.2", "h124.8" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Gost28147 | #include <Arduino.h>
// Default S-Box (GOST 28147-89 standard)
uint8_t S_BOX[8][16] = {
{4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3},
{14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9},
{5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11},
{7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3},
{6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2},
{4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14},
{13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12},
{1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12}
};
// R function with S-Box substitution
uint32_t R(uint32_t x) {
uint32_t result = 0;
for (int i = 0; i < 8; i++) {
uint8_t part = (x >> (4 * (7 - i))) & 0xF;
result |= ((uint32_t)S_BOX[i][part]) << (4 * (7 - i));
}
result = (result << 11) | (result >> (32 - 11));
return result;
}
// Encrypt a single block (64 bits)
void gost_encrypt(uint32_t *block, uint32_t *key) {
uint32_t n1 = block[0];
uint32_t n2 = block[1];
for (int i = 0; i < 24; i++) {
uint32_t temp = n1;
n1 = n2 ^ R(n1 + key[i % 8]);
n2 = temp;
}
for (int i = 7; i >= 0; i--) {
uint32_t temp = n1;
n1 = n2 ^ R(n1 + key[i]);
n2 = temp;
}
block[0] = n1;
block[1] = n2;
}
// Decrypt a single block (64 bits)
void gost_decrypt(uint32_t *block, uint32_t *key) {
uint32_t n1 = block[1];
uint32_t n2 = block[0];
for (int i = 0; i < 8; i++) {
uint32_t temp = n1;
n1 = n2 ^ R(n1 + key[i]);
n2 = temp;
}
for (int i = 23; i >= 0; i--) {
uint32_t temp = n1;
n1 = n2 ^ R(n1 + key[i % 8]);
n2 = temp;
}
block[0] = n2;
block[1] = n1;
}
// Add PKCS#7 padding
void add_padding(uint8_t *data, size_t &length) {
size_t padding = 8 - (length % 8);
for (size_t i = 0; i < padding; i++) {
data[length + i] = padding;
}
length += padding;
}
// Remove PKCS#7 padding
void remove_padding(uint8_t *data, size_t &length) {
uint8_t padding = data[length - 1];
if (padding > 0 && padding <= 8) {
length -= padding;
}
}
void gost_encrypt_with_padding(uint8_t *data, size_t &length, uint32_t *key) {
// To‘ldirish qo‘shish
add_padding(data, length);
// Har bir blokni shifrlash
for (size_t i = 0; i < length; i += 8) {
gost_encrypt((uint32_t *)&data[i], key);
}
}
void gost_decrypt_with_padding(uint8_t *data, size_t &length, uint32_t *key) {
// Har bir blokni deshifrlash
for (size_t i = 0; i < length; i += 8) {
gost_decrypt((uint32_t *)&data[i], key);
}
// To‘ldirishni olib tashlash
remove_padding(data, length);
}
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
String inputData = command.substring(8);
size_t length = inputData.length();
uint8_t data[1024];
inputData.getBytes(data, sizeof(data));
uint32_t key[8] = {0x12345678, 0x23456789, 0x34567890, 0x45678901,
0x56789012, 0x67890123, 0x78901234, 0x89012345};
if (command.startsWith("ENCRYPT:")) {
gost_encrypt_with_padding(data, length, key);
for (size_t i = 0; i < length; i++) {
if(data[i] < 16){
Serial.print("0");
Serial.print(data[i], HEX);
}else{
Serial.print(data[i], HEX);
}
}
} else if (command.startsWith("DECRYPT:")) {
size_t length1 = length / 2; // Har ikkita HEX belgidan bitta byte hosil bo'ladi
uint8_t data[length1]; // uint8_t massiv
// HEX dan uint8_t ga o'zgartirish
for (size_t i = 0; i < length1; i++) {
String byteString = inputData.substring(i * 2, i * 2 + 2); // 2 ta HEX belgi olish
data[i] = (uint8_t) strtoul(byteString.c_str(), NULL, 16); // HEX ni uint8_t ga aylantirish
}
gost_decrypt_with_padding(data, length1, key);
for (size_t i = 0; i < length1; i++) {
Serial.print((char)data[i]);
}
} else {
Serial.println("Unknown command!");
}
}
} | {
"version": 1,
"author": "Programmer GPT",
"editor": "wokwi",
"parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} } ],
"connections": [],
"dependencies": {}
} |
Generate a diagram.json code for Three LED blink in series | #define RED_LED 7
#define GREEN_LED 6
#define BLUE_LED 5
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
}
void loop() {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
delay(1000);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
delay(1000);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
delay(1000);
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -12.6, "left": 300.4, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": 37.8, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -3.6, "left": 339.8, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led2",
"top": -3.6,
"left": 435.8,
"attrs": { "color": "limegreen" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -3.6,
"left": 531.8,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 81.6,
"left": 335.45,
"rotate": 90,
"attrs": { "value": "0" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 81.6,
"left": 431.45,
"rotate": 90,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 81.6,
"left": 527.45,
"rotate": 90,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "led1:C", "bb1:4t.b", "green", [ "v9.6", "h0.4" ] ],
[ "led2:C", "bb1:14t.b", "green", [ "v9.6", "h0.4" ] ],
[ "led2:A", "bb1:15t.b", "green", [ "v9.6" ] ],
[ "led3:C", "bb1:24t.b", "green", [ "v9.6", "h0.4" ] ],
[ "led3:A", "bb1:25t.b", "green", [ "v9.6" ] ],
[ "led1:A", "bb1:5t.a", "", [ "$bb" ] ],
[ "led1:C", "bb1:4t.a", "", [ "$bb" ] ],
[ "led2:A", "bb1:15t.a", "", [ "$bb" ] ],
[ "led2:C", "bb1:14t.a", "", [ "$bb" ] ],
[ "led3:A", "bb1:25t.a", "", [ "$bb" ] ],
[ "led3:C", "bb1:24t.a", "", [ "$bb" ] ],
[ "bb1:5t.b", "led1:A", "green", [ "v0" ] ],
[ "r1:1", "bb1:5t.c", "", [ "$bb" ] ],
[ "r1:2", "bb1:5b.g", "", [ "$bb" ] ],
[ "r2:1", "bb1:15t.c", "", [ "$bb" ] ],
[ "r2:2", "bb1:15b.g", "", [ "$bb" ] ],
[ "r3:1", "bb1:25t.c", "", [ "$bb" ] ],
[ "r3:2", "bb1:25b.g", "", [ "$bb" ] ],
[ "r1:2", "uno:7", "red", [ "h-38.4", "v-164.4", "h-105.6", "v19.2" ] ],
[ "r2:2", "uno:6", "green", [ "h19.2", "v-174", "h-240", "v38.4" ] ],
[ "r3:2", "uno:5", "blue", [ "h19.2", "v-202.8", "h0", "v0", "h-316.8", "v67.2" ] ],
[ "bb1:4t.d", "bb1:bn.3", "purple", [ "v115.2" ] ],
[ "bb1:14t.d", "bb1:bn.11", "purple", [ "v115.2" ] ],
[ "bb1:24t.d", "bb1:bn.20", "purple", [ "v115.2" ] ],
[ "bb1:bp.1", "uno:5V", "red", [ "v47.1", "h-132.8" ] ],
[ "bb1:bn.2", "uno:GND.3", "green", [ "v27.5", "h-123.2" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for piano using arduino | #include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[]={12,11,10,9,8,7,6,5,4};
const int buttonTones[]={
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones= sizeof(buttonPins) / sizeof (buttonPins[0]);
void setup() {
int pitch=0;// put your setup code here, to run once:
for (uint8_t i=0; i< numTones;i++){
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch=0;
for (uint8_t i=0; i < numTones; i++){
if (digitalRead(buttonPins[i])== LOW){
pitch = buttonTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
}
else {
tone(SPEAKER_PIN,0);
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -55.2,
"left": 366.6,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": -96.1,
"left": 233.9,
"rotate": 90,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": -105.7,
"left": 185.9,
"rotate": 90,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-pushbutton",
"id": "btn4",
"top": -105.7,
"left": 137.9,
"rotate": 90,
"attrs": { "color": "green", "bounce": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn5",
"top": -115.3,
"left": 89.9,
"rotate": 90,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton",
"id": "btn8",
"top": -115.3,
"left": -63.7,
"rotate": 90,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -96.1,
"left": 291.5,
"rotate": 90,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton",
"id": "btn6",
"top": -124.9,
"left": -6.1,
"rotate": 90,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton",
"id": "btn7",
"top": -144.1,
"left": 41.9,
"rotate": 90,
"attrs": { "color": "blue" }
}
],
"connections": [
[ "btn1:1.r", "uno:4", "red", [ "v0" ] ],
[ "btn2:1.r", "uno:5", "orange", [ "v0" ] ],
[ "btn3:1.r", "uno:6", "gold", [ "v0" ] ],
[ "btn5:1.r", "uno:9", "cyan", [ "v0" ] ],
[ "btn8:1.r", "uno:12", "red", [ "v0" ] ],
[ "uno:GND.1", "btn5:2.r", "black", [ "v-76.2", "h-0.3" ] ],
[ "uno:GND.1", "btn7:2.r", "black", [ "v0" ] ],
[ "uno:GND.1", "btn6:2.r", "black", [ "v0" ] ],
[ "uno:GND.1", "btn8:2.r", "black", [ "v0" ] ],
[ "uno:GND.1", "btn4:2.r", "black", [ "v0" ] ],
[ "uno:GND.1", "btn3:2.r", "black", [ "v0" ] ],
[ "uno:GND.1", "btn2:2.r", "black", [ "v0" ] ],
[ "uno:GND.1", "btn1:2.r", "black", [ "v0" ] ],
[ "btn6:1.r", "uno:11", "purple", [ "v0" ] ],
[ "btn7:1.r", "uno:10", "blue", [ "v0" ] ],
[ "uno:GND.1", "bz1:1", "black", [ "v0" ] ],
[ "bz1:2", "uno:8", "green", [ "v0" ] ],
[ "uno:7", "btn4:1.r", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for sequential LED tower | void setup() {
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, INPUT_PULLUP);
// put your setup code here, to run once:
}
void loop() {
int pb1=digitalRead(2);
if(pb1==LOW)
{
digitalWrite(4, HIGH);
digitalWrite(10, HIGH);
delay(250);
digitalWrite(4, LOW);
digitalWrite(10, LOW);
digitalWrite(3, HIGH);
digitalWrite(9, HIGH);
delay(250);
digitalWrite(3, LOW);
digitalWrite(9, LOW);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(250);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(250);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(250);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(250);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
delay(250);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
delay(250);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(250);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(250);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
delay(250);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
// put your main code here, to run repeatedly:
} | {
"version": 1,
"author": "Mas Wan",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": -0.6, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -61,
"left": 19.2,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": -118.8,
"left": 128.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -118.8,
"left": 176.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -214.8,
"left": 128.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": -166.8,
"left": 128.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": -214.8,
"left": 176.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": -166.8,
"left": 176.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led7",
"top": -262.8,
"left": 128.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led8",
"top": -262.8,
"left": 176.6,
"attrs": { "color": "red" }
}
],
"connections": [
[ "uno:GND.1", "btn1:2.r", "limegreen", [ "v0" ] ],
[ "uno:2", "btn1:1.r", "green", [ "v0" ] ],
[ "uno:GND.1", "led1:C", "black", [ "v0" ] ],
[ "uno:GND.1", "led2:C", "limegreen", [ "v0" ] ],
[ "uno:3", "led1:A", "red", [ "v0" ] ],
[ "uno:4", "led2:A", "blue", [ "v0" ] ],
[ "uno:5", "led6:A", "red", [ "v0" ] ],
[ "uno:6", "led4:A", "blue", [ "v0" ] ],
[ "uno:GND.1", "led4:C", "black", [ "v0" ] ],
[ "uno:GND.1", "led6:C", "limegreen", [ "v0" ] ],
[ "uno:7", "led3:A", "red", [ "v0" ] ],
[ "uno:8", "led5:A", "blue", [ "v0" ] ],
[ "uno:GND.1", "led3:C", "black", [ "v0" ] ],
[ "uno:GND.1", "led5:C", "limegreen", [ "v0" ] ],
[ "uno:9", "led8:A", "red", [ "v0" ] ],
[ "uno:10", "led7:A", "blue", [ "v0" ] ],
[ "uno:GND.1", "led7:C", "black", [ "v0" ] ],
[ "uno:GND.1", "led8:C", "limegreen", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for modern turn lights motorcycles using arduino | //Modern turn lights v.3.1.0 for scooters and motorcycles with arduino and ws2812b - by Fedaceag Ionut ( Youtube - Think small, build big! )
#include <FastLED.h> //FastLed library version 3.2.1 - https://github.com/FastLED/FastLED/wiki/Overview or http://fastled.io/ with NEOPIXEL or WS2812B
#define NUM_STRIPS 4 // number of small led strips
#define NUM_LEDS_PART_A 20 // number of leds for the big strip - should be the at least the number of leds from the small strips multiply by tree (in this case the minimum should be 10 x 3 = 30 leds)
#define NUM_LEDS_PART_B 4 // number of leds for the small strips
CRGB ledsA[NUM_LEDS_PART_A];
CRGB ledsB[NUM_STRIPS][NUM_LEDS_PART_B];
//input pins
const int buttonPinL = 2; // turn left
const int buttonPinR = 3; // turn right
const int buttonPinEng = 4; // engine on to start day lights
const int buttonPinKnightRider = 5; // knight rider lights
const int buttonPinBrake = 6; // brake lights
const int buttonPinTailLights = 7; // tail lights
int halfOfLeds = (NUM_LEDS_PART_A/2);
int buttonStateL = 0;
int buttonStateR = 0;
int engineOn = 0;
int maxBrtN = 0;
int KnightRiderState = 0;
int KnightRiderToOff = 0;
int Brake = 0;
int TailLights = 0;
int leftLeds = 0;
int rightLeds = 0;
int tailLeft = halfOfLeds-1;
int tailLeftSmall = 0;
int tailRight = halfOfLeds;
int tailRightSmall = 0;
int brakeToOffL = 0;
int brakeToOffR = 0;
int stateLT = 0;
int stateRT = 0;
uint8_t gBrtL = 0;
uint8_t gBrtR = 0;
int TailOn = 0;
unsigned long turnColor = 0xff6a00; //turn signal color
int showSignalBigStrip = 1; // show the signal on the big led strip 0 = no | 1 = yes
int showTailBrakeSmalStrip = 1; // show tail and brake lights on small strips 0 = no | 1 = yes
int maxBrt = 254; // maxim brightness day lights - from 0 to 254
int maxBrtNight = 100; // maxim brightness day lights with tail lights on - from 0 to 254
int maxBrtBrake = 254; // maxim brightness Brake lights - from 0 to 254
int maxBrtTailLights = 70; // maxim brightness tail lights - from 0 to 254
uint8_t gHue = 0; // knight rider color
int knightRiderRainbow = 0; // knight rider rainbow effect 0 = no | 1 = yes
int knightRiderOvrDayL = 1; // knight rider override day lights 0 = no | 1 = yes
int knightRiderSpeed = 45; // speed for knight rider lights
int knightRiderTail = 12; // tail effect for knight rider lights
int delayTurnLedAnim = 8; //delay of each led in turn light animation
int delayTurnLedOff = 250; //delay from animation to black (is used twice)
int delayLedToDayLight = 500; //delay from animation to day light on
int tailDelay = 6; //delay for animation tail lights on or off
int nrAnimAfterOff = 3; //number of animations for a single impulse
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPinL, INPUT);
pinMode(buttonPinR, INPUT);
pinMode(buttonPinEng, INPUT);
pinMode(buttonPinKnightRider, INPUT);
pinMode(buttonPinBrake, INPUT);
pinMode(buttonPinTailLights, INPUT);
FastLED.addLeds<NEOPIXEL, 13>(ledsA, NUM_LEDS_PART_A); //big led strip rear turn signals, brake light, tail lights
FastLED.addLeds<NEOPIXEL, 12>(ledsB[0], NUM_LEDS_PART_B); //led strip rear left turn signals
FastLED.addLeds<NEOPIXEL, 11>(ledsB[1], NUM_LEDS_PART_B); //led strip rear right turn signals
FastLED.addLeds<NEOPIXEL, 10>(ledsB[2], NUM_LEDS_PART_B); //led strip front left turn signals
FastLED.addLeds<NEOPIXEL, 9>(ledsB[3], NUM_LEDS_PART_B); //led strip front right turn signals
attachInterrupt(digitalPinToInterrupt(buttonPinL),btnPressL,RISING); // we use interrupt for instant reaction of turn lights
attachInterrupt(digitalPinToInterrupt(buttonPinR),btnPressR,RISING); // we use interrupt for instant reaction of turn lights
fill_solid(ledsA, NUM_LEDS_PART_A, CRGB::Black); // some led strips are all on at power on, so let's power them off at boot
fill_solid(ledsB[0], NUM_LEDS_PART_B, CRGB::Black); // some led strips are all on at power on, so let's power them off at boot
fill_solid(ledsB[1], NUM_LEDS_PART_B, CRGB::Black); // some led strips are all on at power on, so let's power them off at boot
fill_solid(ledsB[2], NUM_LEDS_PART_B, CRGB::Black); // some led strips are all on at power on, so let's power them off at boot
fill_solid(ledsB[3], NUM_LEDS_PART_B, CRGB::Black); // some led strips are all on at power on, so let's power them off at boot
FastLED.show();
}
void loop() {
// read the input state
buttonStateL = digitalRead(buttonPinL);
buttonStateR = digitalRead(buttonPinR);
engineOn = digitalRead(buttonPinEng);
KnightRiderState = digitalRead(buttonPinKnightRider);
Serial.print(buttonStateL);
Serial.print(", ");
Serial.print(buttonStateR);
Serial.print(", ");
Serial.print(engineOn);
Serial.print(", ");
Serial.println(KnightRiderState);
//function for hazard lights
if(stateLT != 0 && stateRT != 0){
leftLeds = NUM_LEDS_PART_B-1;
rightLeds = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
if(showSignalBigStrip == 1){
ledsA[leftLeds] = turnColor;
ledsA[rightLeds] = turnColor;
}
ledsB[0][dot] = turnColor;
ledsB[1][dot] = turnColor;
ledsB[2][dot] = turnColor;
ledsB[3][dot] = turnColor;
FastLED.show();
//delay(delayTurnLedAnim);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedAnim >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
leftLeds--;
rightLeds++;
}
//delay(delayTurnLedOff);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedOff >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
if(showSignalBigStrip == 1){
leftLeds = NUM_LEDS_PART_B-1;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
ledsA[leftLeds] = 0x000000;
leftLeds--;
}
rightLeds = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
ledsA[rightLeds] = 0x000000;
rightLeds++;
}
}
fill_solid(ledsB[0], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[1], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[2], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[3], NUM_LEDS_PART_B, CRGB::Black);
FastLED.show();
//delay(delayTurnLedOff);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedOff >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
buttonStateL = digitalRead(buttonPinL);
buttonStateR = digitalRead(buttonPinR);
if(buttonStateL != HIGH || buttonStateR != HIGH){
if(buttonStateL == HIGH){
stateLT = 1;
}else{
stateLT = 0;
gBrtL = 0;
}
if(buttonStateR == HIGH){
stateRT = 1;
}else{
stateRT = 0;
gBrtR = 0;
}
if(buttonStateL != HIGH && buttonStateR != HIGH){
//delay(delayLedToDayLight);
currentMillis = previousMillis = millis();
while(previousMillis + delayLedToDayLight >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
leftLeds = NUM_LEDS_PART_B-1;
rightLeds = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
if(showSignalBigStrip == 1){
tailLeft = NUM_LEDS_PART_B;
tailRight = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
}else{
tailLeft = halfOfLeds-1;
tailRight = halfOfLeds;
}
}
}
//function for left turn lights
}else if(stateLT != 0){
if(KnightRiderState == HIGH){
fill_solid(ledsB[1], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[3], NUM_LEDS_PART_B, CRGB::Black);
}
leftLeds = NUM_LEDS_PART_B-1;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
if(showSignalBigStrip == 1){
ledsA[leftLeds] = turnColor;
}
ledsB[0][dot] = turnColor;
ledsB[2][dot] = turnColor;
FastLED.show();
//delay(delayTurnLedAnim);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedAnim >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
leftLeds--;
}
//delay(delayTurnLedOff);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedOff >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
leftLeds = NUM_LEDS_PART_B-1;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
if(showSignalBigStrip == 1){
ledsA[leftLeds] = 0x000000;
}
ledsB[0][dot] = 0x000000;
ledsB[2][dot] = 0x000000;
leftLeds--;
}
FastLED.show();
//delay(delayTurnLedOff);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedOff >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
stateLT++;
if(stateLT >= nrAnimAfterOff && buttonStateL != HIGH){
stateLT = 0;
gBrtL = 0;
//delay(delayLedToDayLight);
currentMillis = previousMillis = millis();
while(previousMillis + delayLedToDayLight >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
leftLeds = NUM_LEDS_PART_B-1;
if(showSignalBigStrip == 1){
tailLeft = NUM_LEDS_PART_B;
tailRight = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
}else{
tailLeft = halfOfLeds-1;
tailRight = halfOfLeds;
}
}
//function for right turn lights
}else if(stateRT != 0){
if(KnightRiderState == HIGH){
fill_solid(ledsB[0], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[2], NUM_LEDS_PART_B, CRGB::Black);
}
rightLeds = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
if(showSignalBigStrip == 1){
ledsA[rightLeds] = turnColor;
}
ledsB[1][dot] = turnColor;
ledsB[3][dot] = turnColor;
FastLED.show();
//delay(delayTurnLedAnim);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedAnim >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
rightLeds++;
}
//delay(delayTurnLedOff);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedOff >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
rightLeds = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
for(int dot = 0; dot < NUM_LEDS_PART_B; dot++) {
if(showSignalBigStrip == 1){
ledsA[rightLeds] = 0x000000;
}
ledsB[1][dot] = 0x000000;
ledsB[3][dot] = 0x000000;
rightLeds++;
}
FastLED.show();
//delay(delayTurnLedOff);
currentMillis = previousMillis = millis();
while(previousMillis + delayTurnLedOff >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
stateRT++;
if(stateRT >= nrAnimAfterOff && buttonStateR != HIGH){
stateRT = 0;
gBrtR = 0;
//delay(delayLedToDayLight);
currentMillis = previousMillis = millis();
while(previousMillis + delayLedToDayLight >= currentMillis){
TailAndBrake();
FastLED.show();
currentMillis = millis();
}
rightLeds = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
if(showSignalBigStrip == 1){
tailLeft = NUM_LEDS_PART_B;
tailRight = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
}else{
tailLeft = halfOfLeds-1;
tailRight = halfOfLeds;
}
}
//function for kight rider and day lights
}else{
if(knightRiderOvrDayL == 1 && KnightRiderState == HIGH && engineOn == HIGH){
engineOn = LOW;
}
if(TailLights == HIGH && engineOn == HIGH){
maxBrtN = maxBrtNight;
}else{
maxBrtN = maxBrt;
}
if(stateLT == 0 && engineOn == HIGH){
if(gBrtL <= maxBrtN){
EVERY_N_MILLISECONDS( 1 ) { gBrtL++; }
fill_solid( ledsB[2], NUM_LEDS_PART_B, CHSV(0,0,gBrtL));
}else if(gBrtL >= maxBrtN){
EVERY_N_MILLISECONDS( 1 ) { gBrtL--; }
fill_solid( ledsB[2], NUM_LEDS_PART_B, CHSV(0,0,gBrtL));
}
}else{
if(gBrtL > 0){
EVERY_N_MILLISECONDS( 1 ) { gBrtL--; }
fill_solid( ledsB[2], NUM_LEDS_PART_B, CHSV(0,0,gBrtL));
}
}
if(stateRT == 0 && engineOn == HIGH){
if(gBrtR <= maxBrtN){
EVERY_N_MILLISECONDS( 1 ) { gBrtR++; }
fill_solid( ledsB[3], NUM_LEDS_PART_B, CHSV(0,0,gBrtR));
}else if(gBrtR >= maxBrtN){
EVERY_N_MILLISECONDS( 1 ) { gBrtR--; }
fill_solid( ledsB[3], NUM_LEDS_PART_B, CHSV(0,0,gBrtR));
}
}else{
if(gBrtR > 0){
EVERY_N_MILLISECONDS( 1 ) { gBrtR--; }
fill_solid( ledsB[3], NUM_LEDS_PART_B, CHSV(0,0,gBrtR));
}
}
if(KnightRiderState == HIGH && engineOn == LOW){
if(knightRiderRainbow == 1){
EVERY_N_MILLISECONDS( 20 ) { gHue++; }
}
fadeToBlackBy( ledsB[0], NUM_LEDS_PART_B, knightRiderTail);
fadeToBlackBy( ledsB[1], NUM_LEDS_PART_B, knightRiderTail);
fadeToBlackBy( ledsB[2], NUM_LEDS_PART_B, knightRiderTail);
fadeToBlackBy( ledsB[3], NUM_LEDS_PART_B, knightRiderTail);
int pos = beatsin16( knightRiderSpeed, 0, NUM_LEDS_PART_B-1 );
ledsB[0][pos] += CHSV( gHue, 255, 192);
ledsB[1][pos] += CHSV( gHue, 255, 192);
ledsB[2][pos] += CHSV( gHue, 255, 192);
ledsB[3][pos] += CHSV( gHue, 255, 192);
KnightRiderToOff = 1;
}else{
if(KnightRiderToOff == 1){
fill_solid(ledsB[0], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[1], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[2], NUM_LEDS_PART_B, CRGB::Black);
fill_solid(ledsB[3], NUM_LEDS_PART_B, CRGB::Black);
if(showSignalBigStrip == 1){
tailLeft = NUM_LEDS_PART_B;
tailRight = NUM_LEDS_PART_A-NUM_LEDS_PART_B;
}else{
tailLeft = halfOfLeds-1;
tailRight = halfOfLeds;
}
KnightRiderToOff = 0;
}
}
TailAndBrake();
FastLED.show();
}
}
//function for Brake lights, tail lights
void TailAndBrake(){
Brake = digitalRead(buttonPinBrake);
TailLights = digitalRead(buttonPinTailLights);
if(TailLights == HIGH && Brake == LOW){
if(stateLT == 0 && stateRT == 0){
if(tailLeft >= 0){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailLeft]=CHSV(0,255,maxBrtTailLights);
if(showTailBrakeSmalStrip == 1 && tailLeft < NUM_LEDS_PART_B){
tailLeftSmall = map(tailLeft,NUM_LEDS_PART_B-1,0,0,NUM_LEDS_PART_B-1);
ledsB[0][tailLeftSmall]=CHSV(0,255,maxBrtTailLights);
}
tailLeft--;
}
}
if(tailRight < NUM_LEDS_PART_A){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailRight]=CHSV(0,255,maxBrtTailLights);
if(showTailBrakeSmalStrip == 1 && tailRight > NUM_LEDS_PART_A-NUM_LEDS_PART_B-1){
tailRightSmall = map(tailRight,NUM_LEDS_PART_A-NUM_LEDS_PART_B-1,NUM_LEDS_PART_A-1,0,NUM_LEDS_PART_B-1);
ledsB[1][tailRightSmall]=CHSV(0,255,maxBrtTailLights);
}
tailRight++;
}
}
}else if(stateLT != 0 && stateRT != 0){
if(showSignalBigStrip == 1){
if(tailLeft >= NUM_LEDS_PART_B){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailLeft]=CHSV(0,255,maxBrtTailLights);
tailLeft--;
}
}
if(tailRight < NUM_LEDS_PART_A-NUM_LEDS_PART_B){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailRight]=CHSV(0,255,maxBrtTailLights);
tailRight++;
}
}
}else{
if(tailLeft >= 0){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailLeft]=CHSV(0,255,maxBrtTailLights);
tailLeft--;
}
}
if(tailRight < NUM_LEDS_PART_A){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailRight]=CHSV(0,255,maxBrtTailLights);
tailRight++;
}
}
}
}else{
if(stateLT != 0){
if(showSignalBigStrip == 1){
if(tailLeft >= NUM_LEDS_PART_B){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailLeft]=CHSV(0,255,maxBrtTailLights);
tailLeft--;
}
}
}else{
if(tailLeft >= 0){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailLeft]=CHSV(0,255,maxBrtTailLights);
tailLeft--;
}
}
}
if(tailRight < NUM_LEDS_PART_A){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailRight]=CHSV(0,255,maxBrtTailLights);
if(showTailBrakeSmalStrip == 1 && tailRight > NUM_LEDS_PART_A-NUM_LEDS_PART_B-1){
tailRightSmall = map(tailRight,NUM_LEDS_PART_A-NUM_LEDS_PART_B-1,NUM_LEDS_PART_A-1,0,NUM_LEDS_PART_B-1);
ledsB[1][tailRightSmall]=CHSV(0,255,maxBrtTailLights);
}
tailRight++;
}
}
}
if(stateRT != 0){
if(tailLeft >= 0){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailLeft]=CHSV(0,255,maxBrtTailLights);
if(showTailBrakeSmalStrip == 1 && tailLeft < NUM_LEDS_PART_B){
tailLeftSmall = map(tailLeft,NUM_LEDS_PART_B-1,0,0,NUM_LEDS_PART_B-1);
ledsB[0][tailLeftSmall]=CHSV(0,255,maxBrtTailLights);
}
tailLeft--;
}
}
if(showSignalBigStrip == 1){
if(tailRight < NUM_LEDS_PART_A-NUM_LEDS_PART_B){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailRight]=CHSV(0,255,maxBrtTailLights);
tailRight++;
}
}
}else{
if(tailRight < NUM_LEDS_PART_A){
EVERY_N_MILLISECONDS( tailDelay ) {
ledsA[tailRight]=CHSV(0,255,maxBrtTailLights);
tailRight++;
}
}
}
}
}
}else if(Brake == HIGH){
if(stateLT == 0 && stateRT == 0){
fill_solid( ledsA, NUM_LEDS_PART_A, CHSV(0,255,maxBrtBrake));
if(showTailBrakeSmalStrip == 1){
fill_solid( ledsB[0], NUM_LEDS_PART_B, CHSV(0,255,maxBrtBrake));
fill_solid( ledsB[1], NUM_LEDS_PART_B, CHSV(0,255,maxBrtBrake));
}
}else if(stateLT != 0 && stateRT != 0){
if(showSignalBigStrip == 1){
for(int i = NUM_LEDS_PART_B; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
for(int i = halfOfLeds; i < NUM_LEDS_PART_A-NUM_LEDS_PART_B; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
}else{
for(int i = 0; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
for(int i = halfOfLeds; i < NUM_LEDS_PART_A; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
}
}else{
if(stateLT != 0){
if(showSignalBigStrip == 1){
for(int i = NUM_LEDS_PART_B; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
for(int i = halfOfLeds; i < NUM_LEDS_PART_A; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
}else{
for(int i = 0; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
for(int i = halfOfLeds; i < NUM_LEDS_PART_A; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
}
if(showTailBrakeSmalStrip == 1){
fill_solid( ledsB[1], NUM_LEDS_PART_B, CHSV(0,255,maxBrtBrake));
}
}
if(stateRT != 0){
if(showSignalBigStrip == 1){
for(int i = halfOfLeds; i < NUM_LEDS_PART_A-NUM_LEDS_PART_B; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
for(int i = 0; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
}else{
for(int i = halfOfLeds; i < NUM_LEDS_PART_A; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
for(int i = 0; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,255,maxBrtBrake);
}
}
if(showTailBrakeSmalStrip == 1){
fill_solid( ledsB[0], NUM_LEDS_PART_B, CHSV(0,255,maxBrtBrake));
}
}
}
tailLeft = halfOfLeds-1;
tailRight = halfOfLeds;
brakeToOffL = 1;
brakeToOffR = 1;
}else{
if(tailLeft < halfOfLeds-1){
EVERY_N_MILLISECONDS( tailDelay ) {
tailLeft++;
ledsA[tailLeft]=CHSV(0,0,0);
if(showTailBrakeSmalStrip == 1 && tailLeft < NUM_LEDS_PART_B){
tailLeftSmall = map(tailLeft,NUM_LEDS_PART_B-1,0,0,NUM_LEDS_PART_B-1);
ledsB[0][tailLeftSmall]=CHSV(0,0,0);
}
}
}else{
if(stateLT != 0){
if(brakeToOffL == 1){
if(showSignalBigStrip == 1){
for(int i = NUM_LEDS_PART_B; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,0,0);
}
}else{
for(int i = 0; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,0,0);
}
}
brakeToOffL = 0;
}
}else{
for(int i = 0; i < halfOfLeds; i++){
ledsA[i] = CHSV(0,0,0);
}
if(KnightRiderState == LOW){
fill_solid( ledsB[0], NUM_LEDS_PART_B, CHSV(0,0,0));
}
}
}
if(tailRight > halfOfLeds){
EVERY_N_MILLISECONDS( tailDelay ) {
tailRight--;
ledsA[tailRight]=CHSV(0,0,0);
if(showTailBrakeSmalStrip == 1 && tailRight > NUM_LEDS_PART_A-NUM_LEDS_PART_B-1){
tailRightSmall = map(tailRight,NUM_LEDS_PART_A-NUM_LEDS_PART_B-1,NUM_LEDS_PART_A-1,0,NUM_LEDS_PART_B-1);
ledsB[1][tailRightSmall]=CHSV(0,0,0);
}
}
}else{
if(stateRT != 0){
if(brakeToOffR == 1){
if(showSignalBigStrip == 1){
for(int i = halfOfLeds; i < NUM_LEDS_PART_A-NUM_LEDS_PART_B; i++){
ledsA[i] = CHSV(0,0,0);
}
}else{
for(int i = halfOfLeds; i < NUM_LEDS_PART_A; i++){
ledsA[i] = CHSV(0,0,0);
}
}
brakeToOffR = 0;
}
}else{
for(int i = halfOfLeds; i < halfOfLeds*2; i++){
ledsA[i] = CHSV(0,0,0);
}
if(KnightRiderState == LOW){
fill_solid( ledsB[1], NUM_LEDS_PART_B, CHSV(0,0,0));
}
}
}
}
}
//function for left signal interrupt
void btnPressL(){
stateLT = 1;
for(int nr = 0; nr < 1500; nr++) {
buttonStateR = digitalRead(buttonPinR);
if(buttonStateR == 1){
stateRT = 1;
}
}
}
//function for right signal interrupt
void btnPressR(){
stateRT = 1;
for(int nr = 0; nr < 1500; nr++) {
buttonStateL = digitalRead(buttonPinL);
if(buttonStateL == 1){
stateLT = 1;
}
}
} | {
"version": 1,
"author": "Usama Shafiq",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -214.2, "left": 156.4, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": -0.6, "attrs": {} },
{
"type": "wokwi-led-ring",
"id": "ring1",
"top": -513.02,
"left": 27.56,
"attrs": { "pixels": "20" }
},
{
"type": "wokwi-led-ring",
"id": "ring3",
"top": -339.96,
"left": -68.31,
"attrs": { "pixels": "4" }
},
{
"type": "wokwi-led-ring",
"id": "ring4",
"top": -330.36,
"left": 210.09,
"attrs": { "pixels": "4" }
},
{
"type": "wokwi-led-ring",
"id": "ring5",
"top": -368.76,
"left": 152.49,
"attrs": { "pixels": "4" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": -52.8,
"left": 201.05,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": -52.8,
"left": 191.45,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": -52.8,
"left": 181.85,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": -52.8,
"left": 172.25,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": -52.8,
"left": 162.65,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": -52.8,
"left": 153.05,
"rotate": 90,
"attrs": { "value": "10000" }
},
{ "type": "wokwi-dip-switch-8", "id": "sw1", "top": -147.3, "left": 174.3, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r7",
"top": -24,
"left": 133.85,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r8",
"top": -24,
"left": 124.25,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r9",
"top": -24,
"left": 114.65,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r10",
"top": -24,
"left": 105.05,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r11",
"top": -24,
"left": 95.45,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led-ring",
"id": "ring2",
"top": -378.36,
"left": -10.71,
"attrs": { "pixels": "4" }
},
{ "type": "wokwi-vcc", "id": "vcc1", "top": -373.64, "left": 326.4, "attrs": {} }
],
"connections": [
[ "uno:2", "bb1:6b.g", "magenta", [ "v0" ] ],
[ "uno:3", "bb1:5b.g", "gray", [ "v0" ] ],
[ "uno:4", "bb1:4b.g", "limegreen", [ "v0" ] ],
[ "uno:5", "bb1:3b.g", "cyan", [ "v0" ] ],
[ "uno:6", "bb1:2b.g", "yellow", [ "v0" ] ],
[ "uno:7", "bb1:1b.g", "purple", [ "v0" ] ],
[ "r6:2", "r5:2", "green", [ "h0" ] ],
[ "bb1:tp.1", "bb1:1t.a", "red", [ "v0.3", "h-8" ] ],
[ "bb1:2t.b", "bb1:1t.b", "green", [ "v0" ] ],
[ "bb1:3t.a", "bb1:2t.a", "green", [ "v0" ] ],
[ "bb1:3t.b", "bb1:4t.b", "green", [ "v0" ] ],
[ "bb1:4t.a", "bb1:5t.a", "green", [ "v0" ] ],
[ "bb1:5t.b", "bb1:6t.b", "green", [ "v0" ] ],
[ "uno:GND.2", "bb1:bn.25", "black", [ "v19.1", "h128.7" ] ],
[ "uno:5V", "bb1:bp.24", "red", [ "v28.7", "h109.4" ] ],
[ "ring5:DIN", "r7:1", "violet", [ "v48", "h-28.8" ] ],
[ "ring2:DIN", "r8:1", "gold", [ "v57.6", "h115.2" ] ],
[ "ring4:DIN", "r9:1", "gray", [ "v19.2", "h-76.8" ] ],
[ "ring3:DIN", "r10:1", "orange", [ "v28.8", "h144" ] ],
[ "ring1:DIN", "r11:1", "blue", [ "v0" ] ],
[ "bb1:tp.25", "bb1:bp.25", "red", [ "v0.3", "h40", "v172.8" ] ],
[ "bb1:tn.25", "bb1:bn.25", "black", [ "v-0.1", "h49.6", "v192" ] ],
[ "r1:1", "bb1:6b.h", "", [ "$bb" ] ],
[ "r1:2", "bb1:bn.5", "", [ "$bb" ] ],
[ "r2:1", "bb1:5b.h", "", [ "$bb" ] ],
[ "r2:2", "bb1:bn.4", "", [ "$bb" ] ],
[ "r3:1", "bb1:4b.h", "", [ "$bb" ] ],
[ "r3:2", "bb1:bn.3", "", [ "$bb" ] ],
[ "r4:1", "bb1:3b.h", "", [ "$bb" ] ],
[ "r4:2", "bb1:bn.2", "", [ "$bb" ] ],
[ "r5:1", "bb1:2b.h", "", [ "$bb" ] ],
[ "r5:2", "bb1:bn.1", "", [ "$bb" ] ],
[ "r6:1", "bb1:1b.h", "", [ "$bb" ] ],
[ "sw1:1a", "bb1:1b.f", "", [ "$bb" ] ],
[ "sw1:2a", "bb1:2b.f", "", [ "$bb" ] ],
[ "sw1:3a", "bb1:3b.f", "", [ "$bb" ] ],
[ "sw1:4a", "bb1:4b.f", "", [ "$bb" ] ],
[ "sw1:5a", "bb1:5b.f", "", [ "$bb" ] ],
[ "sw1:6a", "bb1:6b.f", "", [ "$bb" ] ],
[ "sw1:7a", "bb1:7b.f", "", [ "$bb" ] ],
[ "sw1:8a", "bb1:8b.f", "", [ "$bb" ] ],
[ "sw1:8b", "bb1:8t.c", "", [ "$bb" ] ],
[ "sw1:7b", "bb1:7t.c", "", [ "$bb" ] ],
[ "sw1:6b", "bb1:6t.c", "", [ "$bb" ] ],
[ "sw1:5b", "bb1:5t.c", "", [ "$bb" ] ],
[ "sw1:4b", "bb1:4t.c", "", [ "$bb" ] ],
[ "sw1:3b", "bb1:3t.c", "", [ "$bb" ] ],
[ "sw1:2b", "bb1:2t.c", "", [ "$bb" ] ],
[ "sw1:1b", "bb1:1t.c", "", [ "$bb" ] ],
[ "ring4:GND", "bb1:tn.5", "black", [ "v0" ] ],
[ "ring5:GND", "ring4:GND", "black", [ "v0" ] ],
[ "ring1:GND", "ring5:GND", "black", [ "v0" ] ],
[ "ring2:GND", "ring4:GND", "black", [ "v0" ] ],
[ "ring3:GND", "bb1:tn.1", "black", [ "v0" ] ],
[ "ring4:VCC", "vcc1:VCC", "red", [ "v9.6", "h67.2" ] ],
[ "ring5:VCC", "ring4:VCC", "red", [ "v67.2", "h28.8" ] ],
[ "ring1:VCC", "ring5:VCC", "red", [ "v67.2", "h67.2" ] ],
[ "ring2:VCC", "ring1:VCC", "red", [ "v38.4", "h76.8" ] ],
[ "ring3:VCC", "ring2:VCC", "red", [ "v19.2", "h48" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for LED sequence | void setup() {
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
pinMode(1, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(7, HIGH);
delay(2000);
digitalWrite(1, HIGH);
delay(1000);
digitalWrite(13, LOW);
digitalWrite(7, LOW);
digitalWrite(1, LOW);
delay(1000);
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -90, "left": 99.8, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led2",
"top": -99.6,
"left": 138.2,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -109.2,
"left": 186.2,
"attrs": { "color": "yellow" }
}
],
"connections": [
[ "led1:C", "uno:GND.1", "green", [ "v0" ] ],
[ "led1:A", "uno:13", "green", [ "v9.6", "h0", "v9.6" ] ],
[ "led2:C", "led1:C", "green", [ "v9.6", "h-38" ] ],
[ "led2:A", "uno:7", "green", [ "v0" ] ],
[ "led3:C", "led2:C", "green", [ "v0" ] ],
[ "led3:A", "uno:4", "green", [ "v0" ] ],
[ "uno:4", "uno:1", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for window control using arduino | #include <Arduino.h>
#include "Button.h"
#include "ServoMotor.h"
#include "Potentiometer.h"
#include "MsgService.h"
#include "Lcd.h"
#define button_pin 2
#define pot_pin A0
#define servo_pin 10
int lastPercentage = 0;
int percentage = 0;
int temperature = 0;
Button* btn;
ServoMotor* window;
Potentiometer* potentiometer;
Lcd* lcd;
enum {automatic, manual} state ;
void setup() {
// put your setup code here, to run once:
MsgService.init();
btn = new Button(button_pin);
window = new ServoMotor(servo_pin);
potentiometer = new Potentiometer(pot_pin);
lcd = new Lcd(0x27,20,4);
lcd->init();
window->openPercentage(0);
state = automatic;
lcd->setCursorTo(3,0);
lcd->print("state:Automatic");
Serial.println("Hello Friend!");
}
void loop() {
// put your main code here, to run repeatedly:
bool buttonIsPressed = btn->isPressed();
if(MsgService.isMsgAvailable()){
Msg* msg = MsgService.receiveMsg();
String tag = msg->getContent().substring(0,1);
String value = msg->getContent().substring(2);
if(tag == "T"){
temperature = value.toInt();
}else if(tag == "S"){
if(value == "automatic"){
state = automatic;
}else if(value == "manual"){
state = manual;
}else{
lcd->print("State is not set properly");
}
}else if(tag == "P"){
percentage = value.toInt();
}else{
Serial.println("I didn't understand!");
}
}
switch(state){
case automatic:
lcd->updateState("Automatic");
lcd->updatePercentage(percentage);
MsgService.sendMsg("S:automatic");
if(buttonIsPressed){
state = manual;
}else{
lcd->setCursorTo(0, 2);
lcd->print(" ");
if(percentage != lastPercentage){
lastPercentage = percentage;
lcd->updatePercentage(lastPercentage);
window->openPercentage(lastPercentage);
}
}
break;
case manual:
if(buttonIsPressed){
state = automatic;
lcd->clearScreen();
}else{
lcd->updateState("Manual");
lcd->updatePercentage(lastPercentage);
lcd->updateTemp(temperature);
MsgService.sendMsg("S:manual");
int potPercentage = potentiometer->percentageValue();
if(potPercentage != lastPercentage){
lastPercentage = potPercentage;
lcd->updatePercentage(lastPercentage);
window->openPercentage(lastPercentage);
String percentMsg = "P:" + String(lastPercentage);
MsgService.sendMsg(percentMsg);
}
}
}
delay(200);
} | {
"version": 1,
"author": "Keni Tafili",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -60.6, "left": 146.8, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": -57, "left": -231, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo1", "top": -194, "left": 96, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot1", "top": -174.1, "left": 374.2, "attrs": {} },
{
"type": "wokwi-lcd2004",
"id": "lcd1",
"top": 198.4,
"left": 149.6,
"attrs": { "pins": "i2c" }
},
{
"type": "wokwi-pushbutton-6mm",
"id": "btn1",
"top": 29.3,
"left": 339.5,
"rotate": 90,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 100.8,
"left": 335.45,
"rotate": 90,
"attrs": { "value": "10000" }
}
],
"connections": [
[ "uno:GND.2", "bb1:bn.1", "black", [ "v28.7", "h243.9" ] ],
[ "uno:5V", "bb1:bp.1", "red", [ "v47.9", "h243.8", "v-57.6" ] ],
[ "bb1:bp.15", "bb1:19b.j", "red", [ "v0" ] ],
[ "bb1:21t.a", "uno:2", "gold", [ "v-9.6", "h-297.6" ] ],
[ "servo1:PWM", "uno:10", "green", [ "h0" ] ],
[ "servo1:V+", "bb1:bp.3", "purple", [ "h-38.4", "v48.1", "h144", "v9.6" ] ],
[ "servo1:GND", "bb1:bn.2", "black", [ "h-57.6", "v76.8", "h153.6" ] ],
[ "pot1:GND", "bb1:bn.21", "black", [ "v0" ] ],
[ "pot1:VCC", "bb1:bp.24", "red", [ "v0" ] ],
[ "pot1:SIG", "uno:A0", "green", [ "v297.6", "h-436.2" ] ],
[ "lcd1:GND", "bb1:bn.4", "black", [ "h-28.8", "v-57.6", "h86.4" ] ],
[ "lcd1:VCC", "bb1:bp.5", "red", [ "h-19.2", "v-86.3", "h86.4", "v-27.9" ] ],
[ "lcd1:SDA", "uno:A4", "orange", [ "h-134.4", "v-28.6" ] ],
[ "lcd1:SCL", "uno:A5", "blue", [ "h-115.2", "v9.9" ] ],
[ "btn1:1.l", "bb1:21t.e", "", [ "$bb" ] ],
[ "btn1:2.l", "bb1:19t.e", "", [ "$bb" ] ],
[ "btn1:1.r", "bb1:21b.f", "", [ "$bb" ] ],
[ "btn1:2.r", "bb1:19b.f", "", [ "$bb" ] ],
[ "r1:1", "bb1:21b.h", "", [ "$bb" ] ],
[ "r1:2", "bb1:bn.17", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Simple LED blink using arduino | int led = 11;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
} | {
"version": 1,
"author": "Julian Manders-Jones",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard", "id": "bb1", "top": 93, "left": 70, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": -124.2, "left": 354.6, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 164.8,
"left": 589.8,
"rotate": 180,
"attrs": { "color": "red", "flip": "" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 147.95,
"left": 499.2,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "bb1:55t.a", "bb1:tn.45", "green", [ "v0" ] ],
[ "bb1:54t.c", "bb1:49t.c", "green", [ "v0" ] ],
[ "uno:11", "bb1:43t.a", "gold", [ "v0" ] ],
[ "r1:1", "bb1:43t.b", "", [ "$bb" ] ],
[ "r1:2", "bb1:49t.b", "", [ "$bb" ] ],
[ "led1:A", "bb1:54t.d", "", [ "$bb" ] ],
[ "led1:C", "bb1:55t.d", "", [ "$bb" ] ],
[ "uno:GND.3", "bb1:tn.38", "black", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for relay control | int i = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
}
void loop() {
if (digitalRead(4) == 0)
{
digitalWrite(2, HIGH);
}
else if (digitalRead(5) == 0)
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
else if (digitalRead(6) == 0)
{
digitalWrite(3, HIGH);
}
delay(50);
} | {
"version": 1,
"author": "Барсук Иванов",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -58.1, "attrs": {} },
{ "type": "wokwi-relay-module", "id": "relay1", "top": -86.2, "left": 96, "attrs": {} },
{ "type": "wokwi-relay-module", "id": "relay2", "top": 105.8, "left": 96, "attrs": {} },
{
"type": "wokwi-pushbutton-6mm",
"id": "btn1",
"top": -98.2,
"left": -124.8,
"attrs": { "color": "green", "key": "(" }
},
{
"type": "wokwi-pushbutton-6mm",
"id": "btn2",
"top": -98.2,
"left": -86.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton-6mm",
"id": "btn3",
"top": -98.2,
"left": -48,
"attrs": { "color": "green" }
},
{ "type": "wokwi-vcc", "id": "vcc1", "top": -133.64, "left": 249.6, "attrs": {} },
{ "type": "wokwi-junction", "id": "j1", "top": -72, "left": 350.4, "attrs": {} },
{ "type": "wokwi-junction", "id": "j2", "top": 120, "left": 350.4, "attrs": {} }
],
"connections": [
[ "relay1:GND", "nano:GND.2", "black", [ "h0" ] ],
[ "relay1:IN", "nano:2", "green", [ "h-38.4", "v9.4" ] ],
[ "nano:GND.1", "relay2:GND", "black", [ "v67.2" ] ],
[ "relay2:IN", "nano:3", "green", [ "h-163.2", "v-163.4", "h115.2" ] ],
[ "relay2:VCC", "nano:5V", "red", [ "h0" ] ],
[ "relay1:VCC", "relay2:VCC", "red", [ "h-19.2", "v57.6", "h38.4", "v86.4", "h-19.2" ] ],
[ "relay1:COM", "relay2:COM", "black", [ "h39.6", "v190.6" ] ],
[ "relay2:GND", "relay2:COM", "black", [ "h-19.2", "v47.6", "h172.8", "v-48" ] ],
[ "btn2:1.r", "btn3:1.r", "black", [ "v-9.6", "h39.2" ] ],
[ "btn1:1.r", "btn2:1.r", "black", [ "v-9.6", "h39.2" ] ],
[ "btn3:1.r", "nano:GND.2", "black", [ "v-9.6", "h87.2" ] ],
[ "btn3:2.r", "nano:4", "green", [ "h0" ] ],
[ "btn2:2.r", "nano:5", "green", [ "h0.8", "v10", "h86.4" ] ],
[ "btn1:2.r", "nano:6", "green", [ "h0.8", "v19.6", "h115.2" ] ],
[ "relay1:NO", "vcc1:VCC", "red", [ "h0" ] ],
[ "relay2:NO", "vcc1:VCC", "red", [ "h49.2", "v-193.8" ] ],
[ "relay2:NC", "j2:J", "orange", [ "v-1.2", "h145.2" ] ],
[ "j1:J", "relay1:NC", "orange", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Traffic light control | int Knop = A3;
int RED_LED = A0;
int GREEN_LED = A2;
int YELLOW_LED = A1;
typedef enum {Rood, Groen, Geel} states;
static states state = Rood;
// Constante voor de duur van geel
const int geelDuur = 4000;
int geelStartTijd = 0; // Tijd in milliseconden voor geel
void setup() {
pinMode(Knop, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
// Zet beginwaarden voor LEDs
digitalWrite(RED_LED, HIGH); // Start met rood aan
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
}
void loop() {
// Lees de waarde van de knop
int knopStatus = digitalRead(Knop);
switch (state) {
case Rood:
if (knopStatus == LOW) { // Knop ingedrukt (LOW vanwege INPUT_PULLUP)
state = Groen;
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(YELLOW_LED, LOW);
}
break;
case Groen:
if (knopStatus == HIGH) { // Knop losgelaten
state = Geel;
geelStartTijd = millis(); // Sla de huidige tijd op
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, HIGH);
}
break;
case Geel:
// Controleer of de huidige tijd 3 seconden na geelStartTijd is
if ((millis() - geelStartTijd) >= geelDuur) {
state = Rood;
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
}
break;
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": -105, "left": -0.6, "attrs": {} },
{
"type": "wokwi-rgb-led",
"id": "rgb1",
"top": 90.4,
"left": -114.1,
"attrs": { "common": "cathode" }
},
{
"type": "wokwi-rgb-led",
"id": "rgb2",
"top": 157.6,
"left": -114.1,
"attrs": { "common": "cathode" }
},
{
"type": "wokwi-rgb-led",
"id": "rgb3",
"top": 224.8,
"left": -114.1,
"attrs": { "common": "cathode" }
},
{
"type": "wokwi-slide-switch",
"id": "sw1",
"top": 167.6,
"left": -6.5,
"attrs": { "bounce": "0", "value": "0" }
}
],
"connections": [
[ "sw1:2", "uno:A3", "green", [ "v19.2", "h192.1" ] ],
[ "sw1:3", "uno:5V", "red", [ "v9.6", "h134.6" ] ],
[ "uno:GND.2", "sw1:1", "black", [ "v143.9", "h-159.3" ] ],
[ "rgb1:COM", "rgb2:COM", "black", [ "v-0.4", "h-28.7", "v67.2" ] ],
[ "rgb2:COM", "rgb3:COM", "black", [ "v-0.4", "h-28.7", "v67.2" ] ],
[ "uno:GND.3", "rgb1:COM", "black", [ "v220.7", "h-303.2", "v-163.2" ] ],
[ "rgb1:R", "uno:A0", "red", [ "v19.2", "h316.8" ] ],
[ "rgb3:G", "uno:A2", "green", [ "v28.8", "h308.5" ] ],
[ "rgb2:R", "uno:A1", "gold", [ "v19.2", "h76.8", "v38.4", "h240" ] ],
[ "rgb2:G", "uno:A1", "gold", [ "v9.6", "h68.5", "v38.4", "h220.8" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for motor control | #include <LiquidCrystal.h>
#define thermocouple A2
#define PRIV 13
#define ENTER 12
#define Next 11
#define Limit 10
#define Heater 8
#define stepPin A4
#define dirPin A3
//Temperature initialization
int Temperature = 0;
int SetTemperature = 300;
int adc_val;
byte ShowTemp = 0;
byte HeaterCond=0;
//Motor initialization
byte Speed = 2;
byte TestSpeed = 2;
byte TestSpeedCond=0;
int MotorSpeed = 10;
unsigned long previousMillisM = 0;
byte Motorcondi = 0;
byte MotorStart = 0;
//Button initialization
byte ButtonNumber = 0;
unsigned long previousMillisBtn = 0;
byte intervalBtn = 200;
byte ActiveBtn = 1;
byte BtnCondi = 0;
//Action initialization
byte Windwos = 0;
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(thermocouple, INPUT);
pinMode(PRIV, INPUT);
pinMode(ENTER, INPUT);
pinMode(Next, INPUT);
pinMode(Limit, INPUT);
pinMode(Heater, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Negs Electronics");
lcd.setCursor(0, 1);
lcd.print("Tel:- 0951754711");
delay(3000);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
LcdDisplay(1);
}
void loop() {
Button();
Action();
Temps();
Motor();
/*
lcd.setCursor(0, 1);
lcd.print(ButtonNumber);
if(ButtonNumber==1){
Temperature=Temperature+1;
ButtonNumber=0;
ActiveBtn=0;
}
lcd.setCursor(0, 0);
lcd.print(Temperature);
*/
}
void Action() {
if (Windwos == 1) {
if (ButtonNumber == 2) {
LcdDisplay(4);
ButtonNumber = 0;
} else if (ButtonNumber == 3) {
LcdDisplay(2);
ButtonNumber = 0;
}
} else if (Windwos == 4) {
if (ButtonNumber == 2) {
LcdDisplay(1);
ButtonNumber = 0;
}
} else if (Windwos == 2) {
if (ButtonNumber == 1) {
LcdDisplay(1);
ButtonNumber = 0;
} else if (ButtonNumber == 2) {
LcdDisplay(5);
ButtonNumber = 0;
} else if (ButtonNumber == 3) {
LcdDisplay(3);
ButtonNumber = 0;
}
} else if (Windwos == 5) {
if (ButtonNumber == 2) {
LcdDisplay(2);
ButtonNumber = 0;
}else if (ButtonNumber == 1) {
TestSpeed=TestSpeed>0? TestSpeed-1:TestSpeed;
ButtonNumber = 0;
}else if (ButtonNumber == 3) {
TestSpeed=TestSpeed<10? TestSpeed+1:TestSpeed;
ButtonNumber = 0;
}
}
else if (Windwos == 3) {
if (ButtonNumber == 1) {
LcdDisplay(2);
ButtonNumber = 0;
} else if (ButtonNumber == 2) {
LcdDisplay(6);
ButtonNumber = 0;
}
else if (ButtonNumber == 3) {
LcdDisplay(7);
ButtonNumber = 0;
}
}
else if (Windwos == 6) {
if (ButtonNumber == 2) {
LcdDisplay(3);
ButtonNumber = 0;
} else if (ButtonNumber == 1) {
Speed=Speed>0? Speed-1:Speed;
ButtonNumber = 0;
}else if (ButtonNumber == 3) {
Speed=Speed<10? Speed+1:Speed;
ButtonNumber = 0;
}
}
else if (Windwos == 7) {
if (ButtonNumber == 1) {
LcdDisplay(3);
ButtonNumber = 0;
} else if (ButtonNumber == 2) {
LcdDisplay(8);
ButtonNumber = 0;
}
}
else if (Windwos == 8) {
if (ButtonNumber == 2) {
LcdDisplay(7);
ButtonNumber = 0;
}else if (ButtonNumber == 1) {
SetTemperature=SetTemperature>100? SetTemperature-1:SetTemperature;
ButtonNumber = 0;
}else if (ButtonNumber == 3) {
SetTemperature=SetTemperature<500? SetTemperature+1:SetTemperature;
ButtonNumber = 0;
}
}
}
void Button() {
timeBtn();
if (BtnCondi) {
BtnCondi = !BtnCondi;
ButtonNumber = digitalRead(Next) == 1 ? 3 : ButtonNumber;
ButtonNumber = digitalRead(ENTER) == 1 ? 2 : ButtonNumber;
ButtonNumber = digitalRead(PRIV) == 1 ? 1 : ButtonNumber;
ButtonNumber = digitalRead(Limit) == 1 ? 4 : ButtonNumber;
}
}
void LcdDisplay(byte Condi) {
switch (Condi) {
case 1:
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("=>Start ");
lcd.setCursor(0, 1);
lcd.print(" Test Motor ");
Windwos = 1;
ShowTemp=0;
HeaterCond=0;
break;
case 2:
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("=>Test Motor ");
lcd.setCursor(0, 1);
lcd.print(" Speed Setting ");
Windwos = 2;
TestSpeedCond=0;
MotorStart=0;
break;
case 3:
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("=>Speed Setting ");
lcd.setCursor(0, 1);
lcd.print(" Themp Setting ");
Windwos = 3;
TestSpeedCond=0;
MotorStart=0;
break;
case 4:
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("<=Stop ");
lcd.setCursor(0, 1);
lcd.print("Sped=");
lcd.setCursor(5, 1);
lcd.print(Speed);
lcd.setCursor(7, 1);
lcd.print(" Temp=");
lcd.setCursor(13, 1);
lcd.print(Temperature);
ShowTemp = 1;
HeaterCond=1;
Windwos = 4;
break;
case 5:
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("<=Test Motor ");
lcd.setCursor(0, 1);
lcd.print("Speed = ");
lcd.setCursor(8, 1);
lcd.print(TestSpeed);
Windwos = 5;
TestSpeedCond=1;
MotorStart=1;
break;
case 6:
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("<=Speed Setting ");
lcd.setCursor(0, 1);
lcd.print("Speed = ");
lcd.setCursor(8, 1);
lcd.print(Speed);
Windwos = 6;
TestSpeedCond=2;
MotorStart=1;
break;
case 7:
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("=>Themp Setting ");
lcd.setCursor(0, 1);
lcd.print(" ");
Windwos = 7;
ShowTemp = 0;
break;
case 8:
lcd.setCursor(0, 0);
//lcd.print(" ");
lcd.print("<=Themp Setting ");
lcd.setCursor(0, 1);
lcd.print("Temperatur=");
lcd.setCursor(11, 1);
lcd.print(SetTemperature);
Windwos = 8;
ShowTemp = 2;
break;
}
}
void timeBtn() {
unsigned long currentMillisBtn = millis();
if (currentMillisBtn - previousMillisBtn >= intervalBtn) {
previousMillisBtn = currentMillisBtn;
BtnCondi = BtnCondi == 0 ? 1 : 0;
ActiveBtn = 1;
}
}
void Temps() {
adc_val = analogRead(thermocouple);
Temperature = (((adc_val * 4.88) - 0.0027) / 10.0);
if (ShowTemp == 1)
{
lcd.setCursor(13, 1);
lcd.print(Temperature);
}
else if (ShowTemp == 2)
{
lcd.setCursor(11, 1);
lcd.print(SetTemperature);
}
if(HeaterCond){
if(SetTemperature>=Temperature){
digitalWrite(Heater,true);
MotorStart=0;
}else{
digitalWrite(Heater,LOW);
MotorStart=1;
}
}else{
digitalWrite(Heater,LOW);
if(Windwos==1){
MotorStart=0;
}
}
}
void Motor(){
if(TestSpeedCond==1){
lcd.setCursor(8, 1);
lcd.print(TestSpeed);
if(TestSpeed==9){
lcd.setCursor(9, 1);
lcd.print(" ");
}
}
else if(TestSpeedCond==2){
lcd.setCursor(8, 1);
lcd.print(Speed);
if(Speed==9){
lcd.setCursor(9, 1);
lcd.print(" ");
}
}
if (MotorStart) {
timeMotor();
digitalWrite(dirPin, HIGH);
if (Motorcondi == 0) {
digitalWrite(stepPin, HIGH);
} else {
digitalWrite(stepPin, LOW);
}
}
}
void timeMotor() {
if (Windwos == 6 || Windwos == 4) {
MotorSpeed = Speed;
} else if (Windwos == 5) {
MotorSpeed = TestSpeed;
}
unsigned long currentMillisM = micros();
if (currentMillisM - previousMillisM >= MotorSpeed * 99) {
previousMillisM = currentMillisM;
Motorcondi = Motorcondi == 0 ? 1 : 0;
}
} | {
"version": 1,
"author": "negs electronics",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 106.2, "left": -39, "attrs": {} },
{ "type": "wokwi-lcd1602", "id": "lcd1", "top": -82.97, "left": 25.6, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 83,
"left": 297.6,
"attrs": { "color": "green", "key": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 140.6,
"left": 297.6,
"attrs": { "color": "blue", "key": "2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": 198.2,
"left": 297.6,
"attrs": { "color": "green", "key": "3" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 282.35,
"left": 326.4,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 301.55,
"left": 326.4,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 320.75,
"left": 326.4,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 254.4,
"left": -106.15,
"rotate": 90,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-pushbutton",
"id": "btn4",
"top": 159.8,
"left": -153.6,
"attrs": { "color": "green", "bounce": "1", "key": "4" }
},
{ "type": "wokwi-led", "id": "led1", "top": 246, "left": 253.4, "attrs": { "color": "red" } },
{ "type": "wokwi-a4988", "id": "drv1", "top": 523.2, "left": 292.8, "attrs": {} },
{
"type": "wokwi-stepper-motor",
"id": "stepper1",
"top": 349.21,
"left": 374.83,
"attrs": { "size": "17" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": 311.15,
"left": 230.4,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-potentiometer", "id": "pot1", "top": -30.1, "left": 345.4, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r6",
"top": 493.55,
"left": 134.4,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "lcd1:RS", "uno:2", "green", [ "v38.4", "h101.8" ] ],
[ "lcd1:E", "uno:3", "green", [ "v28.8", "h72.8" ] ],
[ "lcd1:D4", "uno:4", "green", [ "v19.2", "h9.6" ] ],
[ "lcd1:D5", "uno:5", "green", [ "v0" ] ],
[ "lcd1:D6", "uno:6", "green", [ "v9.6", "h-28.6", "v9.6" ] ],
[ "lcd1:D7", "uno:7", "green", [ "v19.2", "h0.3" ] ],
[ "lcd1:RW", "uno:GND.1", "green", [ "v38.4", "h-19.3" ] ],
[ "btn3:2.r", "btn2:2.r", "green", [ "h38.6", "v-57.4" ] ],
[ "btn1:2.r", "btn2:2.r", "green", [ "h38.6", "v57.8" ] ],
[ "uno:5V", "btn3:2.r", "red", [ "v47.9", "h253.4" ] ],
[ "uno:GND.2", "r3:1", "black", [ "v0" ] ],
[ "r3:1", "r2:1", "green", [ "v0" ] ],
[ "r1:1", "r2:1", "green", [ "v0" ] ],
[ "btn3:1.r", "r1:2", "green", [ "v0", "h48.2", "v76.8" ] ],
[ "btn2:1.r", "r2:2", "green", [ "v0", "h67.4", "v153.6" ] ],
[ "btn1:1.r", "r3:2", "green", [ "v0", "h96.2", "v220.8" ] ],
[ "uno:13", "btn1:1.r", "magenta", [ "v-57.6", "h154" ] ],
[ "uno:12", "btn2:1.r", "yellow", [ "v-48", "h182.9", "v57.6", "h96" ] ],
[ "uno:11", "btn3:1.r", "purple", [ "v-28.8", "h163.8", "v96", "h105.6" ] ],
[ "btn4:1.r", "uno:10", "green", [ "v0" ] ],
[ "uno:GND.3", "r4:2", "black", [ "v0" ] ],
[ "r4:1", "btn4:1.r", "green", [ "h28.8", "v-57.6", "h-38.4" ] ],
[ "uno:5V", "btn4:2.r", "red", [ "v47.9", "h-207.4", "v-144", "h19.2", "v-19.2" ] ],
[ "drv1:2B", "stepper1:A-", "green", [ "h0" ] ],
[ "drv1:2A", "stepper1:A+", "green", [ "h0" ] ],
[ "stepper1:B+", "drv1:1A", "green", [ "v0" ] ],
[ "drv1:1B", "stepper1:B-", "green", [ "h0" ] ],
[ "drv1:RESET", "drv1:SLEEP", "green", [ "h-28.8", "v9.6" ] ],
[ "drv1:STEP", "uno:A4", "green", [ "h-19.2", "v-240", "h-76.8" ] ],
[ "drv1:DIR", "uno:A3", "green", [ "h-57.6", "v-259.2", "h-57.6" ] ],
[ "drv1:VDD", "uno:5V", "green", [ "h-38.25", "v-211.12", "h-211.2" ] ],
[ "uno:8", "led1:A", "green", [ "v-19.2", "h125.2", "v201.6", "h28.8" ] ],
[ "r5:2", "led1:C", "green", [ "v-9.6", "h-30" ] ],
[ "r5:1", "uno:GND.3", "green", [ "v0", "h-86.4", "v-19.2" ] ],
[ "pot1:GND", "uno:GND.2", "black", [ "v0" ] ],
[ "pot1:VCC", "uno:5V", "red", [ "v0" ] ],
[ "pot1:SIG", "uno:A2", "green", [ "v38.4", "h86" ] ],
[ "r6:1", "pot1:SIG", "green", [ "v0", "h-19.2" ] ],
[ "r6:2", "pot1:GND", "green", [ "v-28.8", "h-97.2", "v-9.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for inbuilt LED blink | int led = 13;
void setup() {
pinMode(led,OUTPUT);
}
void loop() {
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(500);
} | {
"version": 1,
"author": "8.9 Fikri nur Fauzi Siswanto",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": -0.6, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -42, "left": 99.8, "attrs": { "color": "red" } }
],
"connections": [ [ "led1:C", "uno:GND.1", "green", [ "v0" ] ], [ "led1:A", "uno:13", "green", [ "v0" ] ] ],
"dependencies": {}
} |
Generate a diagram.json code for wifi scanning using ESP32 | /* ESP32 WiFi Scanning example */
#include "WiFi.h"
void setup() {
Serial.begin(115200);
Serial.println("Initializing WiFi...");
WiFi.mode(WIFI_STA);
Serial.println("Setup done!");
}
void loop() {
Serial.println("Scanning...");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Scan done!");
if (n == 0) {
Serial.println("No networks found.");
} else {
Serial.println();
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [ { "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": 0, "attrs": {} } ],
"connections": [ [ "esp:TX", "$serialMonitor:RX", "", [] ], [ "esp:RX", "$serialMonitor:TX", "", [] ] ]
} |
Generate a diagram.json code for NTP clock | // Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
}
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void loop() {
printLocalTime();
delay(250);
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -76.8, "left": 43.24, "attrs": {} },
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": -70.4,
"left": 188,
"attrs": { "pins": "i2c" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "lcd1:SDA", "esp:21", "green", [ "v-4.49", "h-28.8", "v23.89" ] ],
[ "lcd1:GND", "esp:GND.2", "black", [ "h-16.67", "v162.78" ] ],
[ "lcd1:VCC", "esp:3V3", "red", [ "h-9.6", "v-57.5", "h-134.55" ] ],
[ "lcd1:SCL", "esp:22", "purple", [ "h-19.2", "v-18.9" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ESP32 HTTP server | /* ESP32 HTTP IoT Server Example for Wokwi.com
https://wokwi.com/projects/320964045035274834
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
WebServer server(80);
const int LED1 = 26;
const int LED2 = 27;
bool led1State = false;
bool led2State = false;
void sendHtml() {
String response = R"(
<!DOCTYPE html><html>
<head>
<title>ESP32 Web Server Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html { font-family: sans-serif; text-align: center; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
.btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
font-size: 2em; text-decoration: none }
.btn.OFF { background-color: #333; }
</style>
</head>
<body>
<h1>ESP32 Web Server</h1>
<div>
<h2>LED 1</h2>
<a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
<h2>LED 2</h2>
<a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
</div>
</body>
</html>
)";
response.replace("LED1_TEXT", led1State ? "ON" : "OFF");
response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
server.send(200, "text/html", response);
}
void setup(void) {
Serial.begin(115200);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", sendHtml);
server.on(UriBraces("/toggle/{}"), []() {
String led = server.pathArg(0);
Serial.print("Toggle LED #");
Serial.println(led);
switch (led.toInt()) {
case 1:
led1State = !led1State;
digitalWrite(LED1, led1State);
break;
case 2:
led2State = !led2State;
digitalWrite(LED2, led2State);
break;
}
sendHtml();
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
delay(2);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": -4.76, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 109.55,
"left": -76.8,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 119.15,
"left": -76.8,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": 34.8,
"left": -111.4,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 63.6,
"left": -140.2,
"attrs": { "color": "green" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "r1:1", "led1:A", "green", [ "v0.07", "h-10" ] ],
[ "r2:1", "led2:A", "green", [ "h-38.4", "v-19.2" ] ],
[ "esp:GND.1", "led2:C", "black", [ "h0" ] ],
[ "esp:GND.1", "led1:C", "black", [ "h0" ] ],
[ "esp:26", "r1:2", "green", [ "h0" ] ],
[ "r2:2", "esp:27", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ESP32 joke machine | /*
ESP32 HTTPClient Jokes API Example
https://wokwi.com/projects/342032431249883731
Copyright (C) 2022, Uri Shaked
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BTN_PIN 5
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const String url = "https://v2.jokeapi.dev/joke/Programming";
String getJoke() {
HTTPClient http;
http.useHTTP10(true);
http.begin(url);
http.GET();
String result = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, result);
// Test if parsing succeeds.
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return "<error>";
}
String type = doc["type"].as<String>();
String joke = doc["joke"].as<String>();
String setup = doc["setup"].as<String>();
String delivery = doc["delivery"].as<String>();
http.end();
return type.equals("single") ? joke : setup + " " + delivery;
}
void nextJoke() {
tft.setTextColor(ILI9341_WHITE);
tft.println("\nLoading joke...");
String joke = getJoke();
tft.setTextColor(ILI9341_GREEN);
tft.println(joke);
}
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
WiFi.begin(ssid, password, 6);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
tft.print(".");
}
tft.print("\nOK! IP=");
tft.println(WiFi.localIP());
nextJoke();
}
void loop() {
if (digitalRead(BTN_PIN) == LOW) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
nextJoke();
}
delay(100);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 96,
"left": -158.36,
"attrs": { "fastTLS": "1" }
},
{
"type": "wokwi-ili9341",
"id": "lcd1",
"top": 34.2,
"left": 88.1,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 294.2,
"left": 144,
"attrs": { "color": "green", "label": "New joke" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "lcd1:GND", "esp:GND.2", "black", [ "h-19.2", "v91.54" ] ],
[ "lcd1:RST", "esp:4", "purple", [ "h-48", "v67.2" ] ],
[ "lcd1:D/C", "esp:2", "#8f4814", [ "h-28.8", "v44.14" ] ],
[ "lcd1:SCK", "esp:18", "gray", [ "v-0.01", "h-48", "v-19.2" ] ],
[ "lcd1:MISO", "esp:19", "orange", [ "h-67.2", "v-9.61", "h0", "v-19.2" ] ],
[ "lcd1:MOSI", "esp:23", "green", [ "h-38.4", "v-67.31" ] ],
[ "esp:5V", "lcd1:VCC", "red", [ "h-21.83", "v-206.3", "h201.6", "v48.5" ] ],
[ "lcd1:CS", "esp:15", "violet", [ "h-57.6", "v105.6" ] ],
[ "esp:GND.2", "btn1:2.l", "black", [ "h76.8", "v201.4" ] ],
[ "esp:5", "btn1:1.l", "green", [ "h19.2", "v96" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for oled display with esp32 using micropython | from machine import Pin, I2C
import ssd1306
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('Hello, Wokwi!', 10, 10)
oled.show() | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 9.6,
"left": -100.76,
"attrs": { "env": "micropython-20231005-v1.21.0" }
},
{ "type": "board-ssd1306", "id": "oled1", "top": 99.14, "left": 57.83, "attrs": {} }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "oled1:SCL", "esp:22", "green", [ "v0" ] ],
[ "oled1:SDA", "esp:21", "blue", [ "v-19.2", "h-124.73" ] ],
[ "oled1:GND", "esp:GND.2", "black", [ "v-67.2", "h-96" ] ],
[ "oled1:VCC", "esp:3V3", "red", [ "v-28.8", "h0.15", "v-76.8", "h-201.75" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for micro-python MQTT weather logger with ESP32 | import network
import time
from machine import Pin
import dht
import ujson
from umqtt.simple import MQTTClient
# MQTT Server Parameters
MQTT_CLIENT_ID = "micropython-weather-demo"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-weather"
sensor = dht.DHT22(Pin(15))
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
prev_weather = ""
while True:
print("Measuring weather conditions... ", end="")
sensor.measure()
message = ujson.dumps({
"temp": sensor.temperature(),
"humidity": sensor.humidity(),
})
if message != prev_weather:
print("Updated!")
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
client.publish(MQTT_TOPIC, message)
prev_weather = message
else:
print("No change")
time.sleep(1) | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -37.58, "left": -96.37, "attrs": {} },
{ "type": "wokwi-dht22", "id": "dht1", "top": -32.2, "left": 40.16, "attrs": {} }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "dht1:VCC", "esp:3V3", "red", [ "v109.3", "h-170.36", "v-200.78" ] ],
[ "dht1:SDA", "esp:15", "green", [ "v0" ] ],
[ "dht1:GND", "esp:GND.1", "black", [ "v99.7", "h-189.56", "v-66.38" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for simon says esp32-c3 | #include "pitches.h"
/* Define pin numbers for LEDs, buttons and speaker: */
const uint8_t buttonPins[] = {0, 1, 2, 3};
const uint8_t ledPins[] = {8, 7, 6, 5};
#define SPEAKER_PIN 10
// These are connected to 74HC595 shift register (used to show game score):
const int LATCH_PIN = 18; // 74HC595 pin 12
const int DATA_PIN = 19; // 74HC595 pin 14
const int CLOCK_PIN = 9; // 74HC595 pin 11
#define MAX_GAME_LENGTH 100
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
/* Global variables - store the game state */
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
/**
Set up the Arduino board and initialize Serial communication
*/
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
// The following line primes the random number generator.
// It assumes pin 4 is floating (disconnected):
randomSeed(analogRead(4));
}
/* Digit table for the 7-segment display */
const uint8_t digitTable[] = {
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10010000,
};
const uint8_t DASH = 0b10111111;
void sendScore(uint8_t high, uint8_t low) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, low);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, high);
digitalWrite(LATCH_PIN, HIGH);
}
void displayScore() {
int high = gameIndex % 100 / 10;
int low = gameIndex % 10;
sendScore(high ? digitTable[high] : 0xff, digitTable[low]);
}
/**
Lights the given LED and plays a suitable tone
*/
void lightLedAndPlayTone(byte ledIndex) {
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(300);
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
}
/**
Plays the current sequence of notes that the user has to repeat
*/
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlayTone(currentLed);
delay(50);
}
}
/**
Waits until the user pressed one of the buttons,
and returns the index of that button
*/
byte readButtons() {
while (true) {
for (byte i = 0; i < 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
delay(1);
}
}
/**
Play the game over sequence, and report the game score
*/
void gameOver() {
Serial.print("Game over! your score: ");
Serial.println(gameIndex - 1);
gameIndex = 0;
delay(200);
// Play a Wah-Wah-Wah-Wah sound
tone(SPEAKER_PIN, NOTE_DS5);
delay(300);
tone(SPEAKER_PIN, NOTE_D5);
delay(300);
tone(SPEAKER_PIN, NOTE_CS5);
delay(300);
for (byte i = 0; i < 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) {
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(6);
}
}
noTone(SPEAKER_PIN);
sendScore(DASH, DASH);
delay(500);
}
/**
Get the user's input and compare it with the expected sequence.
*/
bool checkUserSequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gameSequence[i];
byte actualButton = readButtons();
lightLedAndPlayTone(actualButton);
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
/**
Plays a hooray sound whenever the user finishes a level
*/
void playLevelUpSound() {
tone(SPEAKER_PIN, NOTE_E4);
delay(150);
tone(SPEAKER_PIN, NOTE_G4);
delay(150);
tone(SPEAKER_PIN, NOTE_E5);
delay(150);
tone(SPEAKER_PIN, NOTE_C5);
delay(150);
tone(SPEAKER_PIN, NOTE_D5);
delay(150);
tone(SPEAKER_PIN, NOTE_G5);
delay(150);
noTone(SPEAKER_PIN);
}
/**
The main game loop
*/
void loop() {
displayScore();
// Add a random color to the end of the sequence
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH - 1;
}
playSequence();
if (!checkUserSequence()) {
gameOver();
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
delay(300);
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-buzzer",
"id": "buzzer",
"top": 21.6,
"left": 126.6,
"attrs": { "volume": "0.1" }
},
{ "type": "wokwi-led", "id": "led-red", "top": 6, "left": 3.8, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led-green",
"top": 73.2,
"left": 3.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led-blue",
"top": 6,
"left": 272.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led-yellow",
"top": 73.2,
"left": 272.6,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-red",
"top": 6.2,
"left": 48,
"attrs": { "color": "red", "key": "1", "label": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-green",
"top": 73.4,
"left": 48,
"attrs": { "color": "green", "key": "2", "label": "2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-blue",
"top": 6.2,
"left": 201.6,
"attrs": { "color": "blue", "key": "3", "label": "3" }
},
{
"type": "wokwi-pushbutton",
"id": "btn-yellow",
"top": 73.4,
"left": 201.6,
"attrs": { "color": "yellow", "key": "4", "label": "4" }
},
{
"type": "wokwi-74hc595",
"id": "sr1",
"top": 171.8,
"left": 361.16,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-74hc595",
"id": "sr2",
"top": 171.8,
"left": 457.16,
"rotate": 180,
"attrs": {}
},
{ "type": "wokwi-7segment", "id": "sevseg1", "top": 47.16, "left": 379.48, "attrs": {} },
{ "type": "wokwi-7segment", "id": "sevseg2", "top": 47.16, "left": 446.68, "attrs": {} },
{
"type": "board-esp32-c3-devkitm-1",
"id": "esp",
"top": 136.02,
"left": 120.9,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 196.8,
"left": 9.05,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 235.2,
"left": -0.55,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 235.2,
"left": 268.25,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 196.8,
"left": 277.85,
"rotate": 90,
"attrs": { "value": "220" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "esp:GND.1", "buzzer:1", "black", [ "v-9.6", "h-19.2", "v-38.4", "h-57.6" ] ],
[ "esp:0", "btn-yellow:1.l", "gold", [ "v-38.4", "h42", "v-48" ] ],
[ "esp:GND.1", "btn-yellow:2.r", "black", [ "v-9.6", "h48", "v-57.8" ] ],
[ "esp:1", "btn-blue:1.l", "blue", [ "v-43.2", "h47.6", "v-110.4" ] ],
[ "esp:GND.1", "btn-blue:2.r", "black", [ "v-9.6", "h48", "v-125" ] ],
[ "esp:3", "btn-green:2.r", "green", [ "v-19.2", "h-72", "v-48.2" ] ],
[ "esp:GND.1", "btn-green:1.l", "black", [ "v-9.6", "h-188.4", "v-76.8" ] ],
[ "esp:2", "btn-red:2.r", "orange", [ "v-24", "h-76.8", "v-110.6" ] ],
[ "esp:GND.1", "btn-red:1.l", "black", [ "v-9.6", "h-188.4", "v-144" ] ],
[ "esp:10", "buzzer:2", "purple", [ "v-57.6", "h29.2" ] ],
[ "esp:GND.1", "led-yellow:C", "black", [ "v-9.6", "h48", "v-44", "h9.2" ] ],
[ "esp:GND.1", "led-blue:C", "black", [ "v-9.6", "h48", "v-111.2", "h9.2" ] ],
[ "esp:GND.1", "led-green:C", "black", [ "v-9.6", "h-220.8", "v-48" ] ],
[ "esp:GND.1", "led-red:C", "black", [ "v-9.6", "h-220.8", "v-115.2" ] ],
[ "esp:3V3.1", "sr1:VCC", "red", [ "v-19.2", "h96", "v67.2", "h115.2" ] ],
[ "esp:9", "sr1:SHCP", "gray", [ "v21.58", "h192" ] ],
[ "esp:18", "sr1:STCP", "purple", [ "v26.38", "h278.4" ] ],
[ "esp:19", "sr1:DS", "blue", [ "v31.18", "h307.2" ] ],
[ "sr1:SHCP", "sr2:SHCP", "gray", [ "v47", "h106.12" ] ],
[ "sr1:STCP", "sr2:STCP", "purple", [ "v37.4", "h96.52" ] ],
[ "sr1:Q7S", "sr2:DS", "blue", [ "h0.52", "v56.6", "h144" ] ],
[ "sr1:VCC", "sr1:MR", "red", [ "v17", "h-57.6" ] ],
[ "sr1:VCC", "sr2:MR", "red", [ "v17", "h38.4" ] ],
[ "sr1:VCC", "sr2:VCC", "red", [ "v17", "h96" ] ],
[ "sr1:OE", "sr2:OE", "black", [ "v26.6", "h96" ] ],
[ "sr1:MR", "sevseg1:COM.1", "red", [ "v17", "h-57.6", "v-96", "h76.8" ] ],
[ "sevseg1:COM.1", "sevseg2:COM.1", "red", [ "h0", "v9.6", "h57.6" ] ],
[ "sr2:Q0", "sevseg2:A", "green", [ "v7.4", "h28.8", "v-182.4", "h-67.2" ] ],
[ "sr2:Q1", "sevseg2:B", "green", [ "v0", "h9.6", "v-134.4", "h-48" ] ],
[ "sr2:Q2", "sevseg2:C", "green", [ "v-38.4", "h-38.4" ] ],
[ "sr2:Q3", "sevseg2:D", "green", [ "v-33.6", "h-33.6", "v-9.6", "h-14.4" ] ],
[ "sr2:Q4", "sevseg2:E", "green", [ "v-28.8", "h-28.8", "v-9.6", "h-14.4" ] ],
[ "sr2:Q5", "sevseg2:F", "green", [ "v-24", "h-24", "v-9.6", "h-24", "v-110.4", "h19.2" ] ],
[ "sr2:Q6", "sevseg2:G", "green", [ "v-19.2", "h-43.2", "v-115.2", "h14.4" ] ],
[ "sr1:GND", "sr2:GND", "black", [ "v-9.6", "h96" ] ],
[ "sr1:Q1", "sevseg1:B", "green", [ "v-134.4", "h-19.2" ] ],
[ "sr1:Q2", "sevseg1:C", "green", [ "v-38.4", "h-19.2" ] ],
[ "sr1:Q3", "sevseg1:D", "green", [ "v-33.6", "h-24" ] ],
[ "sr1:Q4", "sevseg1:E", "green", [ "v-28.8", "h-28.8" ] ],
[ "esp:GND.1", "sr1:GND", "black", [ "v-9.6", "h134.4" ] ],
[ "sr1:GND", "sr1:OE", "black", [ "v-9.6", "h-9.6", "v67.2", "h172.8" ] ],
[ "sr1:Q0", "sevseg1:A", "green", [ "v65", "h-76.8", "v-240", "h57.6" ] ],
[ "sr1:Q5", "sevseg1:F", "green", [ "v-24", "h-19.2", "v-110.4", "h19.2" ] ],
[ "sr1:Q6", "sevseg1:G", "green", [ "v-19.2", "h-14.4", "v-110.4", "h14.4" ] ],
[ "esp:6", "r1:2", "orange", [ "v7.18", "h-115.2" ] ],
[ "r1:1", "led-red:A", "orange", [ "v-105.6", "h-9.6" ] ],
[ "esp:5", "r2:2", "green", [ "v11.98", "h-115.2" ] ],
[ "r2:1", "led-green:A", "green", [ "h0" ] ],
[ "r3:2", "esp:8", "gold", [ "v3.6", "h-115.2" ] ],
[ "r3:1", "led-yellow:A", "gold", [ "h0" ] ],
[ "r4:2", "esp:7", "blue", [ "v46.8", "h-144" ] ],
[ "r4:1", "led-blue:A", "blue", [ "v-105.6", "h-9.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for esp32-c3 alarm clock | #include <Keypad.h>
#include "Clock.h"
#include "Weather.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
uint8_t state;
unsigned long clockTimer;
unsigned long weatherAPItimer;
unsigned long weatherDisplayTimer;
const char* password = "";
const char* ssid = "Wokwi-GUEST";
uint8_t valIndex;
uint8_t cursorPos;
char entered_value [6];
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 1, 0, 3, 2 };
uint8_t rowPins[ROWS] = { 4, 5, 6, 7 };
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Clock rtc(&lcd);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void enterTime()
{
state = 1;
memset(&entered_value[0], 0, sizeof(entered_value));
cursorPos = 0;
valIndex = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set clock then ");
lcd.setCursor(0, 1);
lcd.print("press # to save.");
delay(3000);
lcd.clear();
}
void enterAlarm()
{
state = 2;
memset(&entered_value[0], 0, sizeof(entered_value));
cursorPos = 0;
valIndex = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter alarm time");
lcd.setCursor(0, 1);
lcd.print("press # to save.");
delay(3000);
lcd.clear();
}
void nextChar(char key)
{
if (valIndex < 6)
{
entered_value[valIndex] = key;
lcd.setCursor(0, 0);
lcd.print(entered_value);
cursorPos++;
valIndex++;
}
}
void eraseChar()
{
if (valIndex > 0 )
{
valIndex--;
cursorPos--;
entered_value[valIndex] = '\0';
lcd.setCursor(cursorPos, 0);
lcd.print(' ');
lcd.setCursor(cursorPos, 0);
}
}
void keyPadState0()
{
char key = keypad.getKey();
switch(key)
{
case 'A':
enterAlarm();
break;
case 'C':
enterTime();
break;
case '#':
rtc.silence();
break;
case '*':
rtc.addToSnooze();
break;
}
}
void keyPadState1()
{
char key = keypad.getKey();
switch(key)
{
case '#':
rtc.setTime(entered_value);
state = 0;
break;
case '*':
eraseChar();
break;
default:
if (isDigit(key))
{
nextChar(key);
}
break;
}
}
void keyPadState2()
{
char key = keypad.getKey();
switch(key)
{
case '#':
rtc.setAlarm(entered_value);
state = 0;
break;
case '*':
eraseChar();
break;
default:
if (isDigit(key))
{
nextChar(key);
}
break;
}
}
void getInput()
{
switch (state)
{
case 0:
keyPadState0();
break;
case 1:
keyPadState1();
break;
case 2:
keyPadState2();
break;
}
}
void setup()
{
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
Wire.begin(18, 19);
lcd.init();
lcd.backlight();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
lcd.print(".");
delay(1000);
}
lcd.clear();
if (!rtc.setTimeFromAPI())
{
char t [] = __TIME__;
char compileTime [] = { t[0], t[1], t[3], t[4], t[6], t[7] };
rtc.setTime(compileTime);
}
getWeather(lcd);
printWeather(lcd);
}
void loop()
{
if (state == 0)
{
unsigned long millisNow = millis();
if (millisNow - clockTimer >= 1000)
{
clockTimer = millisNow;
rtc.updateClock();
}
if (millisNow - weatherDisplayTimer >= 10000)
{
weatherDisplayTimer = millisNow;
printWeather(lcd);
}
if (millisNow - weatherAPItimer >= 3600000)
{
weatherAPItimer = millisNow;
getWeather(lcd);
}
}
getInput();
} | {
"version": 1,
"author": "Maverick",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-c3-devkitm-1",
"id": "esp",
"top": -454.69,
"left": 262.1,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-membrane-keypad",
"id": "keypad1",
"top": -824.35,
"left": 162.4,
"attrs": {}
},
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": -444.26,
"left": 414.36,
"attrs": { "pins": "i2c" }
},
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -479.95,
"left": 71.13,
"rotate": 270,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": -380.49,
"left": 88.44,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": -250.19,
"left": 181.17,
"rotate": 180,
"attrs": { "value": "220" }
},
{
"type": "wokwi-text",
"id": "A",
"top": -772.62,
"left": 404.66,
"attrs": { "text": "<------- Set Alarm" }
},
{
"type": "wokwi-text",
"id": "C",
"top": -666.01,
"left": 405.29,
"attrs": { "text": "<------- Set Clock" }
},
{ "type": "wokwi-text", "id": "|1", "top": -578.59, "left": 322.96, "attrs": { "text": "|" } },
{ "type": "wokwi-text", "id": "^1", "top": -581.3, "left": 321.71, "attrs": { "text": "^" } },
{
"type": "wokwi-text",
"id": "#",
"top": -572.97,
"left": 326.2,
"attrs": { "text": "------------------------------ Submit / Silence Alarm" }
},
{
"type": "wokwi-text",
"id": "*",
"top": -572.62,
"left": 22.16,
"attrs": { "text": "Erase / Snooze-----------------" }
},
{ "type": "wokwi-text", "id": "^2", "top": -580.39, "left": 206.9, "attrs": { "text": "^" } },
{ "type": "wokwi-text", "id": "|2", "top": -579.17, "left": 208.11, "attrs": { "text": "|" } }
],
"connections": [
[ "esp:1", "keypad1:C1", "cyan", [ "v-18.48", "h15.7" ] ],
[ "esp:0", "keypad1:C2", "yellow", [ "v-9.6", "h10.51" ] ],
[ "esp:3", "keypad1:C3", "limegreen", [ "v-9.59", "h-13.05" ] ],
[ "esp:2", "keypad1:C4", "magenta", [ "v-18.48", "h-12.9" ] ],
[ "esp:4", "keypad1:R1", "#8f4814", [ "v7.96", "h-84.23", "v-144.63", "h71.03" ] ],
[ "esp:5", "keypad1:R2", "orange", [ "v14.55", "h-87.01", "v-144.5", "h74.21" ] ],
[ "esp:6", "keypad1:R3", "green", [ "v20.84", "h-89.17", "v-143.71", "h76.27" ] ],
[ "esp:7", "keypad1:R4", "purple", [ "v27.13", "h-91.33", "v-143.27", "h78.33" ] ],
[ "lcd1:GND", "esp:GND.1", "black", [ "h-11.48", "v-27.23", "h-35.28" ] ],
[ "lcd1:VCC", "esp:3V3.1", "red", [ "h-18.59", "v-30.7", "h-37.77" ] ],
[ "lcd1:SCL", "esp:19", "limegreen", [ "h-10.71", "v117.57", "h-160.66", "v-62.9" ] ],
[ "lcd1:SDA", "esp:18", "cyan", [ "h-19.02", "v119.09", "h-142.94" ] ],
[ "led1:A", "esp:8", "gray", [ "v80.81", "h210.16" ] ],
[ "led1:C", "r1:2", "black", [ "v93.24", "h119.4" ] ],
[ "esp:GND.8", "r1:1", "black", [ "v0" ] ],
[ "esp:10", "bz1:2", "white", [ "v-19.85", "h-127.94" ] ],
[ "bz1:1", "esp:GND.4", "black", [ "h7.46", "v-0.31", "h107.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ESP32-S2 | void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [ { "id": "esp", "type": "board-esp32-s2-devkitm-1" } ],
"connections": [ [ "esp:TX", "$serialMonitor:RX", "" ], [ "esp:RX", "$serialMonitor:TX", "" ] ]
} |
Generate a diagram.json code for ESP32-S3 | void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S3!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [ { "id": "esp", "type": "board-esp32-s3-devkitc-1" } ],
"connections": [ [ "esp:TX", "$serialMonitor:RX", "" ], [ "esp:RX", "$serialMonitor:TX", "" ] ]
} |
Generate a diagram.json code for ESP32-C3 | void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-C3!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [ { "id": "esp", "type": "board-esp32-c3-devkitm-1" } ],
"connections": [ [ "esp:TX", "$serialMonitor:RX", "" ], [ "esp:RX", "$serialMonitor:TX", "" ] ]
} |
Generate a diagram.json code for ESP32-C6 | void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-C6!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [ { "id": "esp", "type": "board-esp32-c6-devkitc-1" } ],
"connections": [ [ "esp:TX", "$serialMonitor:RX", "" ], [ "esp:RX", "$serialMonitor:TX", "" ] ]
} |
Generate a diagram.json code for ESP32-H2 | void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-H2!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [ { "id": "esp", "type": "board-esp32-h2-devkitm-1" } ],
"connections": [ [ "esp:TX", "$serialMonitor:RX", "" ], [ "esp:RX", "$serialMonitor:TX", "" ] ]
} |
Generate a diagram.json code for ESP32-S3-box3 | /*
ESP32-S3-BOX + Adafruit_ILI9341 Example
https://wokwi.com/projects/399780007361137665
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 4
#define TFT_CS 5
#define TFT_MOSI 6
#define TFT_CLK 7
#define TFT_CTRL 45
#define TFT_RST 48
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST);
void setup() {
Serial.begin(115200);
Serial.println("Welcome to Wokwi, ESP32-S3-BOX-3");
pinMode(TFT_CTRL, OUTPUT);
digitalWrite(TFT_CTRL, HIGH);
tft.begin();
// Configure the direction of the display
const uint8_t mode = 0xc8;
tft.sendCommand(ILI9341_MADCTL, &mode, 1);
tft.setCursor(88, 60);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("S3-BOX-3");
}
const uint32_t colors[] = {
ILI9341_GREEN,
ILI9341_CYAN,
ILI9341_MAGENTA,
ILI9341_YELLOW,
};
uint8_t colorIndex = 0;
void loop() {
tft.setTextSize(2);
tft.setCursor(36, 106);
tft.setTextColor(colors[colorIndex++ % 4]);
tft.println("Welcome to Wokwi!");
delay(250);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-s3-box-3",
"id": "esp32",
"top": -24.91,
"left": -388.54,
"attrs": { "psramSize": "16", "flashSize": "16" }
}
],
"connections": [ [ "$serialMonitor:RX", "esp32:G14", "", [] ], [ "$serialMonitor:TX", "esp32:G11", "", [] ] ],
"dependencies": {}
} |
Generate a diagram.json code for core-s3 | #include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 35
#define TFT_CS 3
#define TFT_MOSI 37
#define TFT_CLK 36
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK);
void setup() {
Serial.begin(115200);
Serial.println("Welcome to Wokwi, CORE-S3");
tft.begin();
// Configure the direction of the display
const uint8_t mode = 0xc8;
tft.sendCommand(ILI9341_MADCTL, &mode, 1);
tft.setCursor(88, 60);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("CORE-S3");
}
const uint32_t colors[] = {
ILI9341_GREEN,
ILI9341_CYAN,
ILI9341_MAGENTA,
ILI9341_YELLOW,
};
uint8_t colorIndex = 0;
void loop() {
tft.setTextSize(2);
tft.setCursor(36, 106);
tft.setTextColor(colors[colorIndex++ % 4]);
tft.println("Welcome to Wokwi!");
delay(250);
} | {
"version": 1,
"author": "Ambitious maker",
"editor": "wokwi",
"parts": [
{
"type": "board-m5stack-core-s3",
"id": "board",
"top": -179.23,
"left": -62.59,
"attrs": {
"serialInterface": "USB_SERIAL_JTAG"
}
}
],
"connections": [
[
"$serialMonitor:RX",
"board:G43",
"",
[]
],
[
"$serialMonitor:TX",
"board:G44",
"",
[]
]
],
"dependencies": {}
} |
Generate a diagram.json code for RGB LED sequence using XIOA-ESP32-C3 | void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
Serial.println("");
Serial.println("Hello, XIAO ESP32-C3!");
Serial.println("Welcome to Wokwi :-)");
}
void loop() {
Serial.println("Red");
digitalWrite(D2, HIGH);
delay(500);
digitalWrite(D2, LOW);
Serial.println("Green");
digitalWrite(D3, HIGH);
delay(500);
digitalWrite(D3, LOW);
Serial.println("Blue");
digitalWrite(D4, HIGH);
delay(500);
digitalWrite(D4, LOW);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "board-xiao-esp32-c3", "id": "esp", "top": 38.97, "left": 13.78, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": 6, "left": -73, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 147.95,
"left": -57.6,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 15.6,
"left": -101.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 167.15,
"left": -57.6,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-junction", "id": "j1", "top": 148.8, "left": 24, "attrs": {} },
{
"type": "wokwi-led",
"id": "led3",
"top": 25.2,
"left": -130.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 186.35,
"left": -57.6,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "esp:D2", "led1:A", "green", [ "h0" ] ],
[ "led1:C", "r1:1", "black", [ "v0" ] ],
[ "esp:D3", "led2:A", "green", [ "h0" ] ],
[ "led2:C", "r2:1", "black", [ "v0" ] ],
[ "r2:2", "j1:J", "black", [ "v0", "h27.6" ] ],
[ "j1:J", "r1:2", "black", [ "v0" ] ],
[ "esp:GND", "j1:J", "black", [ "h19.82", "v86.4" ] ],
[ "led3:A", "esp:D4", "green", [ "v0" ] ],
[ "led3:C", "r3:1", "black", [ "v0" ] ],
[ "j1:J", "r3:2", "black", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for RGB LED sequence with XIAO-ESP32-C6 | void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
Serial.println("");
Serial.println("Hello, XIAO ESP32-C6!");
Serial.println("Welcome to Wokwi :-)");
}
void loop() {
Serial.println("Red");
digitalWrite(D2, HIGH);
delay(500);
digitalWrite(D2, LOW);
Serial.println("Green");
digitalWrite(D3, HIGH);
delay(500);
digitalWrite(D3, LOW);
Serial.println("Blue");
digitalWrite(D4, HIGH);
delay(500);
digitalWrite(D4, LOW);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "board-xiao-esp32-c6", "id": "esp", "top": 38.97, "left": 13.78, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": 6, "left": -73, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 147.95,
"left": -57.6,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 15.6,
"left": -101.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 167.15,
"left": -57.6,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-junction", "id": "j1", "top": 148.8, "left": 24, "attrs": {} },
{
"type": "wokwi-led",
"id": "led3",
"top": 25.2,
"left": -130.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 186.35,
"left": -57.6,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "esp:D2", "led1:A", "green", [ "h0" ] ],
[ "led1:C", "r1:1", "black", [ "v0" ] ],
[ "esp:D3", "led2:A", "green", [ "h0" ] ],
[ "led2:C", "r2:1", "black", [ "v0" ] ],
[ "r2:2", "j1:J", "black", [ "v0", "h27.6" ] ],
[ "j1:J", "r1:2", "black", [ "v0" ] ],
[ "esp:GND", "j1:J", "black", [ "h19.82", "v86.4" ] ],
[ "led3:A", "esp:D4", "green", [ "v0" ] ],
[ "led3:C", "r3:1", "black", [ "v0" ] ],
[ "j1:J", "r3:2", "black", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for RGB LED sequence with XIOA-ESP32-S3 | void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
Serial.println("");
Serial.println("Hello, XIAO ESP32-S3!");
Serial.println("Welcome to Wokwi :-)");
}
void loop() {
Serial.println("Red");
digitalWrite(D2, HIGH);
delay(500);
digitalWrite(D2, LOW);
Serial.println("Green");
digitalWrite(D3, HIGH);
delay(500);
digitalWrite(D3, LOW);
Serial.println("Blue");
digitalWrite(D4, HIGH);
delay(500);
digitalWrite(D4, LOW);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "board-xiao-esp32-s3", "id": "esp", "top": 38.97, "left": 13.78, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": 6, "left": -73, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 147.95,
"left": -57.6,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 15.6,
"left": -101.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 167.15,
"left": -57.6,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-junction", "id": "j1", "top": 148.8, "left": 24, "attrs": {} },
{
"type": "wokwi-led",
"id": "led3",
"top": 25.2,
"left": -130.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 186.35,
"left": -57.6,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "esp:D2", "led1:A", "green", [ "h0" ] ],
[ "led1:C", "r1:1", "black", [ "v0" ] ],
[ "esp:D3", "led2:A", "green", [ "h0" ] ],
[ "led2:C", "r2:1", "black", [ "v0" ] ],
[ "r2:2", "j1:J", "black", [ "v0", "h27.6" ] ],
[ "j1:J", "r1:2", "black", [ "v0" ] ],
[ "esp:GND", "j1:J", "black", [ "h19.82", "v86.4" ] ],
[ "led3:A", "esp:D4", "green", [ "v0" ] ],
[ "led3:C", "r3:1", "black", [ "v0" ] ],
[ "j1:J", "r3:2", "black", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ESP32 wifi | #include "WiFi.h"
void setup() {
Serial.begin(115200);
Serial.println("Initializing WiFi...");
WiFi.mode(WIFI_STA);
Serial.println("Setup done!");
}
void loop() {
Serial.println("Scanning...");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Scan done!");
if (n == 0) {
Serial.println("No networks found.");
} else {
Serial.println();
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
} | {
"version": 1,
"author": "Carl B",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb2", "top": 198.6, "left": -35.6, "attrs": {} },
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 219.14,
"left": 32.5,
"rotate": 90,
"attrs": {}
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "esp:GND.2", "bb2:bn.25", "black", [ "v38.4", "h144", "v-19.2" ] ],
[ "esp:3V3", "bb2:bp.25", "red", [ "v0.15", "h134.4", "v105.6" ] ],
[ "esp:12", "bb2:7t.d", "", [ "$bb" ] ],
[ "esp:13", "bb2:5t.d", "", [ "$bb" ] ],
[ "esp:14", "bb2:8t.d", "", [ "$bb" ] ],
[ "esp:25", "bb2:11t.d", "", [ "$bb" ] ],
[ "esp:26", "bb2:10t.d", "", [ "$bb" ] ],
[ "esp:27", "bb2:9t.d", "", [ "$bb" ] ],
[ "esp:32", "bb2:13t.d", "", [ "$bb" ] ],
[ "esp:33", "bb2:12t.d", "", [ "$bb" ] ],
[ "esp:34", "bb2:15t.d", "", [ "$bb" ] ],
[ "esp:35", "bb2:14t.d", "", [ "$bb" ] ],
[ "esp:3V3", "bb2:19t.d", "", [ "$bb" ] ],
[ "esp:EN", "bb2:18t.d", "", [ "$bb" ] ],
[ "esp:VP", "bb2:17t.d", "", [ "$bb" ] ],
[ "esp:VN", "bb2:16t.d", "", [ "$bb" ] ],
[ "esp:GND.1", "bb2:6t.d", "", [ "$bb" ] ],
[ "esp:D2", "bb2:4t.d", "", [ "$bb" ] ],
[ "esp:D3", "bb2:3t.d", "", [ "$bb" ] ],
[ "esp:CMD", "bb2:2t.d", "", [ "$bb" ] ],
[ "esp:5V", "bb2:1t.d", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ultrasonic distance measurement using LED indicators(ESP32) | // Define GPIO pins for Ultrasonic Sensor and LED
#define TRIG_PIN 12 // GPIO for Trig pin
#define ECHO_PIN 14 // GPIO for Echo pin
#define RED_LED 27
#define YELLOW_LED 26
#define GREEN_LED 25
void setup() {
// Set GPIO pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
// Start Serial Monitor
Serial.begin(115200);
}
void loop() {
// Measure distance
float distance = measureDistance();
// Display distance on Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check distance
if (distance < 100) {
digitalWrite(GREEN_LED, HIGH); // Turn on LED
} else {
digitalWrite(GREEN_LED, LOW); // Turn off LED
}
if (distance > 100) {
digitalWrite(YELLOW_LED, HIGH);
} else {
digitalWrite(YELLOW_LED, LOW);
}
if (distance > 202) {
digitalWrite(RED_LED, HIGH);
} else {
digitalWrite(RED_LED, LOW);
}
delay(500); // Delay for 500 milliseconds
}
// Function to measure distance
float measureDistance() {
// Send Trig signal
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read Echo time
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance (unit: centimeters)
float distance = duration * 0.034 / 2;
return distance;
} | {
"version": 1,
"author": "KAMONPHOB WONGPAN",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -70.2, "left": -150.8, "attrs": {} },
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 103.94,
"left": -92.3,
"rotate": 90,
"attrs": {}
},
{ "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -94.5, "left": -138.5, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -42, "left": 71, "attrs": { "color": "yellow" } },
{ "type": "wokwi-led", "id": "led2", "top": -42, "left": 42.2, "attrs": { "color": "red" } },
{ "type": "wokwi-led", "id": "led3", "top": -42, "left": 99.8, "attrs": { "color": "green" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 33.6,
"left": 37.85,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 33.6,
"left": 66.65,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 33.6,
"left": 95.45,
"rotate": 90,
"attrs": { "value": "220" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "ultrasonic1:VCC", "bb1:bp.4", "red", [ "v28.8", "h-19.2" ] ],
[ "bb1:bp.1", "esp:5V", "green", [ "v-0.9", "h-17.6" ] ],
[ "bb1:bn.1", "esp:GND.1", "black", [ "v27.5", "h30.4" ] ],
[ "bb1:8t.e", "esp:12", "orange", [ "v124.8", "h-19.2" ] ],
[ "bb1:9t.e", "esp:14", "gold", [ "v124.8", "h-19.2" ] ],
[ "bb1:10t.e", "bb1:bn.6", "purple", [ "v0" ] ],
[ "bb1:tn.16", "bb1:20t.a", "green", [ "v0" ] ],
[ "bb1:tn.19", "bb1:23t.a", "green", [ "v0" ] ],
[ "bb1:tn.21", "bb1:26t.a", "green", [ "v0" ] ],
[ "r1:2", "esp:27", "red", [ "h-96", "v85.2", "h-28.8" ] ],
[ "r3:2", "esp:25", "green", [ "h0", "v85.2", "h-153.6" ] ],
[ "bb1:tn.25", "esp:GND.2", "black", [ "v-0.1", "h49.6", "v297.6" ] ],
[ "r2:2", "esp:26", "gold", [ "v8.4", "h-76.8", "v69.6", "h-67.2", "v16.65" ] ],
[ "ultrasonic1:VCC", "bb1:7t.c", "", [ "$bb" ] ],
[ "ultrasonic1:TRIG", "bb1:8t.c", "", [ "$bb" ] ],
[ "ultrasonic1:ECHO", "bb1:9t.c", "", [ "$bb" ] ],
[ "ultrasonic1:GND", "bb1:10t.c", "", [ "$bb" ] ],
[ "led1:A", "bb1:24t.c", "", [ "$bb" ] ],
[ "led1:C", "bb1:23t.c", "", [ "$bb" ] ],
[ "led2:A", "bb1:21t.c", "", [ "$bb" ] ],
[ "led2:C", "bb1:20t.c", "", [ "$bb" ] ],
[ "led3:A", "bb1:27t.c", "", [ "$bb" ] ],
[ "led3:C", "bb1:26t.c", "", [ "$bb" ] ],
[ "r1:1", "bb1:21t.d", "", [ "$bb" ] ],
[ "r1:2", "bb1:21b.h", "", [ "$bb" ] ],
[ "r2:1", "bb1:24t.d", "", [ "$bb" ] ],
[ "r2:2", "bb1:24b.h", "", [ "$bb" ] ],
[ "r3:1", "bb1:27t.d", "", [ "$bb" ] ],
[ "r3:2", "bb1:27b.h", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Temperature and Humidity using DHT11 with ESP32 | #include "DHT.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define R_LED 26
#define B_LED 25
#define DHTPIN 19
#define DHTTYPE DHT22
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
pinMode(R_LED, OUTPUT);
digitalWrite(R_LED, LOW);
pinMode(B_LED, OUTPUT);
digitalWrite(B_LED, LOW);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" OLED 0.96 TESTER");
display.display();
}
void loop() {
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
display.clearDisplay();
display.setCursor(0,2);
display.print("Temp : ");
display.print(String(t));
display.print(" %");
display.setCursor(0,12);
display.print("Humid : ");
display.print(String(h));
display.print(" *C");
display.display();
if(t > 50){
digitalWrite(R_LED, HIGH);
}else if(h > 50){
digitalWrite(B_LED, HIGH);
}else if (t < 49){
digitalWrite(R_LED, LOW);
}else if (h < 49){
digitalWrite(B_LED, LOW);
}
} | {
"version": 1,
"author": "ADITHEP TONGRUENG",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -147, "left": -160.4, "attrs": {} },
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 76.8, "left": -52.76, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -118.8,
"left": -82.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -118.8,
"left": -121,
"attrs": { "color": "red" }
},
{ "type": "wokwi-dht22", "id": "dht1", "top": -143.7, "left": 42.6, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": -43.2,
"left": -86.95,
"rotate": 90,
"attrs": { "value": "470" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": -43.2,
"left": -125.35,
"rotate": 90,
"attrs": { "value": "470" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": -14.95,
"left": 8.2,
"rotate": 180,
"attrs": { "value": "4700" }
},
{
"type": "board-ssd1306",
"id": "oled1",
"top": 166.34,
"left": 115.43,
"attrs": { "i2cAddress": "0x3c" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "bb1:21b.g", "bb1:16b.g", "green", [ "v0" ] ],
[ "bb1:16b.j", "bb1:bp.13", "red", [ "v0" ] ],
[ "esp:GND.2", "bb1:bn.25", "black", [ "v0", "h96" ] ],
[ "esp:3V3", "bb1:bp.1", "red", [ "h-95.85", "v-38.4" ] ],
[ "bb1:4t.e", "bb1:bn.3", "black", [ "v0" ] ],
[ "bb1:8t.e", "bb1:bn.6", "black", [ "v0" ] ],
[ "bb1:22b.j", "esp:19", "blue", [ "v163.2", "h-9.6" ] ],
[ "oled1:GND", "bb1:bn.22", "black", [ "v-28.8", "h-28.8" ] ],
[ "oled1:VCC", "bb1:bp.21", "red", [ "v-76.8", "h-57.45" ] ],
[ "esp:22", "oled1:SCL", "cyan", [ "h0" ] ],
[ "esp:21", "oled1:SDA", "magenta", [ "h134.4" ] ],
[ "esp:26", "bb1:5b.j", "green", [ "h0" ] ],
[ "esp:25", "bb1:9b.j", "green", [ "v0", "h-28.65", "v-163.2", "h19.2" ] ],
[ "bb1:24b.j", "bb1:bn.20", "black", [ "v0" ] ],
[ "led2:A", "bb1:5t.c", "", [ "$bb" ] ],
[ "led2:C", "bb1:4t.c", "", [ "$bb" ] ],
[ "dht1:VCC", "bb1:21b.f", "", [ "$bb" ] ],
[ "dht1:SDA", "bb1:22b.f", "", [ "$bb" ] ],
[ "dht1:NC", "bb1:23b.f", "", [ "$bb" ] ],
[ "dht1:GND", "bb1:24b.f", "", [ "$bb" ] ],
[ "r1:1", "bb1:9t.d", "", [ "$bb" ] ],
[ "r1:2", "bb1:9b.h", "", [ "$bb" ] ],
[ "r2:1", "bb1:5t.d", "", [ "$bb" ] ],
[ "r2:2", "bb1:5b.h", "", [ "$bb" ] ],
[ "r3:1", "bb1:22b.h", "", [ "$bb" ] ],
[ "r3:2", "bb1:16b.h", "", [ "$bb" ] ],
[ "led1:A", "bb1:9t.c", "", [ "$bb" ] ],
[ "led1:C", "bb1:8t.c", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Ultrasoni distamce measurement using ESP32 | #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define TRIG_PIN 26
#define ECHO_PIN 25
float duration_us, distance_cm;
void setup(){
lcd.init();
lcd.backlight();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop()
{
digitalWrite(TRIG_PIN, HIGH );
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.017* duration_us;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.print(distance_cm);
delay(500);
} | {
"version": 1,
"author": "manlika Phunut",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -9.6, "left": -4.76, "attrs": {} },
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": -80,
"left": 149.6,
"attrs": { "pins": "i2c" }
},
{ "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -56.1, "left": -215.3, "attrs": {} }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "ultrasonic1:VCC", "esp:5V", "red", [ "v0" ] ],
[ "lcd1:VCC", "esp:5V", "red", [ "h-9.6", "v268.9", "h-153.6", "v-38.4" ] ],
[ "lcd1:GND", "esp:GND.2", "black", [ "h0" ] ],
[ "ultrasonic1:GND", "esp:GND.1", "black", [ "v0" ] ],
[ "ultrasonic1:TRIG", "esp:26", "blue", [ "v0" ] ],
[ "ultrasonic1:ECHO", "esp:25", "purple", [ "v0" ] ],
[ "lcd1:SDA", "esp:21", "yellow", [ "h-38.4", "v96.2" ] ],
[ "lcd1:SCL", "esp:22", "magenta", [ "h-48", "v57.9" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for 8-bit counter | #include <Arduino.h>
const int buttonPin = 14; // GPIO pin for button
const int ledPin1 = 26; // GPIO pin for first LED
const int ledPin2 = 25; // GPIO pin for second LED
const int ledPin3 = 33; // GPIO pin for third LED
int buttonState = 0; // variable for reading the button status
int lastButtonState = 0; // previous button state
int clickCount = 0; // variable to count button clicks
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 0; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(115200);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
clickCount = (clickCount + 1) % 8; // increment and wrap click count
Serial.println(clickCount);
updateLEDs(); // immediately update the LEDs
}
}
}
lastButtonState = reading;
}
void updateLEDs() {
// Reset all LEDs to LOW
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
// Update LEDs based on click count
switch (clickCount) {
case 1:
digitalWrite(ledPin3, HIGH);
break;
case 2:
digitalWrite(ledPin2, HIGH);
break;
case 3:
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
break;
case 4:
digitalWrite(ledPin1, HIGH);
break;
case 5:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin3, HIGH);
break;
case 6:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
break;
case 7:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
break;
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -38.4, "left": 72.04, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -13,
"left": -182.4,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": -99.6,
"left": -101.8,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -99.6,
"left": -63.4,
"attrs": { "color": "red" }
},
{ "type": "wokwi-led", "id": "led3", "top": -99.6, "left": -25, "attrs": { "color": "red" } }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "btn1:2.r", "esp:GND.1", "green", [ "h67.4", "v77" ] ],
[ "esp:GND.3", "led1:C", "black", [ "h19.2", "v153.6", "h0", "v-9.6", "h-278.4" ] ],
[ "esp:GND.3", "led2:C", "black", [ "h19.2", "v144", "h-240" ] ],
[ "esp:GND.3", "led3:C", "black", [ "h28.8", "v144", "h-211.2" ] ],
[ "esp:14", "btn1:1.r", "green", [ "h-172.65", "v-96", "h-19.2" ] ],
[ "esp:26", "led1:A", "green", [ "h-153.45", "v0" ] ],
[ "esp:25", "led2:A", "green", [ "v0", "h-143.85" ] ],
[ "esp:33", "led3:A", "green", [ "h-76.65", "v-9.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ESP32 base IoT system | #include <WiFi.h>
#include <PubSubClient.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT server details
const char* mqttServer = "broker.hivemq.com";
int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";
const char* mqttClientID = "ESP32Client";
const char* topic = "DEIOT/room/3395498B";
// Create WiFi and MQTT client objects
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// Define GPIO pins for LEDs and analog input
const int ledPin = 32;
const int yellowLedPin = 22;
const int analogPin = 12;
// Timing variables for LED blinking and analog reading
long previousMillisR = 0;
int intervalR = 1000;
long previousMillisAnalog = 0;
int intervalAnalog = 2000;
void setup() {
// Set GPIO pins as outputs
pinMode(ledPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
// Start Serial Monitor
Serial.begin(115200);
// Connect to WiFi
setupWiFi();
// Set MQTT server and callback function
client.setServer(mqttServer, mqttPort);
client.setCallback(mqttCallback);
// Connect to MQTT broker
while (!client.connected()) {
if (client.connect(mqttClientID)) {
Serial.println("Connected to MQTT Broker!");
} else {
Serial.print("MQTT connection failed. State: ");
Serial.println(client.state());
delay(2000);
}
}
}
void loop() {
long currentMillis = millis();
// Blink LED at regular intervals
if (currentMillis - previousMillisR >= intervalR) {
previousMillisR = currentMillis;
digitalWrite(ledPin, !digitalRead(ledPin));
}
// Read analog value and publish to MQTT at regular intervals
if (currentMillis - previousMillisAnalog >= intervalAnalog) {
previousMillisAnalog = currentMillis;
int analogValue = analogRead(analogPin);
Serial.print("Analog Value from pin ");
Serial.print(analogPin);
Serial.print(": ");
Serial.println(analogValue);
String payload = String(analogValue);
if (client.publish(topic, payload.c_str())) {
Serial.println("Published to MQTT");
} else {
Serial.println("Failed to publish to MQTT");
}
}
// Handle MQTT client loop
client.loop();
}
void setupWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to WiFi with IP: ");
Serial.println(WiFi.localIP());
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.print("Message received on topic ");
Serial.print(topic);
Serial.print(": ");
Serial.println(message);
int receivedValue = message.toInt();
if (receivedValue < 1000) {
digitalWrite(yellowLedPin, HIGH);
Serial.println("yellow LED ON: Value is less than 1000");
} else {
digitalWrite(yellowLedPin, LOW);
Serial.println("yellow LED OFF: Value is greater than or equal to 1000");
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 57.6, "left": 91.24, "attrs": {} },
{
"type": "wokwi-led",
"id": "led2",
"top": -22.8,
"left": -130.6,
"attrs": { "color": "red" }
},
{ "type": "wokwi-potentiometer", "id": "pot1", "top": -20.5, "left": -48.2, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 15.6,
"left": 263,
"attrs": { "color": "yellow" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "led2:A", "esp:32", "green", [ "v0" ] ],
[ "led2:C", "esp:GND.1", "black", [ "v0" ] ],
[ "pot1:SIG", "esp:12", "green", [ "v105.6", "h-0.4", "v38.4" ] ],
[ "esp:GND.2", "pot1:GND", "black", [ "v-48", "h-134.4", "v28.8", "h-67.2" ] ],
[ "pot1:VCC", "esp:3V3", "red", [ "v0" ] ],
[ "led1:C", "esp:22", "green", [ "v0" ] ],
[ "esp:GND.2", "led1:A", "black", [ "v0", "h96" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Smart energy monitoring system using ESP32 | #include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// OLED display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API information
const char* server = "https://api.thingspeak.com";
const char* apiKey = "CDJEFTTGRAFUMV67"; // Replace with your ThingSpeak Write API Key
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextColor(WHITE);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
}
void sendDataToThingSpeak() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "/update?api_key=" + apiKey;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.print("Error sending data: ");
Serial.println(http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("WiFi not connected! Unable to send data.");
}
}
void loop() {
static unsigned long lastSendTime = 0;
if (millis() - lastSendTime >= 10000) { // Send data every 10 seconds
sendDataToThingSpeak();
lastSendTime = millis();
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Sending data...");
display.display();
} | {
"version": 1,
"author": "Faiz Jefri",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": -33.7, "left": 14.2, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": -43.2,
"left": 249.05,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 43.2,
"left": 249.05,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "board-ssd1306",
"id": "oled1",
"top": -131.26,
"left": 345.83,
"attrs": { "i2cAddress": "0x3c" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 3.95,
"left": 316.8,
"attrs": { "value": "100" }
},
{ "type": "wokwi-potentiometer", "id": "pot1", "top": -106.9, "left": -173, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot2", "top": 200.3, "left": -192.2, "attrs": {} }
],
"connections": [
[ "esp:TX0", "$serialMonitor:RX", "", [] ],
[ "esp:RX0", "$serialMonitor:TX", "", [] ],
[ "r2:2", "esp:3V3", "red", [ "v46.8", "h-162.9" ] ],
[ "r1:1", "esp:GND.1", "green", [ "h-57.6", "v105.6" ] ],
[ "r2:1", "r1:2", "black", [ "h0" ] ],
[ "oled1:SCL", "esp:D22", "black", [ "h-162.9", "v57.9" ] ],
[ "oled1:SDA", "esp:D21", "black", [ "h-259.13", "v86.8" ] ],
[ "r3:1", "r1:2", "black", [ "v0", "h-28.8" ] ],
[ "pot1:VCC", "esp:VIN", "red", [ "v0" ] ],
[ "pot1:GND", "esp:GND.2", "green", [ "v0" ] ],
[ "pot2:VCC", "esp:VIN", "red", [ "v0" ] ],
[ "pot2:GND", "esp:GND.2", "green", [ "v0" ] ],
[ "pot1:SIG", "esp:D34", "black", [ "v0" ] ],
[ "pot2:SIG", "esp:D35", "black", [ "v-124.8", "h-0.4", "v-38.4" ] ],
[ "oled1:VCC", "esp:VIN", "red", [ "v-9.6", "h76.95", "v316.8", "h-451.2" ] ],
[ "oled1:GND", "esp:GND.1", "green", [ "v-19.2", "h96", "v268.8" ] ],
[ "esp:VP", "r3:2", "red", [ "h-19.2", "v-86.7", "h326.4", "v76.8", "h48" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for ESP32 based dual core system | #include <Arduino.h>
// Task handles for the two tasks
TaskHandle_t Task1;
TaskHandle_t Task2;
// GPIO pins for the two 7-segment displays
const int seg1Pins[] = {2, 4, 5, 18, 19, 21, 22};
const int seg2Pins[] = {23, 25, 26, 27, 14, 12, 13};
// Binary codes for digits 0-9 for 7-segment display
const byte digitCodes[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup() {
Serial.begin(115200);
// Set all segment pins as outputs
for (int i = 0; i < 7; i++) {
pinMode(seg1Pins[i], OUTPUT);
pinMode(seg2Pins[i], OUTPUT);
}
// Create Task1 on core 0
xTaskCreatePinnedToCore(
Task1code, // Function to implement the task
"Task1", // Name of the task
10000, // Stack size in words
NULL, // Task input parameter
1, // Priority of the task
&Task1, // Task handle
0 // Core where the task should run
);
delay(500);
// Create Task2 on core 1
xTaskCreatePinnedToCore(
Task2code, // Function to implement the task
"Task2", // Name of the task
10000, // Stack size in words
NULL, // Task input parameter
1, // Priority of the task
&Task2, // Task handle
1 // Core where the task should run
);
delay(500);
}
// Function to display a number on a 7-segment display
void displayNumber(const int *pins, int number) {
byte code = digitCodes[number];
for (int i = 0; i < 7; i++) {
digitalWrite(pins[i], (code >> i) & 0x01);
}
}
// Task1 code: Counts from 1 to 9 on the first 7-segment display
void Task1code(void *pvParameters) {
Serial.print("Task1 core: ");
Serial.println(xPortGetCoreID());
int count = 1;
for (;;) {
displayNumber(seg1Pins, count);
count++;
if (count > 9) count = 1;
delay(1500); // Delay for 1.5 seconds
}
}
// Task2 code: Counts from 9 to 1 on the second 7-segment display
void Task2code(void *pvParameters) {
Serial.print("Task2 core: ");
Serial.println(xPortGetCoreID());
int count = 9;
for (;;) {
displayNumber(seg2Pins, count);
count--;
if (count < 1) count = 9;
delay(500); // Delay for 0.5 seconds
}
}
void loop() {
// Empty loop as tasks are running independently
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -9.6, "left": 24.04, "attrs": {} },
{
"type": "wokwi-7segment",
"id": "sevseg1",
"top": 81.78,
"left": 225.88,
"attrs": { "common": "cathode" }
},
{
"type": "wokwi-7segment",
"id": "sevseg2",
"top": 100.98,
"left": -129.32,
"attrs": { "common": "cathode" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "sevseg1:B", "esp:4", "green", [ "v-8.76", "h-57.6", "v67.2", "h0" ] ],
[ "sevseg1:A", "esp:2", "green", [ "v-18.36", "h-57.6", "v96" ] ],
[ "sevseg1:C", "esp:5", "green", [ "v19.2", "h28.8", "v-115.2", "h-67.2", "v57.6" ] ],
[ "sevseg1:D", "esp:18", "green", [ "v19.2", "h-48", "v-67.2" ] ],
[ "sevseg1:E", "esp:19", "green", [ "v28.8", "h-48", "v-86.4" ] ],
[ "sevseg1:F", "esp:21", "green", [ "v-37.56", "h-67.2", "v38.4" ] ],
[ "sevseg1:G", "esp:22", "green", [ "v-47.16", "h-76.8", "v9.6" ] ],
[ "sevseg1:COM.1", "esp:GND.3", "green", [ "h0", "v38.4", "h-96", "v-105.6" ] ],
[ "sevseg2:A", "esp:23", "green", [ "v-143.16", "h230.4", "v67.2" ] ],
[ "sevseg2:B", "esp:25", "green", [ "v0" ] ],
[ "sevseg2:C", "esp:26", "green", [ "v19.2", "h19.2", "v-76.8" ] ],
[ "sevseg2:D", "esp:27", "green", [ "v28.8", "h48", "v-76.8" ] ],
[ "sevseg2:E", "esp:14", "green", [ "v38.4", "h67.2", "v-76.8" ] ],
[ "sevseg2:F", "esp:12", "green", [ "v-18.36", "h67.2", "v57.6" ] ],
[ "sevseg2:G", "esp:13", "green", [ "v0.84", "h-19.2", "v115.2", "h96", "v-57.6" ] ],
[ "sevseg2:COM.1", "esp:GND.1", "green", [ "h0", "v9.6", "h86.4", "v-28.8" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for temperature measurement using NTC sensor(ESP32) | const int tempPin = 14;
#include <LiquidCrystal.h>
LiquidCrystal lcd(18, 5, 17, 16, 4, 0);
void setup() {
Serial.begin(115200);
Serial.println("Hello");
lcd.begin(16, 2);
//lcd.print("ADC Example");
delay(1000);
}
void loop() {
int adc = analogRead(tempPin);
const float BETA = 3950.0; // should match the Beta Coefficient of the thermistor
float temp = 1 / (log(1 / (4096.0 / adc - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("temp: ");
lcd.println(String(temp)+char(223)+"C");
delay(2000);
} | {
"version": 1,
"author": "NAGA NANDHINI P",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 67.2, "left": -23.96, "attrs": {} },
{
"type": "wokwi-ntc-temperature-sensor",
"id": "ntc1",
"top": 117.8,
"left": -202.2,
"attrs": {}
},
{ "type": "wokwi-lcd1602", "id": "lcd1", "top": -92.57, "left": 35.2, "attrs": {} },
{ "type": "wokwi-vcc", "id": "vcc1", "top": 67.96, "left": 220.8, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd1", "top": 76.8, "left": 95.4, "attrs": {} }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "lcd1:RS", "esp:18", "green", [ "v134.4", "h-18.9" ] ],
[ "lcd1:E", "esp:5", "green", [ "v144", "h-38.4" ] ],
[ "esp:0", "lcd1:D7", "green", [ "h115.2", "v-182.4" ] ],
[ "lcd1:D6", "esp:4", "green", [ "v172.8", "h-105.4" ] ],
[ "lcd1:D5", "esp:16", "green", [ "v163.2", "h-95.9" ] ],
[ "lcd1:D4", "esp:17", "green", [ "v153.6", "h-86.4" ] ],
[ "lcd1:RW", "lcd1:K", "green", [ "v19.2", "h105.5" ] ],
[ "lcd1:RW", "gnd1:GND", "black", [ "v38.4", "h-0.1" ] ],
[ "lcd1:A", "vcc1:VCC", "red", [ "v57.6", "h28.7" ] ],
[ "ntc1:OUT", "esp:14", "green", [ "h28.8", "v38.3" ] ],
[ "ntc1:GND", "esp:GND.1", "black", [ "h19.2", "v76.8", "h28.65" ] ],
[ "ntc1:VCC", "esp:3V3", "red", [ "h28.8", "v-57.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Automatic light intensity control using LDR (ESP32) | const int ldrPin = 34; // GPIO pin for LDR
const int ledPin = 32; // GPIO pin for LED
void setup() {
Serial.begin(115200);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read the LDR value
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// Map the LDR value to a PWM value (0-255)
int ledBrightness = map(ldrValue, 0, 4095, 0, 255);
analogWrite(ledPin, ledBrightness); // Set the LED brightness
delay(100); // Delay for a short period
} | {
"version": 1,
"author": "Vigneshwaran",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -22.2, "left": 12.4, "attrs": {} },
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -19.2, "left": -110.36, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -3.6, "left": 42.2, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 72,
"left": 37.85,
"rotate": 90,
"attrs": { "value": "330" }
},
{ "type": "wokwi-photoresistor-sensor", "id": "ldr1", "top": 204.8, "left": 0.8, "attrs": {} }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "esp:4", "bb1:4b.i", "green", [ "h76.8" ] ],
[ "esp:GND.2", "bb1:3t.c", "red", [ "v0", "h19.2", "v38.4" ] ],
[ "esp:15", "bb1:bp.3", "green", [ "v0" ] ],
[ "bb1:bp.3", "ldr1:AO", "green", [ "v37.5", "h136", "v48" ] ],
[ "led1:A", "bb1:4t.b", "", [ "$bb" ] ],
[ "led1:C", "bb1:3t.b", "", [ "$bb" ] ],
[ "r1:1", "bb1:4t.c", "", [ "$bb" ] ],
[ "r1:2", "bb1:4b.g", "", [ "$bb" ] ],
[ "esp:GND.2", "ldr1:GND", "black", [ "v0", "h57.6", "v182.4", "h163.2", "v38.4" ] ],
[ "esp:5V", "ldr1:VCC", "red", [ "h-28.65", "v19.2", "h326.4", "v19.2" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for drawing game using a MAX7219 LED Dot Matrix | #include <MD_MAX72xx.h>
#define MAX_DEVICES 2
const int maxX = MAX_DEVICES * 8 - 1;
const int maxY = 7;
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 2
MD_MAX72XX mx = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, CS_PIN, MAX_DEVICES);
int x = 0;
int y = 0;
void setup() {
mx.begin();
mx.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY / 2);
mx.clear();
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
}
// the loop function runs over and over again forever
void loop() {
int horz = analogRead(HORZ_PIN);
int vert = analogRead(VERT_PIN);
if (vert < 300) {
y = min(y + 1, maxY);
}
if (vert > 700) {
y = max(y - 1, 0);
}
if (horz > 700) {
x = min(x + 1, maxX);
}
if (horz < 300) {
x = max(x - 1, 0);
}
if (digitalRead(SEL_PIN) == LOW) {
mx.clear();
}
mx.setPoint(y, x, true);
mx.update();
delay(100);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 120, "left": 20, "attrs": {} },
{
"type": "wokwi-analog-joystick",
"id": "joystick1",
"top": -9.72,
"left": 264.35,
"attrs": {}
},
{
"type": "wokwi-max7219-matrix",
"id": "matrix1",
"top": 9.03,
"left": 52.86,
"attrs": { "chain": "2" }
}
],
"connections": [
[ "joystick1:HORZ", "uno:A1", "green", [ "v0", "*", "v12" ] ],
[ "joystick1:VERT", "uno:A0", "purple", [ "v0", "*", "v16" ] ],
[ "joystick1:SEL", "uno:2", "blue", [ "v0", "*", "v-12" ] ],
[ "joystick1:GND", "uno:GND.3", "black", [ "v0", "*", "v20" ] ],
[ "joystick1:VCC", "uno:5V", "red", [ "v0", "*", "v24" ] ],
[ "matrix1:CS", "uno:10", "blue", [ "h8", "*", "v-24" ] ],
[ "matrix1:DIN", "uno:11", "green", [ "h12", "*", "v-20" ] ],
[ "matrix1:CLK", "uno:13", "orange", [ "h4", "*", "v-28" ] ],
[ "matrix1:GND", "uno:GND.1", "black", [ "h16", "*", "v-12" ] ],
[ "joystick1:VCC", "matrix1:V+", "red", [ "v7.04", "h-38.02", "v-84.67" ] ]
]
} |
Generate a diagram.json code for LED control using STM32 | #include "main.h"
ADC_HandleTypeDef hadc1; // ADC handle structure for ADC1
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
int main(void)
{
int adcResult; // Variable to store ADC conversion result
HAL_Init(); // Initialize the HAL library and reset peripherals
SystemClock_Config(); // Configure the system clock
MX_GPIO_Init(); // Initialize GPIO pins
MX_ADC1_Init(); // Initialize ADC1
while (1)
{
HAL_ADC_Start(&hadc1); // Start ADC conversion
HAL_ADC_PollForConversion(&hadc1, 100); // Wait for ADC conversion to complete with a timeout of 100ms
adcResult = HAL_ADC_GetValue(&hadc1); // Get the ADC conversion result
HAL_ADC_Stop(&hadc1); // Stop the ADC conversion
if (adcResult < 2048) // Check if the ADC result is below the threshold (mid-range for 12-bit ADC)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, RESET); // Turn off the LED connected to PA5
else
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, SET); // Turn on the LED connected to PA5
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0}; // Structure to configure the oscillator
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; // Structure to configure the clock tree
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1); // Set flash latency for 1 wait state
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; // Use HSI oscillator
RCC_OscInitStruct.HSIState = RCC_HSI_ON; // Enable HSI oscillator
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; // Set HSI division factor to 1
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; // Default HSI calibration
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler(); // Handle configuration error
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; // Enable clocks for HCLK, SYSCLK, and PCLK1
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; // Use HSI as the system clock source
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; // No division for SYSCLK
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; // No division for AHB
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; // No division for APB1
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler(); // Handle configuration error
}
}
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0}; // Structure to configure ADC channels
hadc1.Instance = ADC1; // Set the ADC instance to ADC1
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; // ADC clock prescaler
hadc1.Init.Resolution = ADC_RESOLUTION_12B; // 12-bit resolution
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; // Right-aligned data
hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED; // Fixed sequence scanning mode
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // End of conversion flag after each conversion
hadc1.Init.LowPowerAutoWait = DISABLE; // Disable low power auto wait
hadc1.Init.LowPowerAutoPowerOff = DISABLE; // Disable low power auto power-off
hadc1.Init.ContinuousConvMode = DISABLE; // Single conversion mode
hadc1.Init.NbrOfConversion = 1; // One conversion at a time
hadc1.Init.DiscontinuousConvMode = DISABLE; // Disable discontinuous mode
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Start conversion via software
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; // No external trigger edge
hadc1.Init.DMAContinuousRequests = DISABLE; // Disable continuous DMA requests
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; // Preserve data in case of overrun
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5; // Sampling time of 1.5 ADC clock cycles
hadc1.Init.OversamplingMode = DISABLE; // Disable oversampling
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH; // High trigger frequency mode
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler(); // Handle initialization error
}
sConfig.Channel = ADC_CHANNEL_0; // Select channel 0
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; // Rank the channel by number
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler(); // Handle configuration error
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0}; // Structure to configure GPIO pins
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable clock for GPIO port A
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Set PA5 to low (LED off)
GPIO_InitStruct.Pin = GPIO_PIN_5; // Configure PA5
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Set mode to push-pull output
GPIO_InitStruct.Pull = GPIO_NOPULL; // No internal pull-up or pull-down resistor
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // Set output speed to low
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // Initialize PA5 with the configuration
}
void Error_Handler(void)
{
__disable_irq(); // Disable interrupts
while (1) // Infinite loop to indicate error
{
}
} | {
"version": 1,
"author": "COD",
"editor": "wokwi",
"parts": [
{ "type": "board-st-nucleo-c031c6", "id": "nucleo", "top": 0.83, "left": -0.98, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": 102, "left": 349.4, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 167.15,
"left": 288,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-potentiometer", "id": "pot1", "top": 161.9, "left": -115.4, "attrs": {} }
],
"connections": [
[ "$serialMonitor:TX", "nucleo:PA3", "", [] ],
[ "$serialMonitor:RX", "nucleo:PA2", "", [] ],
[ "nucleo:3V3.2", "pot1:VCC", "green", [ "h-66.75", "v57.6", "h-37.6" ] ],
[ "pot1:GND", "nucleo:PA14", "black", [ "v19.2", "h-48", "v-96", "h96", "v9.6" ] ],
[ "nucleo:A0", "pot1:SIG", "gold", [ "h-47.55", "v9.6", "h-67.2" ] ],
[ "nucleo:PA5", "r1:1", "green", [ "h0" ] ],
[ "r1:2", "led1:A", "green", [ "v0", "h27.6" ] ],
[ "nucleo:GND.9", "led1:C", "black", [ "h0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Traffic light using Raspberry pi pico | #define RED 1
#define YELLOW 5
#define GREEN 9
void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
}
void loop() {
digitalWrite(GREEN, HIGH);
delay(3000);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
delay(500);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
delay(2000);
digitalWrite(YELLOW, HIGH);
delay(500);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 20,
"left": 60,
"attrs": { "env": "arduino-community" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": 3,
"left": 0,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 48,
"left": 0,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": 98,
"left": 0,
"attrs": { "color": "green" }
}
],
"connections": [
[ "pico:GND.1", "led1:C", "black", [] ],
[ "pico:GP1", "led1:A", "blue", [] ],
[ "pico:GND.2", "led2:C", "black", [] ],
[ "pico:GP5", "led2:A", "blue", [] ],
[ "pico:GND.3", "led3:C", "black", [] ],
[ "pico:GP9", "led3:A", "blue", [] ],
[ "$serialMonitor:RX", "pico:GP0", "", [] ]
]
} |
Generate a diagram.json code for LCD using Pi Pico | // LCD1602 and Pi Pico!
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
lcd.begin(16, 2);
lcd.print("Hello World!");
lcd.setCursor(2, 1);
lcd.print("> Pi Pico <");
}
void loop() {
delay(1); // Adding a delay() here speeds up the simulation
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 123.67,
"left": 135.97,
"rotate": 90,
"hide": false,
"attrs": { "env": "arduino-community" }
},
{
"type": "wokwi-lcd1602",
"id": "lcd",
"top": -17.85,
"left": 22.03,
"rotate": 0,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 114.8,
"left": 226.31,
"rotate": 0,
"hide": false,
"attrs": { "value": "220" }
}
],
"connections": [
[ "pico:GND.1", "lcd:VSS", "black", [ "v-51", "*", "h0", "v18" ] ],
[ "pico:GND.1", "lcd:K", "black", [ "v-51", "*", "h0", "v18" ] ],
[ "pico:GND.1", "lcd:RW", "black", [ "v-51", "*", "h0", "v18" ] ],
[ "pico:VSYS", "lcd:VDD", "red", [ "v16", "h-16" ] ],
[ "pico:VSYS", "r1:2", "red", [ "v16", "h0" ] ],
[ "r1:1", "lcd:A", "pink", [] ],
[ "pico:GP12", "lcd:RS", "blue", [ "v-16", "*", "h0", "v20" ] ],
[ "pico:GP11", "lcd:E", "purple", [ "v-20", "*", "h0", "v20" ] ],
[ "pico:GP10", "lcd:D4", "green", [ "v-24", "*", "h0", "v20" ] ],
[ "pico:GP9", "lcd:D5", "brown", [ "v-28", "*", "h0", "v20" ] ],
[ "pico:GP8", "lcd:D6", "gold", [ "v-32", "*", "h0", "v20" ] ],
[ "pico:GP7", "lcd:D7", "gray", [ "v-36", "*", "h0", "v20" ] ]
]
} |
Generate a diagram.json code for Buzzer alaram using Pi Pico | # Project objective: To test a passive buzzer to play an alarm sound at one second interval
# if passive buzzer is used, import the Speaker class from picozero
from picozero import Speaker
from time import sleep
# creating a Speaker object
speaker = Speaker(15)
# continuously beep at 1 sec interval while the board has power
# note: a passive buzzer can also be used to play different tones
while True:
speaker.on()
sleep(1)
speaker.off()
sleep(1) | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -3, "left": -112.4, "attrs": {} },
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 0.9,
"left": -38.65,
"rotate": 270,
"attrs": { "env": "micropython-20220618-v1.19.1" }
},
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": 31.2,
"left": 117,
"attrs": { "volume": "0.1" }
}
],
"connections": [
[ "bb1:3b.j", "bb1:bn.2", "black", [ "v0" ] ],
[ "bb1:25b.h", "bb1:bn.20", "black", [ "v0" ] ],
[ "bb1:26b.j", "bb1:20b.j", "green", [ "v0" ] ],
[ "pico:GP0", "bb1:1b.h", "", [ "$bb" ] ],
[ "pico:GP1", "bb1:2b.h", "", [ "$bb" ] ],
[ "pico:GND.1", "bb1:3b.h", "", [ "$bb" ] ],
[ "pico:GP2", "bb1:4b.h", "", [ "$bb" ] ],
[ "pico:GP3", "bb1:5b.h", "", [ "$bb" ] ],
[ "pico:GP4", "bb1:6b.h", "", [ "$bb" ] ],
[ "pico:GP5", "bb1:7b.h", "", [ "$bb" ] ],
[ "pico:GND.2", "bb1:8b.h", "", [ "$bb" ] ],
[ "pico:GP6", "bb1:9b.h", "", [ "$bb" ] ],
[ "pico:GP7", "bb1:10b.h", "", [ "$bb" ] ],
[ "pico:GP8", "bb1:11b.h", "", [ "$bb" ] ],
[ "pico:GP9", "bb1:12b.h", "", [ "$bb" ] ],
[ "pico:GND.3", "bb1:13b.h", "", [ "$bb" ] ],
[ "pico:GP10", "bb1:14b.h", "", [ "$bb" ] ],
[ "pico:GP11", "bb1:15b.h", "", [ "$bb" ] ],
[ "pico:GP12", "bb1:16b.h", "", [ "$bb" ] ],
[ "pico:GP13", "bb1:17b.h", "", [ "$bb" ] ],
[ "pico:GND.4", "bb1:18b.h", "", [ "$bb" ] ],
[ "pico:GP14", "bb1:19b.h", "", [ "$bb" ] ],
[ "pico:GP15", "bb1:20b.h", "", [ "$bb" ] ],
[ "pico:GP16", "bb1:20t.c", "", [ "$bb" ] ],
[ "pico:GP17", "bb1:19t.c", "", [ "$bb" ] ],
[ "pico:GND.5", "bb1:18t.c", "", [ "$bb" ] ],
[ "pico:GP18", "bb1:17t.c", "", [ "$bb" ] ],
[ "pico:GP19", "bb1:16t.c", "", [ "$bb" ] ],
[ "pico:GP20", "bb1:15t.c", "", [ "$bb" ] ],
[ "pico:GP21", "bb1:14t.c", "", [ "$bb" ] ],
[ "pico:GND.6", "bb1:13t.c", "", [ "$bb" ] ],
[ "pico:GP22", "bb1:12t.c", "", [ "$bb" ] ],
[ "pico:RUN", "bb1:11t.c", "", [ "$bb" ] ],
[ "pico:GP26", "bb1:10t.c", "", [ "$bb" ] ],
[ "pico:GP27", "bb1:9t.c", "", [ "$bb" ] ],
[ "pico:GND.7", "bb1:8t.c", "", [ "$bb" ] ],
[ "pico:GP28", "bb1:7t.c", "", [ "$bb" ] ],
[ "pico:ADC_VREF", "bb1:6t.c", "", [ "$bb" ] ],
[ "pico:3V3", "bb1:5t.c", "", [ "$bb" ] ],
[ "pico:3V3_EN", "bb1:4t.c", "", [ "$bb" ] ],
[ "pico:GND.8", "bb1:3t.c", "", [ "$bb" ] ],
[ "pico:VSYS", "bb1:2t.c", "", [ "$bb" ] ],
[ "pico:VBUS", "bb1:1t.c", "", [ "$bb" ] ],
[ "bz1:1", "bb1:25b.f", "", [ "$bb" ] ],
[ "bz1:2", "bb1:26b.f", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Stepper motor control using Raspberry Pi Pico | // Raspberry Pi Pico + Stepper Motor Example
#define DIR_PIN 2
#define STEP_PIN 3
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
}
void loop() {
// Move 200 steps (one rotation) CW over one second
digitalWrite(DIR_PIN, HIGH);
for (int i = 0; i < 200; i++) {
digitalWrite(STEP_PIN, HIGH);
digitalWrite(STEP_PIN, LOW);
delay(5); // 5 ms * 200 = 1 second
}
delay(500); // Wait half a second
// Move 200 steps (one rotation) CCW over 400 millis
digitalWrite(DIR_PIN, LOW);
for (int i = 0; i < 200; i++) {
digitalWrite(STEP_PIN, HIGH);
digitalWrite(STEP_PIN, LOW);
delay(2); // 2 ms * 200 = 0.4 seconds
}
delay(1000); // Wait another second
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-pi-pico", "id": "pico", "top": 63.95, "left": 86.4, "attrs": {} },
{ "type": "wokwi-a4988", "id": "drv1", "top": 91.4, "left": 226, "rotate": 270, "attrs": {} },
{
"type": "wokwi-stepper-motor",
"id": "stepper1",
"top": -179.4,
"left": 141.6,
"attrs": { "display": "angle" }
}
],
"connections": [
[ "drv1:1B", "stepper1:B-", "black", [ "v0" ] ],
[ "drv1:1A", "stepper1:B+", "green", [ "v0" ] ],
[ "stepper1:A+", "drv1:2A", "blue", [ "v0" ] ],
[ "stepper1:A-", "drv1:2B", "red", [ "v0" ] ],
[ "drv1:SLEEP", "drv1:RESET", "green", [ "v15.92", "h-9.9" ] ],
[ "drv1:STEP", "pico:GP3", "orange", [ "v130.8", "h-203.87", "v-163.2" ] ],
[ "drv1:DIR", "pico:GP2", "violet", [ "v140.4", "h-223.07", "v-182.4" ] ],
[ "drv1:GND.2", "pico:GND.8", "black", [ "v0" ] ],
[ "pico:VBUS", "drv1:VDD", "red", [ "h0" ] ]
]
} |
Generate a diagram.json code for Mini piano using raspberry pi pico | #include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
pitch = buttonTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
delay(10);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 183.54,
"left": 67.92,
"rotate": 90,
"attrs": { "env": "arduino-community" }
},
{
"type": "wokwi-buzzer",
"id": "buzzer",
"top": 208.42,
"left": 382.39,
"rotate": 90,
"attrs": { "volume": "0.2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 70,
"left": 0,
"rotate": 90,
"attrs": { "color": "red", "key": "1" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 70,
"left": 50,
"rotate": 90,
"attrs": { "color": "orange", "key": "2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": 70,
"left": 100,
"rotate": 90,
"attrs": { "color": "yellow", "key": "3" }
},
{
"type": "wokwi-pushbutton",
"id": "btn4",
"top": 70,
"left": 150,
"rotate": 90,
"attrs": { "color": "green", "key": "4" }
},
{
"type": "wokwi-pushbutton",
"id": "btn5",
"top": 70,
"left": 200,
"rotate": 90,
"attrs": { "color": "cyan", "key": "5" }
},
{
"type": "wokwi-pushbutton",
"id": "btn6",
"top": 70,
"left": 250,
"rotate": 90,
"attrs": { "color": "blue", "key": "6" }
},
{
"type": "wokwi-pushbutton",
"id": "btn7",
"top": 70,
"left": 300,
"rotate": 90,
"attrs": { "color": "purple", "key": "7" }
},
{
"type": "wokwi-pushbutton",
"id": "btn8",
"top": 70,
"left": 350,
"rotate": 90,
"attrs": { "color": "red", "key": "8" }
}
],
"connections": [
[ "pico:GND.1", "buzzer:1", "black", [ "v-12", "*", "h-15" ] ],
[ "pico:GP12", "btn1:1.r", "red", [ "v-8", "*", "v46" ] ],
[ "pico:GND.1", "btn1:2.r", "black", [ "v-12", "*", "h6" ] ],
[ "pico:GP11", "btn2:1.r", "orange", [ "v-4", "*", "v40" ] ],
[ "pico:GND.1", "btn2:2.r", "black", [ "v-12", "*", "h6" ] ],
[ "pico:GP10", "btn3:1.r", "yellow", [ "v0", "*", "v28" ] ],
[ "pico:GND.1", "btn3:2.r", "black", [ "v-12", "*", "h-6" ] ],
[ "pico:GP9", "btn4:1.r", "green", [ "v-36", "*", "v20" ] ],
[ "pico:GND.1", "btn4:2.r", "black", [ "v-12", "*", "h-6" ] ],
[ "pico:GP8", "buzzer:2", "gold", [ "v-32", "*", "h-10" ] ],
[ "pico:GP7", "btn5:1.r", "cyan", [ "v-24", "*", "h0" ] ],
[ "pico:GND.1", "btn5:2.r", "black", [ "v-12", "*", "h-15", "v4" ] ],
[ "pico:GP6", "btn6:1.r", "blue", [ "v-24", "*", "h0" ] ],
[ "pico:GND.1", "btn6:2.r", "black", [ "v-12", "*", "h-10", "v4" ] ],
[ "pico:GP5", "btn7:1.r", "purple", [ "v-20", "*", "h0" ] ],
[ "pico:GND.1", "btn7:2.r", "black", [ "v-12", "*", "h-8", "v4" ] ],
[ "pico:GP4", "btn8:1.r", "red", [ "v-16", "*", "h0" ] ],
[ "pico:GND.1", "btn8:2.r", "black", [ "v-12", "*", "h-8", "v4" ] ]
]
} |
Generate a diagram.json code for 4 digit timer using raspberry pi pico | #include "segment.pio.h"
uint8_t digits[] = {
0b11000000, // 0
0b11111001, // 1
0b10100100, // 2
0b10110000, // 3
0b10011001, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10011000, // 9
};
const uint8_t first_segment_pin = 2;
const uint8_t first_digit_pin = 10;
void setup() {
Serial1.begin(115200);
Serial1.println("Raspberry Pi Pico PIO 7-Segment Example");
// Load the PIO program and initialize the machine
auto offset = pio_add_program(pio0, &segment_program);
segment_program_init(pio0, 0, offset, first_segment_pin, first_digit_pin);
}
void displayNumber(uint value) {
pio_sm_put(pio0, 0,
digits[value / 1000 % 10] << 24 |
digits[value / 100 % 10] << 16 |
digits[value / 10 % 10] << 8 |
digits[value % 10]
);
}
int i = 0;
void loop() {
displayNumber(i++);
delay(200);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 65.16,
"left": -27.77,
"rotate": 90,
"hide": false,
"attrs": { "env": "arduino-community" }
},
{
"type": "wokwi-7segment",
"id": "sevseg1",
"top": -21.97,
"left": -85.69,
"rotate": 0,
"hide": false,
"attrs": { "digits": "4" }
}
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP1", "$serialMonitor:TX", "", [] ],
[ "sevseg1:A", "pico:GP2", "green", [ "v-21", "h145", "v115", "h-68" ] ],
[ "sevseg1:B", "pico:GP3", "green", [ "v-15", "h100", "v103", "h-65" ] ],
[ "sevseg1:C", "pico:GP4", "green", [ "v16", "h37" ] ],
[ "sevseg1:D", "pico:GP5", "green", [ "v22", "h53" ] ],
[ "sevseg1:E", "pico:GP6", "green", [ "v32", "h42" ] ],
[ "sevseg1:F", "pico:GP7", "green", [ "v-13", "h-96", "v116", "h104" ] ],
[ "sevseg1:G", "pico:GP8", "green", [ "v11", "h1" ] ],
[ "sevseg1:DP", "pico:GP9", "green", [ "v7", "h-3" ] ],
[ "sevseg1:DIG1", "pico:GP10", "blue", [ "v-19", "h-84", "v128", "h81" ] ],
[ "sevseg1:DIG2", "pico:GP11", "blue", [ "v-28", "h-121", "v144", "h80" ] ],
[ "sevseg1:DIG3", "pico:GP12", "blue", [ "v-39", "h-139", "v162", "h79" ] ],
[ "sevseg1:DIG4", "pico:GP13", "blue", [ "v66", "h-82" ] ]
]
} |
Generate a diagram.json code for Pico keyboard with LED | #include <Keypad.h>
const uint8_t LEDS = 12;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
// Pins connected to LED1, LED2, LED3, ...LED12
uint8_t ledPins[LEDS] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 28, 27 };
uint8_t rowPins[ROWS] = { 26, 22, 21, 20 }; // Pins connected to R1, R2, R3, R4
uint8_t colPins[COLS] = { 19, 18, 17, 16 }; // Pins connected to C1, C2, C3, C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
for (uint8_t l = 0; l < LEDS; l++) {
pinMode(ledPins[l], OUTPUT);
digitalWrite(ledPins[l], LOW);
}
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
switch (key) {
case '1': digitalWrite(ledPins[0], HIGH);
break;
case '2': digitalWrite(ledPins[1], HIGH);
break;
case '3': digitalWrite(ledPins[2], HIGH);
break;
case '4': digitalWrite(ledPins[3], HIGH);
break;
case '5': digitalWrite(ledPins[4], HIGH);
break;
case '6': digitalWrite(ledPins[5], HIGH);
break;
case '7': digitalWrite(ledPins[6], HIGH);
break;
case '8': digitalWrite(ledPins[7], HIGH);
break;
case '9':
for (uint8_t l = 0; l < 8; l++) {
digitalWrite(ledPins[l], HIGH);
}
break;
case '0':
for (uint8_t l = 0; l < 8; l++) {
digitalWrite(ledPins[l], LOW);
}
break;
case 'A': digitalWrite(ledPins[8], HIGH);
break;
case 'B': digitalWrite(ledPins[9], HIGH);
break;
case 'C': digitalWrite(ledPins[10], HIGH);
break;
case 'D': digitalWrite(ledPins[11], HIGH);
break;
case '*':
for (uint8_t l = 8; l < 12; l++) {
digitalWrite(ledPins[l], HIGH);
}
break;
case '#':
for (uint8_t l = 8; l < 12; l++) {
digitalWrite(ledPins[l], LOW);
}
break;
}
}
delay(10);
} | {
"version": 1,
"author": "Anderson Costa",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-pi-pico", "id": "pico", "top": 118, "left": 348, "rotate": 90, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "rp1",
"top": 349.55,
"left": 19.2,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "rp2",
"top": 368.75,
"left": 19.2,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "rp3",
"top": 426.35,
"left": 19.2,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "rp4",
"top": 397.55,
"left": 19.2,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-membrane-keypad", "id": "keypad1", "top": -30.8, "left": -13.6, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 20,
"left": 420,
"attrs": { "color": "blue", "label": "8" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 90,
"left": 415,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 20,
"left": 400,
"attrs": { "color": "blue", "label": "7" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 90,
"left": 395,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": 20,
"left": 380,
"attrs": { "color": "blue", "label": "6" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 90,
"left": 375,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": 20,
"left": 360,
"attrs": { "color": "blue", "label": "5" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 90,
"left": 355,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": 20,
"left": 340,
"attrs": { "color": "blue", "label": "4" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": 90,
"left": 335,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": 20,
"left": 320,
"attrs": { "color": "blue", "label": "3" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": 90,
"left": 315,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led7",
"top": 20,
"left": 300,
"attrs": { "color": "blue", "label": "2" }
},
{
"type": "wokwi-resistor",
"id": "r7",
"top": 90,
"left": 295,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led8",
"top": 20,
"left": 280,
"attrs": { "color": "blue", "label": "1" }
},
{
"type": "wokwi-resistor",
"id": "r8",
"top": 90,
"left": 275,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led9",
"top": 20,
"left": 440,
"attrs": { "color": "red", "label": "A" }
},
{
"type": "wokwi-resistor",
"id": "r9",
"top": 90,
"left": 435,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led10",
"top": 20,
"left": 460,
"attrs": { "color": "red", "label": "B" }
},
{
"type": "wokwi-resistor",
"id": "r10",
"top": 90,
"left": 455,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led11",
"top": 20,
"left": 480,
"attrs": { "color": "red", "label": "C" }
},
{
"type": "wokwi-resistor",
"id": "r11",
"top": 90,
"left": 475,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-led",
"id": "led12",
"top": 20,
"left": 500,
"attrs": { "color": "red", "label": "D" }
},
{
"type": "wokwi-resistor",
"id": "r12",
"top": 90,
"left": 495,
"rotate": 90,
"attrs": { "value": "220" }
}
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP1", "$serialMonitor:TX", "", [] ],
[ "keypad1:C4", "pico:GP16", "green", [ "v15", "h132" ] ],
[ "keypad1:C3", "pico:GP17", "green", [ "v25", "h182" ] ],
[ "keypad1:C2", "pico:GP18", "green", [ "v35", "h204" ] ],
[ "keypad1:C1", "pico:GP19", "green", [ "v45", "h222" ] ],
[ "keypad1:R4", "pico:GP20", "green", [ "v55", "h240" ] ],
[ "keypad1:R3", "pico:GP21", "green", [ "v65", "h260" ] ],
[ "keypad1:R2", "pico:GP22", "green", [ "v75", "h286" ] ],
[ "keypad1:R1", "pico:GP26", "green", [ "v85", "h299" ] ],
[ "r1:1", "led1:A", "green", [ "h0" ] ],
[ "r2:1", "led2:A", "green", [ "h0" ] ],
[ "r3:1", "led3:A", "green", [ "h0" ] ],
[ "r4:1", "led4:A", "green", [ "h0" ] ],
[ "r5:1", "led5:A", "green", [ "h0" ] ],
[ "r6:1", "led6:A", "green", [ "h0" ] ],
[ "r7:1", "led7:A", "green", [ "h0" ] ],
[ "r8:1", "led8:A", "green", [ "h0" ] ],
[ "pico:GND.4", "led8:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led7:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led6:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led5:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led4:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led3:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led2:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led1:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led9:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led10:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led11:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GND.4", "led12:C", "black", [ "v-25", "h-22", "v-77", "h6" ] ],
[ "pico:GP4", "r1:2", "green", [ "v-28", "h10", "v-20" ] ],
[ "pico:GP5", "r2:2", "green", [ "v-36", "h8" ] ],
[ "pico:GP6", "r3:2", "green", [ "v-26", "h7" ] ],
[ "pico:GP7", "r4:2", "green", [ "v-36", "h-3" ] ],
[ "pico:GP8", "r5:2", "green", [ "v-33", "h-13" ] ],
[ "pico:GP9", "r6:2", "green", [ "v-23", "h-14", "v-11", "h-10" ] ],
[ "pico:GP10", "r7:2", "green", [ "v-22", "h-16", "v-9" ] ],
[ "pico:GP11", "r8:2", "green", [ "v-12", "h-17", "v-24", "h-18" ] ],
[ "r9:1", "led9:A", "green", [ "h0" ] ],
[ "r10:1", "led10:A", "green", [ "h0" ] ],
[ "r11:1", "led11:A", "green", [ "h0" ] ],
[ "r12:1", "led12:A", "green", [ "h0" ] ],
[ "pico:GP3", "r9:2", "green", [ "v-20", "h10", "v-20", "h20", "v-13" ] ],
[ "pico:GP2", "r10:2", "green", [ "v-15", "h20", "v-15", "h20", "v-22" ] ],
[ "pico:GP27", "r12:2", "green", [ "v165", "h152" ] ],
[ "pico:GP28", "r11:2", "green", [ "v155", "h104" ] ],
[ "rp1:2", "keypad1:R1", "green", [ "h8.4", "v-48" ] ],
[ "rp2:2", "keypad1:R2", "green", [ "h18", "v-67.2" ] ],
[ "rp4:2", "keypad1:R3", "green", [ "h27.6", "v-96" ] ],
[ "rp3:2", "keypad1:R4", "green", [ "h37.2", "v-124.8" ] ],
[ "rp1:1", "rp2:1", "red", [ "v0" ] ],
[ "rp2:1", "rp4:1", "red", [ "v0" ] ],
[ "rp4:1", "rp3:1", "red", [ "v0" ] ],
[ "rp3:1", "pico:3V3", "red", [ "v19.2", "h417.15" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for LED blink using raspberry pi pico | from machine import Pin
from utime import sleep
print("Hello, Pi Pico!")
led = Pin(5, Pin.OUT)
while True:
led.toggle()
sleep(0.5) | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 0,
"left": 0,
"rotate": 0,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-led",
"id": "led1",
"top": -2.67,
"left": -65.33,
"rotate": 0,
"hide": false,
"attrs": { "color": "red" }
}
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP1", "$serialMonitor:TX", "", [] ],
[ "pico:GP5", "led1:A", "green", [ "h0" ] ],
[ "pico:GND.2", "led1:C", "black", [ "h0" ] ]
]
} |
Generate a diagram.json code for 7 segment diaplay using raspberry pi pico | #include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
/*
Our 7 Segment display has pins as follows:
--A--
F B
--G--
E C
--D--
By default we are allocating GPIO 2 to segment A, 3 to B etc.
So, connect GPIO 2 to pin A on the 7 segment LED display etc. Don't forget
the appropriate resistors, best to use one for each segment!
Connect button so that pressing the switch connects the GPIO 9 (default) to
ground (pull down)
*/
#define FIRST_GPIO 2
#define BUTTON_GPIO (FIRST_GPIO+7)
// This array converts a number 0-9 to a bit pattern to send to the GPIOs
int bits[10] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x67 // 9
};
/// \tag::hello_gpio[]
int main() {
stdio_init_all();
printf("Hello, 7segment - press button to count down!\n");
// We could use gpio_set_dir_out_masked() here
for (int gpio = FIRST_GPIO; gpio < FIRST_GPIO + 7; gpio++) {
gpio_init(gpio);
gpio_set_dir(gpio, GPIO_OUT);
// Our bitmap above has a bit set where we need an LED on, BUT, we are pulling low to light
// so invert our output
gpio_set_outover(gpio, GPIO_OVERRIDE_INVERT);
}
gpio_init(BUTTON_GPIO);
gpio_set_dir(BUTTON_GPIO, GPIO_IN);
// We are using the button to pull down to 0v when pressed, so ensure that when
// unpressed, it uses internal pull ups. Otherwise when unpressed, the input will
// be floating.
gpio_pull_up(BUTTON_GPIO);
int val = 0;
while (true) {
// Count upwards or downwards depending on button input
// We are pulling down on switch active, so invert the get to make
// a press count downwards
if (!gpio_get(BUTTON_GPIO)) {
if (val == 9) {
val = 0;
} else {
val++;
}
} else if (val == 0) {
val = 9;
} else {
val--;
}
// We are starting with GPIO 2, our bitmap starts at bit 0 so shift to start at 2.
int32_t mask = bits[val] << FIRST_GPIO;
// Set all our GPIOs in one go!
// If something else is using GPIO, we might want to use gpio_put_masked()
gpio_set_mask(mask);
sleep_ms(250);
gpio_clr_mask(mask);
}
}
/// \end::hello_gpio[] | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 67.9,
"left": 132.45,
"rotate": 90,
"attrs": { "builder": "pico-sdk" }
},
{
"type": "wokwi-7segment",
"id": "sevseg1",
"top": 14.58,
"left": 158.68,
"attrs": { "common": "anode" }
},
{ "type": "wokwi-slide-switch", "id": "sw1", "top": 62, "left": 99.1, "attrs": {} }
],
"connections": [
[ "pico:GP0", "$serialMonitor:RX", "", [] ],
[ "pico:GP2", "sevseg1:A", "green", [ "v-135", "h-40" ] ],
[ "pico:GP3", "sevseg1:B", "green", [ "v-128", "h-21" ] ],
[ "pico:GP4", "sevseg1:C", "green", [ "v-33", "h-21" ] ],
[ "pico:GP5", "sevseg1:D", "green", [ "v-28", "h-2" ] ],
[ "pico:GP6", "sevseg1:E", "green", [ "v-18", "h-20" ] ],
[ "pico:GP7", "sevseg1:F", "green", [ "v-14", "h-22", "v-108", "h21" ] ],
[ "pico:GP8", "sevseg1:G", "green", [ "v-10", "h-17", "v-123", "h14" ] ],
[ "pico:GP9", "sw1:3", "blue", [ "v-6", "h-17", "v-29" ] ],
[ "sw1:2", "pico:GND.4", "black", [ "v12", "h-18" ] ],
[ "pico:VSYS", "sevseg1:COM.2", "red", [ "v8", "h37", "v-242", "h-5" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for neopixel ring using Pi pico | import time
from neopixel import Neopixel
pixels = Neopixel(17, 0, 6, "GRB")
colors = [
(0xb6, 0xe4, 0x30),
(0x42, 0xd1, 0xe0),
]
pixel_index = 0
color_index = 0
while True:
pixels.set_pixel(pixel_index, colors[color_index])
pixels.show()
#print(pixel_index, colors[color_index])
pixel_index += 1
if pixel_index == 16:
pixel_index = 0
color_index = (color_index + 1) % 2
time.sleep(0.1) | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 80.37,
"left": 2.96,
"attrs": { "env": "micropython-20210902-v1.17" }
},
{
"type": "wokwi-led-ring",
"id": "ring1",
"top": 3.2,
"left": -177.15,
"attrs": { "pixels": "16" }
}
],
"connections": [
[ "pico:GP6", "ring1:DIN", "green", [ "h0" ] ],
[ "ring1:VCC", "pico:3V3", "red", [ "v128.24", "h238.19", "v-147.43" ] ],
[ "ring1:GND", "pico:GND.3", "black", [ "v0" ] ]
]
} |
Generate a diagram.json code for SSD1306 timer with micropython | from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf, sys
import utime
pix_res_x = 128
pix_res_y = 64
def init_i2c(scl_pin, sda_pin):
# Initialize I2C device
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
def display_logo(oled):
# Display the Raspberry Pi logo on the OLED
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB)
oled.fill(0)
oled.blit(fb, 96, 0)
oled.show()
def display_text(oled):
# Display text on the OLED
oled.text("Raspberry Pi", 5, 5)
oled.text("Pico", 5, 15)
oled.show()
def display_anima(oled):
# Display a simple timer animation on the OLED
start_time = utime.ticks_ms()
while True:
elapsed_time = (utime.ticks_diff(utime.ticks_ms(), start_time) // 1000) + 1
# Clear the specific line by drawing a filled black rectangle
oled.fill_rect(5, 40, oled.width - 5, 8, 0)
oled.text("Timer:", 5, 30)
oled.text(str(elapsed_time) + " sec", 5, 40)
oled.show()
utime.sleep_ms(1000)
def main():
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
display_logo(oled)
display_text(oled)
display_anima(oled)
if __name__ == '__main__':
main() | {
"version": 1,
"author": "Anderson Costa",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-mini", "id": "bb1", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 112,
"left": 50,
"rotate": 270,
"attrs": { "env": "micropython-20220618-v1.19.1" }
},
{ "type": "board-ssd1306", "id": "oled1", "top": 60, "left": 80, "attrs": {} }
],
"connections": [
[ "bb1:2b.f", "bb1:2t.e", "black", [ "v0" ] ],
[ "bb1:3b.f", "bb1:3t.e", "red", [ "v0" ] ],
[ "bb1:4b.f", "bb1:4t.e", "purple", [ "v0" ] ],
[ "bb1:5b.f", "bb1:5t.e", "orange", [ "v0" ] ],
[ "bb1:2t.d", "bb1:12t.d", "black", [ "v0" ] ],
[ "bb1:3t.c", "bb1:13t.c", "red", [ "v0" ] ],
[ "bb1:4t.b", "bb1:14t.b", "purple", [ "v0" ] ],
[ "bb1:5t.a", "bb1:15t.a", "orange", [ "v0" ] ],
[ "bb1:12t.e", "oled1:GND", "black", [ "v0" ] ],
[ "bb1:13t.e", "oled1:VCC", "red", [ "v0" ] ],
[ "bb1:14t.e", "oled1:SCL", "purple", [ "v0" ] ],
[ "bb1:15t.e", "oled1:SDA", "orange", [ "v0" ] ],
[ "pico:9", "bb1:4b.j", "purple", [ "v0" ] ],
[ "pico:8", "bb1:5b.j", "orange", [ "v0" ] ],
[ "bb1:2t.a", "bb1:17t.a", "black", [ "v-7.93", "h144" ] ],
[ "bb1:15t.d", "bb1:16t.d", "orange", [ "v0" ] ],
[ "pico:3V3_EN", "bb1:3b.j", "red", [ "v0" ] ],
[ "pico:GND.8", "bb1:2b.j", "black", [ "v0" ] ],
[ "pico:GP27", "bb1:4b.j", "purple", [ "v-15", "h-38.25" ] ],
[ "pico:GP26", "bb1:5b.j", "orange", [ "v-25", "h-36.92" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for servo control based on temperture using Pico | from machine import Pin, I2C, ADC, PWM
import time
from ssd1306 import SSD1306_I2C
import dht
# LED Configuration
LED_PIN = 25 # Built-in LED for Raspberry Pi Pico
led = Pin(LED_PIN, Pin.OUT)
# LDR Configuration
LDR_PIN = 26 # ADC pin for LDR
ldr = ADC(LDR_PIN)
LDR_THRESHOLD = 30 # Simulated light level threshold at 30
# DHT Sensor Configuration
DHT_PIN = 0 # GPIO pin connected to DHT22
dht_sensor = dht.DHT22(Pin(DHT_PIN))
# Shared variables for temperature and humidity
temperature = None
humidity = None
last_temperature = None # Track last temperature
servo_swept = False # Flag to indicate if the servo has swept
# OLED Display Configuration
WIDTH = 128 # Display width
HEIGHT = 64 # Display height
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000) # I2C configuration
display = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Initialize OLED display
# PIR Sensor Configuration
PIR_PIN = 2
pir = Pin(PIR_PIN, Pin.IN)
# Servo motor configuration
SERVO_PIN = 20 # GPIO pin connected to the servo's PWM signal
servo = PWM(Pin(SERVO_PIN))
servo.freq(50) # Set PWM frequency to 50Hz (standard for servos)
def set_servo_angle(angle):
"""Moves the servo to the specified angle."""
if 0 <= angle <= 360:
# Convert angle (0-360) to duty cycle (1000-9000 for 1ms to 2ms pulse)
duty = int(1000 + (angle / 360) * 8000)
servo.duty_u16(duty)
def read_ldr():
"""Reads the LDR value from the ADC pin."""
return ldr.read_u16()
def read_sensor_data():
"""Reads temperature and humidity from the DHT sensor."""
global temperature, humidity
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
except OSError as e:
print(f"Failed to read from DHT sensor: {e}")
temperature = None
humidity = None
def update_display(temp, hum, ldr_value, pir):
"""Updates the OLED display with temperature, Lux, and humidity data."""
display.fill(0) # Clear the display
display.text("Sensor Readings:", 0, 0)
# Display temperature and humidity
if temp is not None and hum is not None:
display.text(f"Temp: {temp:.2f}C", 0, 16)
display.text(f"Humidity: {hum:.2f}%", 0, 32)
else:
display.text("Sensor Error!", 0, 16)
# Display PIR status
if pir.value() == 0:
display.text("No intruders", 0, 48)
else:
display.text("Intruders detected", 0, 48)
# Display LDR value
display.text(f"LDR: {ldr_value}", 0, 56)
# Render the updated display
display.show()
while True:
read_sensor_data() # Read data from DHT22
ldr_value = read_ldr() # Read LDR value
update_display(temperature, humidity, ldr_value, pir) # Update the OLED display
time.sleep(2) # Wait before the next reading
# LDR Control
new = (381445 / ldr_value)
if new < LDR_THRESHOLD: # Low light condition
led.value(1) # Turn LED ON
else: # Bright light condition
led.value(0) # Turn LED OFF
# PIR Sensor (Motion)
if pir.value() == 0: # No motion detected
time.sleep(0.5)
else: # Motion detected
time.sleep(0.5)
# Servo Control based on temperature
if temperature != last_temperature: # Check if temperature has changed
last_temperature = temperature # Update last temperature
servo_swept = False # Reset the sweep flag
if not servo_swept: # Only move servo if it hasn't swept
if temperature >= 30:
for angle in range(0, 361, 10): # Sweep from 0° to 360°
set_servo_angle(angle)
time.sleep(0.1)
servo_swept = True # Set flag to indicate sweeping is done
else:
for angle in range(360, -1, -10): # Sweep from 360° to 0°
set_servo_angle(angle)
time.sleep(0.1)
servo_swept = True # Set flag to indicate sweeping is done | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 25.65,
"left": 3.6,
"attrs": { "env": "micropython-20231227-v1.22.0" }
},
{
"type": "wokwi-dht22",
"id": "dht1",
"top": -86.1,
"left": -91.8,
"attrs": { "temperature": "-40", "humidity": "80.5" }
},
{
"type": "board-ssd1306",
"id": "oled1",
"top": 118.34,
"left": 345.83,
"attrs": { "i2cAddress": "0x3c" }
},
{
"type": "wokwi-photoresistor-sensor",
"id": "ldr1",
"top": -131.2,
"left": -28,
"attrs": {}
},
{ "type": "wokwi-pir-motion-sensor", "id": "pir1", "top": -44, "left": -208.98, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -3.6,
"left": -274.6,
"attrs": { "color": "red" }
},
{ "type": "wokwi-servo", "id": "servo1", "top": -126.8, "left": 268.8, "attrs": {} }
],
"connections": [
[ "dht1:VCC", "pico:3V3", "red", [ "v0" ] ],
[ "dht1:SDA", "pico:GP0", "green", [ "v0" ] ],
[ "dht1:GND", "pico:GND.1", "black", [ "v0" ] ],
[ "oled1:VCC", "pico:3V3", "red", [ "v0" ] ],
[ "oled1:SCL", "pico:GP17", "magenta", [ "v-57.6", "h-210.9", "v144" ] ],
[ "oled1:SDA", "pico:GP16", "green", [ "v-28.8", "h-211.13", "v124.8" ] ],
[ "oled1:GND", "pico:GND.6", "black", [ "v-38.4", "h-201.6", "v67.2" ] ],
[ "ldr1:VCC", "pico:3V3", "red", [ "h28.8", "v163.2" ] ],
[ "pico:GP15", "ldr1:DO", "yellow", [ "v9.6", "h144", "v-325.8" ] ],
[ "pico:GP26", "ldr1:AO", "green", [ "h68.4", "v-210.5" ] ],
[ "ldr1:GND", "pico:GND.6", "black", [ "h19.2", "v249.2", "h-87.6" ] ],
[ "pir1:VCC", "pico:3V3", "red", [ "v0" ] ],
[ "pir1:GND", "pico:GND.1", "black", [ "v0" ] ],
[ "pir1:OUT", "pico:GP2", "green", [ "v0" ] ],
[ "led1:C", "pico:GND.1", "black", [ "v19.2", "h269.2" ] ],
[ "led1:A", "pico:GP4", "green", [ "v0" ] ],
[ "servo1:GND", "pico:GND.8", "black", [ "h-76.8", "v115.2" ] ],
[ "servo1:V+", "pico:3V3", "red", [ "h-67.2", "v124.9" ] ],
[ "servo1:PWM", "pico:GP20", "cyan", [ "h-57.6", "v211.4" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for keyboard input using pi pico | import rp2
from rp2 import PIO
from machine import Pin
from time import sleep
# ------ #
# sevseg #
# ------ #
@rp2.asm_pio(set_init=[PIO.IN_HIGH]*4)
def keypad():
wrap_target()
set(y, 0) # 0
label("1")
mov(isr, null) # 1
set(pindirs, 1) # 2
in_(pins, 4) # 3
set(pindirs, 2) # 4
in_(pins, 4) # 5
set(pindirs, 4) # 6
in_(pins, 4) # 7
set(pindirs, 8) # 8
in_(pins, 4) # 9
mov(x, isr) # 10
jmp(x_not_y, "13") # 11
jmp("1") # 12
label("13")
push(block) # 13
irq(0)
mov(y, x) # 14
jmp("1") # 15
wrap()
for i in range(10, 14):
Pin(i, Pin.IN, Pin.PULL_DOWN)
key_names = "*7410852#963DCBA"
def oninput(machine):
keys = machine.get()
while machine.rx_fifo():
keys = machine.get()
pressed = []
for i in range(len(key_names)):
if (keys & (1 << i)):
pressed.append(key_names[i])
print("Keys changed! pressed keys:", pressed)
sm = rp2.StateMachine(0, keypad, freq=2000, in_base=Pin(10, Pin.IN, Pin.PULL_DOWN), set_base=Pin(6))
sm.active(1)
sm.irq(oninput)
print("Please press any key on the keypad, or press Ctrl+C to enter REPL.")
while True:
sleep(0.1) | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 200.57,
"left": -42.52,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-membrane-keypad",
"id": "keypad1",
"top": -126.8,
"left": -157.6,
"attrs": {}
}
],
"connections": [
[ "pico:GP6", "keypad1:C4", "blue", [ "v-8", "h0" ] ],
[ "pico:GP7", "keypad1:C3", "blue", [ "v-12", "h0" ] ],
[ "pico:GP8", "keypad1:C2", "blue", [ "v-16", "h0" ] ],
[ "pico:GP9", "keypad1:C1", "blue", [ "v-20", "h0" ] ],
[ "pico:GP10", "keypad1:R4", "purple", [ "v-24", "h0" ] ],
[ "pico:GP11", "keypad1:R3", "purple", [ "v-28", "h0" ] ],
[ "pico:GP12", "keypad1:R2", "purple", [ "v-32", "h0" ] ],
[ "pico:GP13", "keypad1:R1", "purple", [ "v-36", "h0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for LCD diplay with IR remote | #include <IRremote.h>
#include <LiquidCrystal.h>
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup()
{
lcd.begin(16, 2);
lcd.print("<press a button>");
receiver.enableIRIn(); // Start the receiver
}
void loop()
{
// Checks received an IR signal
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
}
void lcdPrint(char* text)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("button pressed:");
lcd.setCursor(0, 1);
lcd.print(text);
lcd.print(" code: ");
lcd.print(receiver.decodedIRData.command);
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
lcdPrint("POWER");
break;
case 226:
lcdPrint("MENU");
break;
case 34:
lcdPrint("TEST");
break;
case 2:
lcdPrint("PLUS");
break;
case 194:
lcdPrint("BACK");
break;
case 224:
lcdPrint("PREV.");
break;
case 168:
lcdPrint("PLAY");
break;
case 144:
lcdPrint("NEXT");
break;
case 104:
lcdPrint("num: 0");
break;
case 152:
lcdPrint("MINUS");
break;
case 176:
lcdPrint("key: C");
break;
case 48:
lcdPrint("num: 1");
break;
case 24:
lcdPrint("num: 2");
break;
case 122:
lcdPrint("num: 3");
break;
case 16:
lcdPrint("num: 4");
break;
case 56:
lcdPrint("num: 5");
break;
case 90:
lcdPrint("num: 6");
break;
case 66:
lcdPrint("num: 7");
break;
case 74:
lcdPrint("num: 8");
break;
case 82:
lcdPrint("num: 9");
break;
default:
lcd.clear();
lcd.print(receiver.decodedIRData.command);
lcd.print(" other button");
}
} | {
"version": 1,
"author": "Anderson Costa",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-nano",
"id": "nano",
"top": 210,
"left": 245,
"rotate": 0,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-lcd1602",
"id": "lcd",
"top": 25,
"left": 235,
"rotate": 0,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 160,
"left": 450,
"rotate": 0,
"hide": false,
"attrs": { "value": "220" }
},
{
"type": "wokwi-ir-receiver",
"id": "ir",
"top": 185,
"left": 450,
"rotate": 0,
"hide": false,
"attrs": { "color": "green" }
},
{
"type": "wokwi-ir-remote",
"id": "remote1",
"top": 20,
"left": 70,
"rotate": 0,
"hide": false,
"attrs": {}
}
],
"connections": [
[ "nano:GND.2", "ds1307:GND", "black", [ "v20", "*", "v0" ] ],
[ "nano:5V", "ds1307:5V", "red", [ "v8", "*", "h-25", "v0" ] ],
[ "nano:GND.1", "lcd:VSS", "black", [ "v12", "*", "h-30", "v18" ] ],
[ "nano:GND.1", "lcd:K", "black", [ "v12", "*", "h-174", "v18" ] ],
[ "nano:GND.1", "lcd:RW", "black", [ "v12", "*", "h-68", "v18" ] ],
[ "nano:5V", "lcd:VDD", "red", [ "v8", "*", "h-35", "v22" ] ],
[ "nano:5V", "r1:2", "red", [ "v8", "h166", "v-112", "h50" ] ],
[ "nano:12", "lcd:RS", "orange", [ "v-32", "*", "h0", "v20" ] ],
[ "nano:11", "lcd:E", "purple", [ "v-28", "*", "h0", "v20" ] ],
[ "nano:10", "lcd:D4", "green", [ "v-24", "*", "h0", "v20" ] ],
[ "nano:9", "lcd:D5", "brown", [ "v-20", "*", "h0", "v20" ] ],
[ "nano:8", "lcd:D6", "gold", [ "v-16", "*", "h0", "v20" ] ],
[ "nano:7", "lcd:D7", "gray", [ "v-12", "*", "h0", "v20" ] ],
[ "nano:A4", "ds1307:SDA", "orange", [ "v16", "*", "h-20" ] ],
[ "nano:A5", "ds1307:SCL", "purple", [ "v20", "*", "h-15" ] ],
[ "r1:1", "lcd:A", "red", [] ],
[ "nano:GND.1", "ir:GND", "black", [ "v12", "h0" ] ],
[ "nano:5V", "ir:VCC", "red", [ "v0", "h0", "v8", "h0" ] ],
[ "nano:2", "ir:DAT", "green", [ "v-8", "h84", "v-26", "h72", "v92" ] ]
]
} |
Generate a diagram.json code for Motion detection using PIR sensor and arduino uno | /*
PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "uno",
"top": 98.67,
"left": -98.67,
"attrs": {}
},
{
"type": "wokwi-pir-motion-sensor",
"id": "pir1",
"top": -42,
"left": 25.81,
"attrs": {}
},
{
"type": "wokwi-led",
"id": "led1",
"top": -9,
"left": -32.67,
"attrs": { "color": "red" }
}
],
"connections": [
[ "pir1:VCC", "uno:5V", "red", [ "v19", "h-175", "v261", "h181" ] ],
[ "pir1:OUT", "uno:2", "green", [ "v37", "h68" ] ],
[ "uno:13", "led1:A", "green", [ "v-62", "h-47" ] ],
[ "uno:GND.1", "led1:C", "black", [ "v-54", "h-33" ] ],
[ "pir1:GND", "uno:GND.1", "black", [ "v31", "*", "v-28" ] ]
]
} |
Generate a diagram.json code for potentiometer value plotter | void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
}
void loop() {
int value = analogRead(A0);
Serial.println(value);
delay(100);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot1", "top": 200.3, "left": 201.4, "attrs": {} }
],
"connections": [
[ "pot1:GND", "uno:GND.2", "black", [ "v13", "h-40" ] ],
[ "pot1:SIG", "uno:A0", "green", [ "v14", "h49", "v-90", "h-66" ] ],
[ "uno:VIN", "pot1:VCC", "red", [ "v20", "h76", "v79", "h-33" ] ]
],
"serialMonitor": { "display": "plotter" },
"dependencies": {}
} |
Generate a diagram.json code for Light intensity measuring using LDR and arduino uno | #include <LiquidCrystal_I2C.h>
#define LDR_PIN 2
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(2, 0);
lcd.print("Room: ");
if (lux > 50) {
lcd.print("Light!");
} else {
lcd.print("Dark ");
}
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 20, "left": 20, "attrs": {} },
{ "type": "wokwi-lcd1602", "id": "lcd", "top": 242, "left": 50, "attrs": { "pins": "i2c" } },
{
"type": "wokwi-photoresistor-sensor",
"id": "ldr1",
"top": -62.39,
"left": 70.26,
"attrs": {}
}
],
"connections": [
[ "uno:GND.2", "lcd:GND", "black", [ "v14", "*", "h-20" ] ],
[ "uno:5V", "lcd:VCC", "red", [ "v18", "*", "h-16" ] ],
[ "uno:A4", "lcd:SDA", "green", [ "v22", "*", "h-12" ] ],
[ "uno:A5", "lcd:SCL", "blue", [ "v26", "*", "h-8" ] ],
[ "uno:GND.1", "ldr1:GND", "black", [ "v-18", "h138", "v-44" ] ],
[ "ldr1:VCC", "uno:5V", "red", [ "h92", "v276", "h-145" ] ],
[ "ldr1:AO", "uno:A0", "orange", [ "h69", "v241", "h-84" ] ]
]
} |
Generate a diagram.json code for arduino qr code generator | // Arduino QR Code Generator
// Author: Tomas Hubelbauer
#include <LedControl.h> // https://github.com/wayoda/LedControl
#include <qrcode.h> // https://github.com/ricmoo/QRCode
int size = 6;
int side = size * 8;
QRCode qrcode;
// DIN, CLK, CS
LedControl ledControls[] = {
LedControl(0, 1, 2, size),
LedControl(3, 4, 5, size),
LedControl(6, 7, 8, size),
LedControl(9, 10, 11, size),
LedControl(A0, A1, A2, size),
LedControl(A3, A4, A5, size),
};
int main() {
int version = 1;
uint8_t qrcodeData[qrcode_getBufferSize(version)];
qrcode_initText(&qrcode, qrcodeData, version, ECC_LOW, "HUBELBAUER.NET");
int scale = 2;
int shift = (side - (qrcode.size * scale)) / 2;
for (int y = shift; y < side; y++) {
for (int x = shift; x < side; x++) {
int index = y / 8;
int address = size - 1 - (x / 8);
int row = y % 8;
int column = 7 - (x % 8);
// Turn out of bounds dots off to make sure they don't stay on
bool state = false;
if (x >= shift && x < shift + (qrcode.size * scale) && y >= shift && y < shift + (qrcode.size * scale)) {
state = qrcode_getModule(&qrcode, (x - shift) / scale, (y - shift) / scale);
}
ledControls[index].setLed(address, row, column, state);
}
}
} | {
"version": 1,
"author": "Tomas Hubelbauer, Koepel",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "u",
"top": 33.94,
"left": 793.3,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-max7219-matrix",
"id": "1",
"top": 0,
"left": 260,
"attrs": { "chain": "6", "color": "white" }
},
{
"type": "wokwi-max7219-matrix",
"id": "2",
"top": 75,
"left": 260,
"attrs": { "chain": "6", "color": "white" }
},
{
"type": "wokwi-max7219-matrix",
"id": "3",
"top": 150,
"left": 260,
"attrs": { "chain": "6", "color": "white" }
},
{
"type": "wokwi-max7219-matrix",
"id": "4",
"top": 225,
"left": 260,
"attrs": { "chain": "6", "color": "white" }
},
{
"type": "wokwi-max7219-matrix",
"id": "5",
"top": 300,
"left": 260,
"attrs": { "chain": "6", "color": "white" }
},
{
"type": "wokwi-max7219-matrix",
"id": "6",
"top": 375,
"left": 260,
"attrs": { "chain": "6", "color": "white" }
}
],
"connections": [
[ "u:GND.2", "1:GND", "black", [ "*", "h6" ] ],
[ "u:5V", "1:V+", "red", [ "*", "h12" ] ],
[ "u:0", "1:DIN", "orange", [ "h20", "v25", "*", "h54" ] ],
[ "u:1", "1:CLK", "blue", [ "h23", "v38", "*", "h18" ] ],
[ "u:2", "1:CS", "green", [ "h26", "v51", "*", "h36" ] ],
[ "u:GND.2", "2:GND", "black", [ "*", "h6" ] ],
[ "u:5V", "2:V+", "red", [ "*", "h12" ] ],
[ "u:3", "2:DIN", "orange", [ "h29", "v64", "*", "h57" ] ],
[ "u:4", "2:CLK", "blue", [ "h32", "v77", "*", "h21" ] ],
[ "u:5", "2:CS", "green", [ "h35", "v90", "*", "h39" ] ],
[ "u:GND.2", "3:GND", "black", [ "*", "h6" ] ],
[ "u:5V", "3:V+", "red", [ "*", "h12" ] ],
[ "u:6", "3:DIN", "orange", [ "h38", "v103", "*", "h60" ] ],
[ "u:7", "3:CLK", "blue", [ "h41", "v116", "*", "h24" ] ],
[ "u:8", "3:CS", "green", [ "h44", "v136", "*", "h42" ] ],
[ "u:GND.2", "4:GND", "black", [ "*", "h6" ] ],
[ "u:5V", "4:V+", "red", [ "*", "h12" ] ],
[ "u:9", "4:DIN", "orange", [ "h47", "v149", "*", "h63" ] ],
[ "u:10", "4:CLK", "blue", [ "h50", "v162", "*", "h27" ] ],
[ "u:11", "4:CS", "green", [ "h53", "v175", "*", "h45" ] ],
[ "u:GND.2", "5:GND", "black", [ "*", "h6" ] ],
[ "u:5V", "5:V+", "red", [ "*", "h12" ] ],
[ "u:A0", "5:DIN", "orange", [ "*", "h66" ] ],
[ "u:A1", "5:CLK", "blue", [ "*", "h30" ] ],
[ "u:A2", "5:CS", "green", [ "*", "h48" ] ],
[ "u:GND.2", "6:GND", "black", [ "*", "h6" ] ],
[ "u:5V", "6:V+", "red", [ "*", "h12" ] ],
[ "u:A3", "6:DIN", "orange", [ "*", "h69" ] ],
[ "u:A4", "6:CLK", "blue", [ "*", "h33" ] ],
[ "u:A5", "6:CS", "green", [ "*", "h51" ] ]
],
"serialMonitor": { "display": "never" }
} |
Generate a diagram.json code for volume controller using arduino | #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/Picopixel.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
uint8_t volume = 50;
uint8_t bass = 25;
uint8_t treble = 66;
typedef enum {
SET_VOLUME,
SET_BASS,
SET_TREBLE,
} Mode;
Mode mode = SET_VOLUME;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void nextMode() {
switch (mode) {
case SET_VOLUME:
mode = SET_BASS;
break;
case SET_BASS:
mode = SET_TREBLE;
break;
case SET_TREBLE:
mode = SET_VOLUME;
break;
}
}
void updateValue(int delta) {
switch (mode) {
case SET_VOLUME:
volume = constrain(volume + delta, 0, 100);
break;
case SET_BASS:
bass = constrain(bass + delta, 0, 100);
break;
case SET_TREBLE:
treble = constrain(treble + delta, 0, 100);
break;
}
}
void updateDisplay() {
display.clearDisplay();
display.setFont();
display.setTextColor(1);
display.setCursor(42, 2);
display.print("Volume");
display.drawRoundRect(10, 12, 102, 9, 2, WHITE);
display.fillRect(11, 13, volume, 7, WHITE);
if (mode == SET_VOLUME) {
display.setCursor(32, 2);
display.print(">");
}
display.setCursor(48, 22);
display.print("Bass");
display.drawRoundRect(10, 32, 102, 9, 2, WHITE);
display.fillRect(11, 33, bass, 7, WHITE);
if (mode == SET_BASS) {
display.setCursor(38, 22);
display.print(">");
}
display.setCursor(42, 42);
display.print("Treble");
display.drawRoundRect(10, 52, 102, 9, 2, WHITE);
display.fillRect(11, 53, treble, 7, WHITE);
if (mode == SET_TREBLE) {
display.setCursor(32, 42);
display.print(">");
}
display.display();
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
updateDisplay();
}
long int modeLastChanged = 0;
int prevClk = HIGH;
void loop() {
if (digitalRead(ENCODER_SW) == LOW && millis() - modeLastChanged > 300) {
modeLastChanged = millis();
nextMode();
updateDisplay();
}
int clk = digitalRead(ENCODER_CLK);
if (clk != prevClk && clk == LOW) {
int dt = digitalRead(ENCODER_DT);
int delta = dt == HIGH ? 5 : -5;
updateValue(delta);
updateDisplay();
}
prevClk = clk;
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "uno", "top": -35.94, "left": 26.96, "attrs": {} },
{ "type": "wokwi-ky-040", "id": "encoder1", "top": -143.6, "left": 60.34, "attrs": {} },
{ "type": "wokwi-ssd1306", "id": "oled1", "top": 63.82, "left": 69.64, "attrs": {} }
],
"connections": [
[ "uno:GND.1", "oled1:GND", "black", [ "v19", "h5" ] ],
[ "uno:VIN", "oled1:VIN", "red", [ "v24", "h-15" ] ],
[ "oled1:DATA", "uno:A4", "orange", [ "v-34", "h10" ] ],
[ "oled1:CLK", "uno:A5", "blue", [ "v-30", "h5" ] ],
[ "encoder1:CLK", "uno:2", "purple", [ "h42", "v85", "h-76" ] ],
[ "encoder1:DT", "uno:3", "green", [ "h33", "v71", "h-73" ] ],
[ "encoder1:SW", "uno:4", "gray", [ "h26", "v57", "h-75" ] ],
[ "encoder1:VCC", "uno:VIN", "red", [ "h55", "v149", "h-28" ] ],
[ "encoder1:GND", "uno:GND.2", "black", [ "h18", "v32", "h-43" ] ]
]
} |
Generate a diagram.json code for Date and time reader using ds1307 and arduino uno | #include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
void loop () {
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"id": "uno",
"type": "wokwi-arduino-uno",
"top": 0,
"left": 20
},
{
"id": "ds1307",
"type": "wokwi-ds1307",
"top": 250,
"left": 220
}
],
"connections": [
["uno:GND.2", "ds1307:GND", "black", ["v20", "*", "v0"]],
["uno:5V", "ds1307:5V", "red", ["v0"]],
["uno:A4", "ds1307:SDA", "green", ["v16", "h0", "*", "h-34"]],
["uno:A5", "ds1307:SCL", "orange", ["v20", "*", "h-30"]]
]
} |
Generate a diagram.json code for Parking sensor using hc-sr04 and arduino uno | // I simulate lighting efects on parking sensor lamp
// It should be a led ring lamp in a garage changing
// color and flashing by distance.
//Project link in the description :)
// Direction Color State
// power-up white steady
// >400 green rotating slowly
// 200-400 green rotating faster
// 100-200 yellow --||--
// 50-100 red fast rotation
// 50-20 red flashing
// <20 red/blue flashing
// 3rd approach: rotating and changing palette
// it is too demanding on calculation resources
// When I saw it finished, I realised it would be easier with FadeToBlackBy()
// but it is more flexible.
#include <FastLED.h>
#define TRIG_PIN A3
#define ECHO_PIN A2
#define LED_PIN 8
#define NUM_LEDS 16
#define BRIGHTNESS 255
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_FREQ 100 // per second
#define MEASURE_FREQ 2 // per second
#define DISTANCE_MAX 400 // cm
#define DISTANCE_MIN 0 // cm
CRGBPalette16 currentPalette;
TBlendType currentBlending;
CRGB rgb;
extern const TProgmemPalette16 mySemaphorePalette_p PROGMEM;
/***
*
***
*
***/
void setup() {
delay(2000);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Serial.begin(9600);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); // .setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentPalette = mySemaphorePalette_p;
currentBlending = LINEARBLEND;
// blank
FillColorDelay( CRGB::Yellow, 1000);
// Serial.println(" [BEGIN] ");
// blank
FillColorDelay( CRGB::Black, 1000);
}
/*
*
*
*
****/
void loop() {
static unsigned int distance = 10;
static uint8_t colorIndex;
static byte offset = 0;
static unsigned int offset_delay = 100; // rotation speed
static unsigned long lastOffsetTime = 0; // last rotation change
static unsigned long lastMeasureTime = 0; // last use of HC-SR04 sensor
static unsigned long lastPrintTime = 0; // last Serial.print time
// distance is 200-1500
// ROTATION SPEED should change 20-1 rotation per second
offset_delay = map( distance, 20, 400, 20, 100);
if ( millis() - lastOffsetTime >= offset_delay ) {
offset = (offset < NUM_LEDS - 1 ? offset + 1 : 0); // increment position
lastOffsetTime = millis();
}
// Measure two times per second
if (millis() - lastMeasureTime >= 1000 / MEASURE_FREQ) {
distance = get_distance();
colorIndex = map(distance, DISTANCE_MIN, DISTANCE_MAX, 0, 240);
// update palette
newGradientPaletteByDistance(distance);
lastMeasureTime = millis();
}
if ( distance > 30 ) {
FillLEDsFromPalette( offset );
} else if ( offset % 2 == 0 ) {
FillColorDelay(CRGB::Blue,200);
// fill_solid( leds, NUM_LEDS, CRGB::Blue);
} else {
FillColorDelay(CRGB::Red,200);
// fill_solid( leds, NUM_LEDS, CRGB::Red);
}
// Print only for debugging
// if (millis() - lastPrintTime >= 2000) {
// Serial.print(" [ distance: "); Serial.print(distance, DEC);
// Serial.print("mm o_delay: "); Serial.print(offset_delay, DEC);
// Serial.print(" index: "); Serial.print(colorIndex, DEC);
// Serial.println(" ] ");
// lastPrintTime = millis();
// }
FastLED.show();
FastLED.delay(1000 / UPDATES_FREQ);
}
// Get color mapped by distance and create gradient palette based by this color
void newGradientPaletteByDistance(int distance) {
uint8_t xyz[12]; // Needs to be 4 times however many colors are being used.
// 3 colors = 12, 4 colors = 16, etc.
CRGB rgb;
rgb = ColorFromPalette( mySemaphorePalette_p, map(distance, DISTANCE_MIN, DISTANCE_MAX, 0, 240), BRIGHTNESS, currentBlending);
// index
xyz[0] = 0; xyz[1] = rgb.r; xyz[2] = rgb.g; xyz[3] = rgb.b; // anchor of first color - must be zero
xyz[4] = 40; xyz[5] = rgb.r; xyz[6] = rgb.g; xyz[7] = rgb.b;
xyz[8] = 255; xyz[9] = 0; xyz[10] = 0; xyz[11] = 0; // anchor of last color - must be 255
currentPalette.loadDynamicGradientPalette(xyz);
}
// fill FastLED leds[] strip/ring with colors
void FillLEDsFromPalette( byte offset) {
//fill_solid( leds, NUM_LEDS, 0);
// One gradient
for ( int i = 0; i < NUM_LEDS; i++) {
leds[ i ] = ColorFromPalette( currentPalette, (i+offset) * 255 / (NUM_LEDS - 1), BRIGHTNESS, currentBlending);
}
// Two gradients
// for ( int i = 0; i < NUM_LEDS/2; i++) {
// leds[ i ] = ColorFromPalette( currentPalette, (i+offset) * 255 / (NUM_LEDS/2 - 1), BRIGHTNESS, currentBlending);
// leds[ i+(NUM_LEDS/2) ] = leds[i];
// }
}
// Fill all leds with one color and wait duration ms
void FillColorDelay ( CRGB color, unsigned long duration ) {
fill_solid( leds, NUM_LEDS, color);
FastLED.show();
FastLED.delay(duration);
}
// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM. A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 mySemaphorePalette_p PROGMEM = {
CRGB::Magenta, CRGB::Red, CRGB::Red, CRGB::Red,
CRGB::Orange, CRGB::Orange, CRGB::Orange, CRGB::Orange,
CRGB::Yellow, CRGB::Yellow, CRGB::Yellow, CRGB::Green,
CRGB::Green, CRGB::Green, CRGB::Green, CRGB::White
};
// get distance number 0-400 cm
// use TRIG_PIN and ECHO_PIN of HC-SR04 sensor
int get_distance() {
static int distance;
uint16_t duration = 0;
uint32_t interval = 0;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read time of the trig and echo pins
duration = pulseIn(ECHO_PIN, HIGH);
// Calculates the distance
distance = (duration / 2) / 29;
return distance; // centimeters
} | {
"version": 1,
"author": "stevesigma",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-nano",
"id": "nano",
"top": 187.29,
"left": 177.88,
"rotate": 180,
"hide": false,
"attrs": {}
},
{
"type": "wokwi-hc-sr04",
"id": "sonar",
"top": 61.25,
"left": 190.94,
"rotate": 0,
"hide": false,
"attrs": { "distance": "400" }
},
{
"type": "wokwi-led-ring",
"id": "ring",
"top": 57.34,
"left": 32.62,
"rotate": 0,
"hide": false,
"attrs": { "pixels": "16", "background": "black" }
}
],
"connections": [
[ "nano:5V", "sonar:VCC", "red", [ "*", "v20" ] ],
[ "nano:GND.2", "sonar:GND", "black", [ "v20", "h130", "*", "v5" ] ],
[ "nano:A2", "sonar:ECHO", "orange", [ "*" ] ],
[ "nano:A3", "sonar:TRIG", "white", [ "*" ] ],
[ "nano:GND.2", "ring:GND", "black", [ "v20", "h-133", "*", "*" ] ],
[ "nano:8", "ring:DIN", "green", [ "v10", "h-180", "*" ] ],
[ "nano:5V", "ring:VCC", "red", [ "v-20", "h-50", "v70", "h-70", "*" ] ]
]
} |
Generate a diagram.json code for digital weight scale using hx711 load cell and arduino uno | #include "HX711_ADC.h"
#include <LiquidCrystal_I2C.h>
HX711_ADC LoadCell(9, 8);
LiquidCrystal_I2C lcd(0x27,16,2);
const int buzzer = 11;
int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
int taree = 6;
int num = 1;
int buttonState = 0;
int buttonState1 = 0;
float alarm1 = 0;
float alarm2 = 0;
float z1 = 28.3495;
unsigned long prevTime1 = millis();
unsigned long prevTime2 = millis();
long Time1 = 3000;
long Time2 = 1000;
void setup()
{
pinMode (button1, INPUT_PULLUP);
pinMode (button2, INPUT_PULLUP);
pinMode (button3, INPUT_PULLUP);
pinMode (button4, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode (taree, INPUT_PULLUP);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Weighing Scale");
lcd.setCursor(3, 1);
lcd.print("With Alarm");
delay(2500);
lcd.clear();
delay(150);
lcd.setCursor(3, 0);
lcd.print("Taring");
delay(200);
lcd.setCursor(9, 0);
lcd.print(".");
delay(200);
lcd.setCursor(10, 0);
lcd.print(".");
delay(200);
lcd.setCursor(11, 0);
lcd.print(".");
delay(200);
lcd.setCursor(12, 0);
lcd.print(".");
delay(200);
delay(1000);
lcd.clear();
LoadCell.begin();
LoadCell.start(1000);
LoadCell.setCalFactor(0.42);
}
void loop() {
unsigned long currentTime = millis();
static unsigned long currentTime1 = millis();
LoadCell.update();
float i = LoadCell.getData();
float z = i/28.3495;
if(digitalRead(button3) == LOW){
alarm1=0;
alarm2=0;
lcd.clear();}
buttonState = digitalRead(button2);
buttonState1 = digitalRead(button1);
if(buttonState == 0)
currentTime1 = millis();
if(buttonState1 == 0)
currentTime1 = millis();
switch(num){
case 1:
{
if(millis() - currentTime1 >= 4000) // else
{ num = 1;
}
else
num = num;
break;
}
case 2:
{
if(digitalRead(button1) == LOW){alarm1 = alarm1+1;
delay(75);}
else if(digitalRead(button2) == LOW){alarm1 = alarm1-1;
delay(75);}
if(digitalRead(button3) == LOW)
alarm1=0;
lcd.setCursor(1, 0);
lcd.print("Set for Gram");
lcd.setCursor(0, 1); // set cursor to secon row
lcd.print(alarm1, 0); // print out the retrieved value to the second row
lcd.print("g ");
alarm2 = alarm1/z1;
lcd.setCursor(9, 1);
lcd.print(alarm2, 1);
lcd.print("oz ");
if(millis() - currentTime1 >= 8000)
{ num = 1;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Weighing Scale");
delay(500);
lcd.clear();}
else
num = num;
break;}
case 3:
{
if(digitalRead(button1) == LOW){alarm2 = alarm2+1;
delay(75);}
else if(digitalRead(button2) == LOW){alarm2 = alarm2-1;
delay(75);}
lcd.setCursor(1, 0);
lcd.print("Set for Ounce");
lcd.setCursor(0, 1); // set cursor to secon row
lcd.print(alarm1, 0); // print out the retrieved value to the second row
lcd.print("g ");
alarm1 = alarm2 * z1;
lcd.setCursor(9, 1);
lcd.print(alarm2, 1);
lcd.print("oz ");
if(digitalRead(button3) == LOW)
alarm2=0;
if(millis() - currentTime1 >= 4000) // else
{ num = 1;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Weighing Scale");
delay(500);
lcd.clear();}
else
num = num;
break;}
}
if (digitalRead (taree) == LOW)
{
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Taring");
delay(200);
lcd.setCursor(9, 0);
lcd.print(".");
delay(200);
lcd.setCursor(10, 0);
lcd.print(".");
delay(200);
lcd.setCursor(11, 0);
lcd.print(".");
delay(200);
lcd.setCursor(12, 0);
lcd.print(".");
delay(200);
delay(500);
LoadCell.start(1000);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.clear();
}
if (digitalRead (button4) == LOW){{if(num>2) num = 1;
else num++;
currentTime1 = millis();
delay (300);
lcd.clear();
delay (300);
}
if (num==3)
{ lcd.setCursor(1, 0);
lcd.print("Set for Ounce");
delay(500);
lcd.clear();}
else if (num==2)
{lcd.setCursor(1, 0);
lcd.print("Set for Gram");
delay(500);
lcd.clear();}
else if (num==1)
{
lcd.setCursor(1, 0);
lcd.print("Weighing Scale");
delay(500);
lcd.clear();}
}
if(currentTime - prevTime1 > Time1){
if(i>alarm1 && alarm1>0 )
{
noTone(buzzer);
delay(300);
tone(buzzer,250);
delay(300);
noTone(buzzer);
delay(300);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(300);}
prevTime1 = currentTime;
}
if(i<-0.9){i==i;}
else if(-0.9<i && i<0.1){i=0;}
if(currentTime - prevTime2 > Time2){
if (i>=3000 && num<4)
{
lcd.clear();
delay(500);
num=4;
}else if(num==4 && i<3000){
num=1;
lcd.clear();
delay(1000);
}
if(num==1){
lcd.setCursor(1, 0);
lcd.print("Weighing Scale");
lcd.setCursor(0,1);
lcd.print(i, 0);
lcd.print("g ");
lcd.setCursor(9,1);
lcd.print(z, 1);
lcd.print("oz ");}
prevTime2 = currentTime;
if(num==4){
lcd.setCursor(0, 0);
lcd.print(" Over Loaded ");
delay(500);
lcd.clear();
delay(500);}
}
Serial.print(currentTime);
Serial.print(" ");
Serial.print(millis());
Serial.print(" ");
Serial.print(prevTime2);
Serial.println(" ");
} | {
"version": 1,
"author": "Justine matic013",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -182.77,
"left": 85.42,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-hx711",
"id": "hx7",
"top": 124.2,
"left": 310.51,
"attrs": { "type": "3kg" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -124.43,
"left": 235.69,
"attrs": { "color": "blue", "label": "Up", "key": "1" }
},
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": -327.87,
"left": 97.48,
"attrs": { "pins": "i2c" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": -120.37,
"left": 322.72,
"attrs": { "color": "blue", "label": "Down", "key": "2" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": -118.96,
"left": 410.54,
"attrs": { "color": "green", "label": "Reset/Stop", "key": "3" }
},
{
"type": "wokwi-pushbutton",
"id": "btn4",
"top": -116.38,
"left": 503.88,
"attrs": { "color": "red", "label": "Mode", "key": "4" }
},
{
"type": "wokwi-pushbutton",
"id": "btn5",
"top": -117.37,
"left": 592.34,
"attrs": { "color": "grey", "label": "Tare", "key": "5" }
}
],
"connections": [
[ "bz1:2", "uno:11", "red", [ "v36.27", "h27.43" ] ],
[ "uno:GND.2", "lcd1:GND", "black", [ "v41.96", "h-177.57", "v-195.25" ] ],
[ "uno:A4", "lcd1:SDA", "green", [ "v69.2", "h-243.29", "v-195.81" ] ],
[ "uno:A5", "lcd1:SCL", "blue", [ "v85.1", "h-246.54", "v-22.7" ] ],
[ "btn2:1.r", "uno:3", "orange", [ "v69.56", "h-5.39" ] ],
[ "btn1:2.l", "btn2:2.l", "black", [ "h3.66", "v36.06", "h88.44" ] ],
[ "btn2:2.l", "btn3:2.l", "black", [ "h0.56", "v37.88", "h106.82" ] ],
[ "btn3:1.r", "uno:4", "gold", [ "v78.15", "h-273.08" ] ],
[ "lcd1:VCC", "uno:5V", "red", [ "h-104.11", "v536.86", "h16.59" ] ],
[ "btn3:2.l", "btn4:2.l", "black", [ "h-0.06", "v35.76", "h104.2" ] ],
[ "btn4:1.r", "uno:5", "blue", [ "v88.49", "h-361.09" ] ],
[ "uno:GND.1", "bz1:1", "black", [ "v0" ] ],
[ "btn1:1.l", "uno:2", "green", [ "h0" ] ],
[ "uno:GND.1", "btn1:2.l", "black", [ "v0" ] ],
[ "hx7:GND", "uno:GND.2", "black", [ "h0" ] ],
[ "hx7:VCC", "uno:5V", "red", [ "h0" ] ],
[ "btn4:2.l", "btn5:2.l", "black", [ "h-0.19", "v32.47", "h88.22" ] ],
[ "hx7:DT", "uno:9", "green", [ "h0" ] ],
[ "uno:8", "hx7:SCK", "green", [ "v0" ] ],
[ "uno:6", "btn5:1.r", "green", [ "v-36.51", "h460.24", "v-82.43" ] ]
]
} |
Generate a diagram.json code for LED bar with potentiometer and arduino uno | const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "uno",
"top": 209.75,
"left": -189.97,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-led-bar-graph",
"id": "bargraph1",
"top": 313.04,
"left": 127.68,
"attrs": { "color": "lime" }
},
{ "type": "wokwi-resistor", "id": "r1", "top": 309, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r2", "top": 319, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r3", "top": 328, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r4", "top": 337, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r5", "top": 347, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r6", "top": 357, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r7", "top": 366, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r8", "top": 376, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r9", "top": 385, "left": 190, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r10", "top": 395, "left": 190, "attrs": {} },
{
"type": "wokwi-slide-potentiometer",
"id": "pot1",
"top": 266.78,
"left": -337.42,
"rotate": 270,
"attrs": { "travelLength": "30" }
}
],
"connections": [
[ "bargraph1:A10", "uno:2", "green", [ "h-28", "v31" ] ],
[ "bargraph1:A9", "uno:3", "green", [ "h-38", "v31" ] ],
[ "bargraph1:A8", "uno:4", "green", [ "h-47", "v31" ] ],
[ "bargraph1:A7", "uno:5", "green", [ "h-57", "v31" ] ],
[ "bargraph1:A6", "uno:6", "green", [ "h-67", "v31" ] ],
[ "bargraph1:A5", "uno:7", "green", [ "h-76", "v31" ] ],
[ "bargraph1:A4", "uno:8", "green", [ "h0" ] ],
[ "bargraph1:A3", "uno:9", "green", [ "h0" ] ],
[ "bargraph1:A2", "uno:10", "green", [ "h0" ] ],
[ "bargraph1:A1", "uno:11", "green", [ "h0" ] ],
[ "r1:1", "bargraph1:C1", "green", [ "v0" ] ],
[ "r2:1", "bargraph1:C2", "green", [ "v0" ] ],
[ "r3:1", "bargraph1:C3", "green", [ "v0" ] ],
[ "r4:1", "bargraph1:C4", "green", [ "v0" ] ],
[ "r5:1", "bargraph1:C5", "green", [ "v0" ] ],
[ "r6:1", "bargraph1:C6", "green", [ "v0" ] ],
[ "r7:1", "bargraph1:C7", "green", [ "v0" ] ],
[ "r8:1", "bargraph1:C8", "green", [ "v0" ] ],
[ "r9:1", "bargraph1:C9", "green", [ "v0" ] ],
[ "r10:1", "bargraph1:C10", "green", [ "v0" ] ],
[ "r10:2", "r9:2", "black", [ "v0" ] ],
[ "r8:2", "r9:2", "black", [ "v0" ] ],
[ "r8:2", "r7:2", "black", [ "v0" ] ],
[ "r7:2", "r6:2", "black", [ "v0" ] ],
[ "r6:2", "r5:2", "black", [ "v0" ] ],
[ "r5:2", "r4:2", "black", [ "v0" ] ],
[ "r4:2", "r3:2", "black", [ "v0" ] ],
[ "r3:2", "r2:2", "black", [ "v0" ] ],
[ "r2:2", "r1:2", "black", [ "v0" ] ],
[ "uno:GND.1", "r1:2", "black", [ "h0" ] ],
[ "pot1:GND", "uno:GND.2", "black", [ "v-14", "h72", "v139" ] ],
[ "pot1:SIG", "uno:A0", "green", [ "v14", "h51", "v-59" ] ],
[ "uno:VIN", "pot1:VCC", "red", [ "h-48", "v72", "h-61" ] ]
]
} |
Generate a diagram.json code for MPU6050 with arduino uno for 6 axis accel and gyro sensor | // originally by Andy Sloane for the Arduboy
// https://www.a1k0n.net/2017/02/01/arduboy-teapot.html
#include <Wire.h>
#include "Tiny4kOLED.h"
#include <avr/pgmspace.h>
#include "draw.h"
#include "sincos.h"
#include "vec.h"
#include "teapot.h"
const int MPU_addr = 0x68; // I2C address of the MPU-6050
uint8_t screen_[1024];
int32_t angle_A_ = 0, angle_B_ = 0, angle_C_ = -16384;
int16_t scale_ = 1024 + 500;
uint16_t frame_ = 0;
static Vec216 verts[mesh_NVERTS]; // rotated, projected screen space vertices
void setup(void) {
Wire.begin();
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
// Initialize the MPU-6050 and test if it is connected.
Wire.beginTransmission( MPU_addr);
Wire.write( 0x6B); // PWR_MGMT_1 register
Wire.write( 0); // set to zero (wakes up the MPU-6050)
auto error = Wire.endTransmission();
}
void ReadInput() {
static bool manual_control = false;
Wire.beginTransmission( MPU_addr);
Wire.write( 0x43); // starting with register 0x43 (GYRO_XOUT_H)
Wire.endTransmission( false);
Wire.requestFrom( MPU_addr, 6); // request a total of 6 bytes
int16_t gx = Wire.read()<<8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
int16_t gy = Wire.read()<<8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
int16_t gz = Wire.read()<<8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
if (gx || gy || gz) {
manual_control = true;
angle_A_ += gx / 16;
angle_B_ += gy / 16;
angle_C_ += gz / 16;
}
if (!manual_control) {
angle_A_ += 499;
angle_B_ += 331;
angle_C_ += 229;
}
}
// down-convert signed 1.10 precision to signed 0.7 precision
// (scaling down from -1024..1024 to -127..127)
static int8_t RescaleR(int16_t x) {
return ((uint32_t) x*127 + 512) >> 10;
}
void DrawObject() {
// construct rotation matrix
int16_t cA, sA, cB, sB, cC, sC;
GetSinCos((angle_A_ >> 6) & 1023, &sA, &cA);
GetSinCos((angle_B_ >> 6) & 1023, &sB, &cB);
GetSinCos((angle_C_ >> 6) & 1023, &sC, &cC);
// rotate about X axis by C, then Y axis by B, then Z axis by A
// [ cA*cB, cB*sA, sB]
// R = [-cA*sB*sC - cC*sA, cA*cC - sA*sB*sC, cB*sC]
// [-cA*cC*sB + sA*sC, -cA*sC - cC*sA*sB, cB*cC]
// local coordinate frame given rotation values
// spend some time up front to get an accurate rotation matrix before
// rounding to 0.7 fixed point
// the 32-bit math is only done once per frame, and then we can do
// all the per-vertex stuff in 8-bit math
Mat338 rotation_matrix(Vec38(
RescaleR((int32_t) cA*cB >> 10),
RescaleR((int32_t) cB*sA >> 10),
RescaleR(sB)),
Vec38(
RescaleR(((int32_t) -cA*sB*sC >> 10) - (int32_t) cC*sA >> 10),
RescaleR((int32_t) cA*cC - ((int32_t) sA*sB*sC >> 10) >> 10),
RescaleR((int32_t) cB*sC >> 10)),
Vec38(
RescaleR(((int32_t) -cA*cC*sB >> 10) + (int32_t) sA*sC >> 10),
RescaleR((int32_t) -cA*sC - ((int32_t) cC*sA*sB >> 10) >> 10),
RescaleR((int32_t) cB*cC >> 10)));
int8_t sortaxis = 0, sortaxisz = rotation_matrix.z.x;
if (abs(rotation_matrix.z.y) > abs(sortaxisz)) {
sortaxis = 1;
sortaxisz = rotation_matrix.z.y;
}
if (abs(rotation_matrix.z.z) > abs(sortaxisz)) {
sortaxis = 2;
sortaxisz = rotation_matrix.z.z;
}
rotation_matrix.RotateAndProject(mesh_vertices, mesh_NVERTS, scale_, verts);
// rotate and project all vertices
/*
{
Vec216 *vertptr = verts;
for (uint16_t j = 0; j < 3*mesh_NVERTS; j += 3) {
Vec38 obj_vert(
pgm_read_byte_near(mesh_vertices + j),
pgm_read_byte_near(mesh_vertices + j + 1),
pgm_read_byte_near(mesh_vertices + j + 2));
Vec38 world_vert(
Fx.dot(obj_vert),
Fy.dot(obj_vert),
Fz.dot(obj_vert));
world_vert.project(scale_, vertptr++);
}
}
*/
// back-face cull and sort faces
for (uint16_t i = 0; i < mesh_NFACES; i++) {
uint16_t jf = i;
// use face sort order depending on which axis is most facing toward camera
if (sortaxisz < 0) {
jf = mesh_NFACES - 1 - i;
}
if (sortaxis == 1) {
jf = pgm_read_byte_near(mesh_ysort_faces + jf);
} else if (sortaxis == 2) {
jf = pgm_read_byte_near(mesh_zsort_faces + jf);
}
jf *= 3;
uint8_t fa = pgm_read_byte_near(mesh_faces + jf),
fb = pgm_read_byte_near(mesh_faces + jf + 1),
fc = pgm_read_byte_near(mesh_faces + jf + 2);
Vec216 sa = verts[fb] - verts[fa];
Vec216 sb = verts[fc] - verts[fa];
if ((int32_t) sa.x * sb.y > (int32_t) sa.y * sb.x) { // check winding order
continue; // back-facing
}
int8_t illum = rotation_matrix.CalcIllumination(mesh_normals + jf);
uint8_t pat[4];
GetDitherPattern(illum, pat);
FillTriangle(
verts[fa].x, verts[fa].y,
verts[fb].x, verts[fb].y,
verts[fc].x, verts[fc].y,
pat, screen_);
}
}
class Stars {
static const int kNumStars = 30;
uint16_t wyhash16_x;
private:
// https://github.com/littleli/go-wyhash16/blob/master/wyhash16.go
uint32_t hash16(uint32_t input, uint32_t key) {
uint32_t hash = input * key;
return ((hash >> 16) ^ hash) & 0xFFFF;
}
uint16_t wyhash16() {
wyhash16_x += 0xfc15;
return hash16(wyhash16_x, 0x2ab);
}
public:
void Draw() {
wyhash16_x = 0xabcd; // reseed our private PRNG
for (uint8_t i = 0; i < kNumStars; i++) {
uint16_t randval = wyhash16();
uint8_t xpos = randval ^ (randval >> 8);
uint8_t ypos = (randval >> 8) & 63;
uint8_t xspeed = 1 + ((randval + (randval >> 13)) & 7);
uint16_t page = (ypos >> 3) << 7;
uint8_t mask = 1 << (ypos & 7);
uint8_t x = xpos - xspeed * frame_;
x >>= 1;
screen_[page + x] |= mask;
}
}
};
Stars stars_;
void loop(void) {
memset(screen_, 0, 1024);
stars_.Draw();
ReadInput();
DrawObject();
uint8_t *addr = screen_;
for (uint8_t y = 0; y < 8; y++) {
oled.setCursor(0, y);
oled.startData();
for (uint8_t i = 0; i < 128; i++)
oled.sendData(*addr++);
oled.endData();
}
frame_++;
} | {
"version": 1,
"author": "Andy Sloane (a1kon)",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "uno",
"top": 30,
"left": 134.67,
"rotate": 90,
"attrs": {}
},
{ "type": "wokwi-mpu6050", "id": "mpu", "top": 170, "left": 25, "rotate": 270, "attrs": {} },
{ "type": "wokwi-ssd1306", "id": "oled1", "top": 267.02, "left": 28.5, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 190,
"left": 110,
"rotate": 90,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 190,
"left": 90,
"rotate": 90,
"attrs": { "value": "10000" }
}
],
"connections": [
[ "uno:5V", "mpu:VCC", "red", [ "h-157", "v9" ] ],
[ "uno:5V", "r1:1", "red", [ "h0" ] ],
[ "uno:5V", "r2:1", "red", [ "h0" ] ],
[ "uno:A4", "r2:2", "LimeGreen", [ "h0" ] ],
[ "uno:A4", "oled1:DATA", "LimeGreen", [ "h-79", "v9", "h-3" ] ],
[ "uno:A4", "mpu:SDA", "LimeGreen", [ "h-79", "v9", "h-78", "v-4" ] ],
[ "uno:A5", "r1:2", "Gold", [ "h0" ] ],
[ "uno:A5", "oled1:CLK", "Gold", [ "h-70", "v9", "h-1" ] ],
[ "uno:A5", "mpu:SCL", "Gold", [ "h-70", "v9", "h-96", "v-60" ] ],
[ "uno:5V", "oled1:VIN", "red", [ "h-16", "v106", "h-20" ] ],
[ "uno:GND.2", "oled1:GND", "black", [ "h-23", "v92", "h-29" ] ],
[ "uno:GND.2", "mpu:GND", "black", [ "h-23", "v-15", "h-140", "v26" ] ]
]
} |
Generate a diagram.json code for TM1637 thermometer in pi pico | import machine
import random
import time
import tm1637
display = tm1637.TM1637(clk=machine.Pin(16), dio=machine.Pin(17))
display.scroll("Pico Thermometer", delay=200)
time.sleep(1)
display.show(' ')
time.sleep(1)
temp = random.randrange(10,15)
while True:
temp = random.randrange(10,25)
display.temperature(temp)
time.sleep(10) | {
"version": 1,
"author": "Luc Volders",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": -9.33,
"left": 28,
"attrs": { "env": "micropython-20220117-v1.18" }
},
{
"type": "wokwi-tm1637-7segment",
"id": "sevseg1",
"top": -127.03,
"left": -124.63,
"attrs": { "color": "red" }
}
],
"connections": [
[ "sevseg1:CLK", "pico:GP16", "green", [ "h70.12", "v350.78" ] ],
[ "sevseg1:DIO", "pico:GP17", "green", [ "h76.12", "v331.58" ] ],
[ "sevseg1:GND", "pico:GND.8", "black", [ "h104.12", "v158.78" ] ],
[ "sevseg1:VCC", "pico:VBUS", "red", [ "h90.78", "v149.18" ] ]
]
} |
Generate a diagram.json code for Auto room light using arduno nano | // **** Students name - Auto Room light
#include "oled.h" //load the oled with Adafruit_SSD1306.h library
void setup() {
setup_oled(); // Start the OLED Display
}
void loop() {
SW_check(); //this function does something after checking A0 low
OLED_PRINT("All OFF ");
digitalWrite(2, LOW);
digitalWrite(3, LOW);
sdelay(300); //off for 0.3 sec
OLED_PRINT("2 ON ");
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
sdelay(200); //blip for 0.2 sec
}
void SW_check() {
if (digitalRead(A0) == LOW) {
OLED_PRINT("RUNNING ");
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
sdelay(3000);
}
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -148.8, "left": -269.3, "attrs": {} },
{
"type": "board-ssd1306",
"id": "oled1",
"top": -150.46,
"left": -57.37,
"attrs": { "i2cAddress": "0x3c" }
},
{ "type": "wokwi-gnd", "id": "gnd2", "top": -192, "left": -58.2, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -32.2,
"left": -336,
"attrs": { "color": "yellow" }
},
{ "type": "wokwi-gnd", "id": "gnd3", "top": 67.2, "left": -250.2, "attrs": {} },
{ "type": "wokwi-vcc", "id": "vcc1", "top": -229.64, "left": -28.8, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd1", "top": -201.6, "left": -298.2, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -253.2,
"left": -217,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": -253.2,
"left": -178.6,
"attrs": { "color": "cyan" }
}
],
"connections": [
[ "oled1:SCL", "nano:A5", "green", [ "v-28.8", "h77.1", "v124.8", "h-393.6" ] ],
[ "oled1:SDA", "nano:A4", "green", [ "v-19.2", "h57.67", "v105.6", "h-393.6" ] ],
[ "nano:A0", "btn1:1.r", "green", [ "v19.2", "h-38.4" ] ],
[ "oled1:VCC", "vcc1:VCC", "red", [ "v0" ] ],
[ "oled1:GND", "gnd2:GND", "black", [ "v-67.2" ] ],
[ "btn1:2.r", "gnd3:GND", "green", [ "h0" ] ],
[ "led2:A", "nano:2", "green", [ "v0" ] ],
[ "led1:A", "nano:3", "green", [ "v28.8", "h28.8" ] ],
[ "gnd1:GND", "led1:C", "black", [ "v0" ] ],
[ "led2:C", "gnd1:GND", "green", [ "v9.6", "h0.4" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Temperature and humitity measurement | #include <DHT.h>
#include <LiquidCrystal_I2C.h>
//Pin definitions for the Thermostat components.
#define DHTPIN 2
#define DHTTYPE DHT22
#define RELAY_PIN 8
#define RED_PIN 9
#define GREEN_PIN 10
#define BLUE_PIN 11
#define INCREASE_BTN 3
#define DECREASE_BTN 4
//Libraries used for the thermostat project.
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
//These variable will be used to control the temperature and humidity levels in the thermostat.
float currentTemp = 25.0;
float targetTemp = 25.0;
float humidity = 50.0;
//Initializaing the Serial monitor, DHT Sensor and LCD.
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
//Set the LED pins as output and button pins as input.
pinMode(RELAY_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(INCREASE_BTN, INPUT_PULLUP);
pinMode(DECREASE_BTN, INPUT_PULLUP);
// Start with green LED
setLEDColor(0, 255, 0);
}
void loop() {
readSensor();
checkButtons();
updateLCD();
controlRelay();
updateLED();
delay(100);
}
void readSensor() {
float newTemp = dht.readTemperature();
float newHumidity = dht.readHumidity();
if (!isnan(newTemp) && !isnan(newHumidity)) {
currentTemp = newTemp;
humidity = newHumidity;
}
}
//Controlling the temperature based on the buttons. Buttons upon being pressed, can alter the temperature.
void checkButtons() {
if (digitalRead(INCREASE_BTN) == LOW) {
targetTemp += 1;
delay(200); // Debounce
}
if (digitalRead(DECREASE_BTN) == LOW) {
targetTemp -= 1;
delay(200); // Debounce
}
}
//Display the temperature and humidity level readings on the LCD we've used.
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(currentTemp, 1);
lcd.print("C ");
lcd.print(targetTemp, 1);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 1);
lcd.print("%");
}
//Controlling the relay according to the temperature. If the set temp.> room temp. then the relay turns on the heating. If the set temp.< room temp. then the relay turns off the heating.
void controlRelay() {
if (currentTemp < targetTemp) {
digitalWrite(RELAY_PIN, HIGH); // Turn on heating
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off heating
}
}
//Changing the color of the LED based on the temperature. If the set temp.> current temp. then led turns red. If set temp.< current temp. then led turns blue. If set temp.= current temp. then led turns green.
void updateLED() {
if (currentTemp < targetTemp) {
setLEDColor(0, 0, 255); // Blue
} else if (currentTemp > targetTemp) {
setLEDColor(255, 0, 0); // Red
} else {
setLEDColor(0, 255, 0); // Green
}
}
void setLEDColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard", "id": "bb1", "top": -31.8, "left": 319.6, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 288.6, "left": 393, "attrs": {} },
{
"type": "wokwi-rgb-led",
"id": "rgb1",
"top": -5.6,
"left": 711.5,
"attrs": { "common": "cathode" }
},
{ "type": "wokwi-dht22", "id": "dht1", "top": -28.5, "left": 589.8, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 90.65,
"left": 555.4,
"rotate": 180,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 28.2,
"left": 435,
"rotate": 90,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 28.2,
"left": 329.4,
"rotate": 90,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": 227.2,
"left": 831.2,
"attrs": { "pins": "i2c" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 81.6,
"left": 690.65,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 81.6,
"left": 709.85,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 81.6,
"left": 719.45,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": 90.35,
"left": 374.4,
"attrs": { "value": "10000" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": 90.35,
"left": 480,
"attrs": { "value": "10000" }
},
{ "type": "wokwi-relay-module", "id": "relay1", "top": 240.2, "left": 220.8, "attrs": {} }
],
"connections": [
[ "uno:5V", "bb1:bp.1", "red", [ "v47.9", "h-188.2" ] ],
[ "uno:GND.3", "bb1:bn.1", "black", [ "v76.7", "h-206.4" ] ],
[ "bb1:23b.h", "bb1:28b.h", "red", [ "v0" ] ],
[ "bb1:23b.i", "bb1:bp.18", "red", [ "v0" ] ],
[ "bb1:31b.i", "bb1:bn.25", "black", [ "v0" ] ],
[ "lcd1:SDA", "uno:A4", "cyan", [ "h-38.4", "v221", "h-157.8" ] ],
[ "lcd1:SCL", "uno:A5", "magenta", [ "h-19.2", "v441.9" ] ],
[ "lcd1:GND", "bb1:bn.37", "black", [ "h0" ] ],
[ "lcd1:VCC", "bb1:bp.37", "red", [ "h-67.2", "v-114.2" ] ],
[ "bb1:4b.h", "bb1:bn.2", "black", [ "v0" ] ],
[ "bb1:15b.h", "bb1:bn.11", "black", [ "v0" ] ],
[ "bb1:13b.g", "uno:3", "red", [ "v144", "h153.6" ] ],
[ "bb1:29b.h", "uno:2", "yellow", [ "v96", "h9.6" ] ],
[ "bb1:41t.e", "bb1:bn.33", "black", [ "v0" ] ],
[ "bb1:2b.g", "uno:4", "blue", [ "v105.6", "h230.4" ] ],
[ "relay1:VCC", "bb1:bp.6", "red", [ "h-19.2", "v-38.4", "h230.4" ] ],
[ "relay1:IN", "uno:8", "white", [ "h-38.4", "v67", "h384" ] ],
[ "relay1:GND", "bb1:bn.6", "black", [ "h-19.2", "v47.6", "h211.2" ] ],
[ "bb1:40b.j", "uno:11", "red", [ "v134.4", "h-134.4" ] ],
[ "bb1:42b.j", "uno:10", "limegreen", [ "v144", "h-182.4" ] ],
[ "bb1:43b.j", "uno:9", "cyan", [ "v153.6", "h-192" ] ],
[ "rgb1:R", "bb1:40t.c", "", [ "$bb" ] ],
[ "rgb1:COM", "bb1:41t.d", "", [ "$bb" ] ],
[ "rgb1:G", "bb1:42t.c", "", [ "$bb" ] ],
[ "rgb1:B", "bb1:43t.c", "", [ "$bb" ] ],
[ "dht1:VCC", "bb1:28b.f", "", [ "$bb" ] ],
[ "dht1:SDA", "bb1:29b.f", "", [ "$bb" ] ],
[ "dht1:NC", "bb1:30b.f", "", [ "$bb" ] ],
[ "dht1:GND", "bb1:31b.f", "", [ "$bb" ] ],
[ "r1:1", "bb1:29b.g", "", [ "$bb" ] ],
[ "r1:2", "bb1:23b.g", "", [ "$bb" ] ],
[ "btn1:1.l", "bb1:15t.a", "", [ "$bb" ] ],
[ "btn1:2.l", "bb1:13t.a", "", [ "$bb" ] ],
[ "btn1:1.r", "bb1:15b.f", "", [ "$bb" ] ],
[ "btn1:2.r", "bb1:13b.f", "", [ "$bb" ] ],
[ "btn2:1.l", "bb1:4t.a", "", [ "$bb" ] ],
[ "btn2:2.l", "bb1:2t.a", "", [ "$bb" ] ],
[ "btn2:1.r", "bb1:4b.f", "", [ "$bb" ] ],
[ "btn2:2.r", "bb1:2b.f", "", [ "$bb" ] ],
[ "r2:1", "bb1:40t.e", "", [ "$bb" ] ],
[ "r2:2", "bb1:40b.i", "", [ "$bb" ] ],
[ "r3:1", "bb1:42t.e", "", [ "$bb" ] ],
[ "r3:2", "bb1:42b.i", "", [ "$bb" ] ],
[ "r4:1", "bb1:43t.e", "", [ "$bb" ] ],
[ "r4:2", "bb1:43b.i", "", [ "$bb" ] ],
[ "r5:1", "bb1:4b.g", "", [ "$bb" ] ],
[ "r5:2", "bb1:10b.g", "", [ "$bb" ] ],
[ "r6:1", "bb1:15b.g", "", [ "$bb" ] ],
[ "r6:2", "bb1:21b.g", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Telephone dialer using buzzr with arduino-nano | // Arduino Phone Dialer.
// Uses the pulse dialing method to dial a phone number.
int offtimer = 60; // The higher the number, the slower the timing.
int ontimer = 30;
uint8_t phone_number[] = {1,2,3,4,5,6,7,8,9,0}; // the phone number to dial
//int number_length = 9; // the number of digits in the phone number (i.e. the length of the array)
int out=13; // the output pin to the pulsing relay
int didthis=0;
uint8_t DebugModeSerial = 1;
//DTMF--------------------------
#include "Tone.h"
Tone freq1;
Tone freq2;
const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477 };
const int DTMF_freq2[] = { 941, 697, 697, 697, 770, 770, 770, 852, 852, 852 };
//DTMF--------------------------
void setup(){
Serial.begin(115200);
pinMode(out, OUTPUT); // define pin as output.
freq1.begin(3);
freq2.begin(4);
}
void loop(){
if(didthis == 0){
digitalWrite(out, HIGH); // turn on the relay to establish a connection to the phone (i.e. plugging in the phone)
delay (1000);
//uint8_t Phone_number[] = {1,2,3,4,5,6,7,8,9,0};
Pulse_Dialer(phone_number,sizeof(phone_number));
}
didthis=1;
digitalWrite(out, HIGH); // turn the relay ON to keep the phone on the line and wait for the other end to ring.
DTMF_Dialing();
}
void Pulse_Dialer(uint8_t phone_number[],uint8_t Phone_Len){
for(int count=0; count<Phone_Len; count++){
delay (1000); // delay after every number dialed
uint8_t Digit = phone_number[count];
if(Digit == 0){Digit = 10;}
if(DebugModeSerial){
Serial.print("DigitCount ");
Serial.print(count);
Serial.print(" Digit ");
Serial.print(Digit);
}
for(int digit=0; digit<Digit; digit++){
if(DebugModeSerial){
Serial.print(" LOW ");
}
digitalWrite(out, LOW); // turn the relay OFF
delay(offtimer); // wait for a while
digitalWrite(out, HIGH); // turn the relay ON
delay(ontimer); // wait for a while
}
if(DebugModeSerial){
Serial.println(" End ");
}
}
if(DebugModeSerial){
Serial.println(" End Pulse_Dialer");
}
}
void playDTMF(uint8_t number, long duration){
freq1.play(DTMF_freq1[number], duration);
freq2.play(DTMF_freq2[number], duration);
}
void DTMF_Dialing(){
int i;
uint8_t phone_number[] = {2,0,4};//{ 8, 6, 7, 5, 3, 0 ,9 };
for(i=0; i<sizeof(phone_number); i ++){
Serial.print(phone_number[i], 10);
Serial.print(' ');
playDTMF(phone_number[i], 500);
delay(600);
}
Serial.println();
delay(4000);
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 82.8,
"left": 61.8,
"attrs": { "color": "blue", "flip": "1" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 100.8,
"left": -11.15,
"rotate": 90,
"attrs": { "value": "100" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": -53.3,
"left": 56.55,
"rotate": 90,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": -52.3,
"left": 75.75,
"rotate": 90,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -170.4,
"left": 49.8,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-buzzer",
"id": "bz2",
"top": -170.4,
"left": 165,
"attrs": { "volume": "0.1" }
}
],
"connections": [
[ "bz1:1", "nano:GND.2", "black", [ "h48", "v91.2" ] ],
[ "r2:1", "bz1:2", "green", [ "h0" ] ],
[ "r1:2", "led1:A", "green", [ "h0" ] ],
[ "led1:C", "nano:GND.1", "black", [ "h57.2", "v-62.4" ] ],
[ "bz1:1", "bz2:1", "black", [ "v0" ] ],
[ "r2:2", "nano:4", "green", [ "v8.4", "h10.1" ] ],
[ "bz2:2", "r3:1", "green", [ "v0" ] ],
[ "r3:2", "nano:3", "green", [ "h0" ] ],
[ "r1:1", "nano:13", "green", [ "h0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Automatic light using PIR sensor and arduino-uno | const byte LED_TINO = A1;
const byte SENSOR_TINO = A0;
boolean pir_Value = 0;
void setup() {
pinMode(LED_TINO, OUTPUT);
pinMode(SENSOR_TINO, INPUT);
Serial.begin(9600);
Serial.println("Tugas 2: Septiandi Budi Triantino");
delay(3000);
}
void loop() {
pir_Value = digitalRead(SENSOR_TINO);
digitalWrite(LED_TINO, pir_Value);
Serial.println("Sensor Value:" + (String)pir_Value);
if (pir_Value) {
digitalWrite(LED_TINO, HIGH);
}
else {
digitalWrite(LED_TINO, LOW);
}
delay(1000);
} | {
"version": 1,
"author": "Ajang Rahmat",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": -18.6, "left": -67.8, "attrs": {} },
{ "type": "wokwi-pir-motion-sensor", "id": "pir1", "top": 71.2, "left": 232.62, "attrs": {} },
{ "type": "wokwi-relay-module", "id": "relay1", "top": 230.6, "left": 201.6, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 73.2,
"left": 339.8,
"attrs": { "color": "yellow" }
},
{ "type": "wokwi-vcc", "id": "vcc1", "top": 19.96, "left": 384, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd1", "top": 307.2, "left": 354.6, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd2", "top": 307.2, "left": 162.6, "attrs": {} },
{ "type": "wokwi-gnd", "id": "gnd3", "top": 240, "left": 37.8, "attrs": {} }
],
"connections": [
[ "uno:5V", "pir1:VCC", "red", [ "v38.3", "h224.6" ] ],
[ "uno:GND.2", "pir1:GND", "black", [ "v28.7", "h253.5" ] ],
[ "uno:A0", "pir1:OUT", "green", [ "v19.1", "h195.8" ] ],
[ "relay1:IN", "uno:A1", "yellow", [ "h0" ] ],
[ "relay1:VCC", "uno:5V", "red", [ "h0" ] ],
[ "vcc1:VCC", "led1:A", "red", [ "v0" ] ],
[ "gnd1:GND", "relay1:NO", "black", [ "v0" ] ],
[ "relay1:COM", "led1:C", "black", [ "h0" ] ],
[ "relay1:GND", "gnd2:GND", "black", [ "h0" ] ],
[ "uno:GND.2", "gnd3:GND", "black", [ "v28.7", "h-53.7" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Hybrid solar pump control using ESP-32 | #include <SPI.h>
#include<EEPROM.h>
#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PID_v1.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define AVG_NUM 10
#define offsetVoltage 2.5 // for ACS712 sensor
#define Sensitivity 0.066 // 66mV/A for ACS712-30A variant
#define Sensitivity1 0.04 // 40mV/A for ACS758 -50A variant
#define PV_volt_pin 34 // defining the analog pin ADC1_CH6 to read solar panel Voltage
#define PV_current_pin 35 //defining the Analog pin ADC1_CH7 to measure solar current
#define bat_volt_pin 32 //defining the analog pin ADC1_CH4 to read battery voltage
#define bat_current_pin 33 //defining the Analog pin ADC1_CH5 to measure battery current
#define inv_volt_pin 25 //defining the Analog pin ADC2_CH8 to measure inverter input volt
#define inv_current_pin 26 //defining the Analog pin ADC2_CH9 to measure inverter input current
#define load_volt_pin 12 //defining the Analog pin ADC2_CH5 to measure DC load output volt
#define load_current_pin 27 //defining the Analog pin ADC2_CH7 to measure DC load output current
#define pwm_pin 14 //PWM Pin GPIO14 conncet to MOsfet Driver
#define DCload_pin 13 // Defining DC load control pin GPIO 13 to drive MOSFET Q2
#define BULK_CHRARGE_SP 14.4
#define FLOAT_CHARGE_SP 13.5
#define Charge_Restart_SP 13.2
#define MIN_SOLAR_VOLT 10
#define LVD 11.5
#define LVR 12.5
#define ABSORPTION_LIMIT 3600000 // 1 hour in milliseconds
#define NIGHT_TIME 3600000 // 1 hour in milliseconds
#define CHARGER_RESTART_TIME 600000 // 10 mins in milliseconds
#define EVENING_TIME 300000 // 5mins in milliseconds
#define MORNING_TIME 180000 // 3mins in milliseconds
#define DCLOAD_VOLT_SP 12.3
#define gradDetect 26.3157894373 // Gradient for SoC Detect
#define Vb_FULL_SCALE 13.8 //SoC 100% Baterai 13.8 Volt
const int PWM_CHANNEL = 0; // ESP32 has 16 channels which can generate 16 independent waveforms
const int PWM_FREQ = 980.39; // Recall that Arduino Uno is ~490 Hz. Official ESP32 example uses 5,000Hz
const int PWM_RESOLUTION = 8; // We'll use same resolution as Uno (8 bits, 0-255) but ESP32 can go up to 16 bits
// The max duty cycle value based on PWM resolution (will be 255 if resolution is 8 bits)
const int MAX_DUTY_CYCLE = (int)(pow(2, PWM_RESOLUTION) - 1);
const int DELAY_MS = 4; // delay between fade increments
float PV_volt=0; // solar panel voltage
float PV_current=0; // solar panel current
float bat_volt=0; // Battery voltage
float bat_current=0; //Battery current (Charge pr Discharge)
float inv_volt=0; // Inverter voltage input
float inv_current=0; // Inverter current input
float load_volt=0; // DC Load current
float load_current=0; // DC Load current
float PV_watts=0; //PV Watt
float PV_wattHours=0; //PV Watt Hours-Calculate
float load_watts=0; //DC Load watt
float pwm_duty=0; // PWM Duty Cycle (0 to 255)
float pwm_dutyDC=0; // PWM Duty Cycle (0 to 255)
float bulk_charge_sp=0; // Bulk charging set point
float float_charge_sp=0; // Float charging set point
int load_status; // 0-off, 1- on
const int bat_type = 1; //Flooded=0,AGM=1
int celsius=36;
int temp_change=0;
float temp_val=0;
int disp,dcloadstate;
unsigned long last_time=0;
unsigned long current_time=0;
unsigned long absorption_time; // to keep track of today's time in absorption state
unsigned long charger_millis; // to keep track of time for charger
unsigned long restart_time;
unsigned long load_millis;
int mode =1;
float SoC_Detect = 0;
float temp_SoC_Detect =0;
float SoCCorr_k_1 =0;
float SoC_VB=0;
int SOC_VB_Temp =0;
float bat_volt_k_1 =0;
float persen1;
int persen;
//***************************PID Controller***********************************
double Sp, input_val, u;
PID myPID(&input_val, &u, &Sp,2,5,1, DIRECT); //Kp= 2 , Ki= 5 and Kd=1
enum charger_state {off, bulk,absorption,Float} charger_state ;
///////////////////////DECLARATION OF ALL BIT MAP ARRAY FOR FONTS////////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------------------------------------------------------
byte solar[8] = //icon for solar panel
{
0b11111,0b10101,0b11111,0b10101,0b11111,0b10101,0b11111,0b00000
};
byte battery[8] = //icon for battery
{
0b01110,0b11011,0b10001,0b10001,0b10001,0b10001,0b10001,0b11111
};
byte charge[8] = // icon for battery charge
{
0b01010,0b11111,0b10001,0b10001,0b10001,0b01110,0b00100,0b00100,
};
byte not_charge[8]=
{
0b00000,0b10001,0b01010,0b00100,0b01010,0b10001,0b00000,0b00000,
};
byte temp[8] = //icon for termometer
{
0b00100,0b01010,0b01010,0b01110,0b01110,0b11111,0b11111,0b01110
};
byte energy[8] = // icon for power
{
0b00010,0b00100,0b01000,0b11111,0b00010,0b00100,0b01000,0b00000
};
byte PWM[8] = {
B11101,
B10101,
B10101,
B10101,
B10101,
B10101,
B10101,
B10111
};
//**************SoC Detect*************************************************
float SOC_Detect()
{
SoC_Detect = (gradDetect*(bat_volt - 10.0));
return SoC_Detect;
}
void SoC_Estimator(float SOCCOOR_K_1, float BAT_VOLT_K_1, float BAT_VOLT,float *SOC_VB)
{
*SOC_VB = SOCCOOR_K_1 + ((BAT_VOLT - BAT_VOLT_K_1)/(Vb_FULL_SCALE - BAT_VOLT_K_1 )*(100 - SOCCOOR_K_1));
}
void INIT(int DCloadstate)
{
//**********************Raw 0 (SOLAR INDICATOR)***********************
lcd.setCursor(0, 0); //(Kolom, Baris)
lcd.print("SOL");
lcd.createChar(0, solar);
lcd.setCursor(4, 0); //(Kolom, Baris)
lcd.write(0);
lcd.setCursor(0, 1); //(Kolom, Baris)
lcd.print(PV_volt,1);
lcd.setCursor(4, 1); //(Kolom, Baris)
lcd.print('V');
lcd.setCursor(0, 2); //(Kolom, Baris)
lcd.print(PV_current,1);
lcd.setCursor(4, 2); //(Kolom, Baris)
lcd.print('A');
lcd.setCursor(0, 3); //(Kolom, Baris)
lcd.print(PV_watts);
lcd.setCursor(4, 3); //(Kolom, Baris)
lcd.print('W');
//*******************Raw 1 (BATTERAI INDICATOR)********************************************
lcd.setCursor(7, 0); //(Kolom, Baris)
lcd.print("BAT");
lcd.createChar(1, battery);
lcd.setCursor(11, 0); //(Kolom, Baris)
lcd.write(1);
lcd.setCursor(7, 1); //(Kolom, Baris)
lcd.print(bat_volt,1);
lcd.setCursor(11, 1); //(Kolom, Baris)
lcd.print('V');
lcd.setCursor(7, 2); //(Kolom, Baris)
lcd.print(bat_current,1);
lcd.setCursor(11, 2); //(Kolom, Baris)
lcd.print('A');
lcd.createChar(2, charge);
lcd.setCursor(7, 3); //(Kolom, Baris)
lcd.write(2);
if(disp == 0)
{
lcd.createChar(3, not_charge); //menampilkan ke Display kondisi sedang tidak melakukan charging
lcd.setCursor(8, 3); //(Kolom, Baris)
lcd.write(3);
}
else if(disp == 1)
{
lcd.setCursor(8, 3); //(Kolom, Baris)
lcd.print('B'); //menampilkan ke Display kondisi charging mode Bulk Charging
}
else if(disp == 2)
{
lcd.setCursor(8, 3); //(Kolom, Baris)
lcd.print('A'); //menampilkan ke Display kondisi charging mode Float Charging
}
else if(disp == 3)
{
lcd.setCursor(8, 3); //(Kolom, Baris)
lcd.print('F'); //menampilkan ke Display kondisi charging mode absorption Charging
}
if(SOC_VB_Temp == 0)
{
lcd.setCursor(9, 3); //(Kolom, Baris)
lcd.print(' ');
}
else
{
lcd.setCursor(9, 3); //(Kolom, Baris)
lcd.print(SOC_VB_Temp);
}
lcd.setCursor(11, 3); //(Kolom, Baris)
lcd.print("%");
//*******************Raw 12 (PWM INDICATOR)********************************************
lcd.setCursor(14, 0); //(Kolom, Baris)
lcd.print("PWM");
lcd.createChar(6, PWM);
lcd.setCursor(18, 0); //(Kolom, Baris)
lcd.write(6);
lcd.setCursor(15, 1); //(Kolom, Baris)
lcd.print(persen); // satuan pwm_duty dalam persen
lcd.print("% ");
lcd.setCursor(13, 2); //(Kolom, Baris)
lcd.print("LOAD DC"); // satuan pwm_duty dalam persen
if(DCloadstate == 0)
{
lcd.setCursor(15, 3); //(Kolom, Baris)
lcd.print("OFF"); // satuan pwm_duty dalam persen
}
else if(DCloadstate == 1)
{
lcd.setCursor(15, 3); //(Kolom, Baris)
lcd.print("ON "); // satuan pwm_duty dalam persen
}
}
int read_adc(int channel){
int sum = 0;
int temp;
int i;
for (i=0; i<AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM number of times
temp = analogRead(channel); // read the input pin
sum += temp; // store sum for averaging
delayMicroseconds(50); // pauses for 50 microseconds
}
return(sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
}
void Initial_setpoint()
{
PV_volt= read_adc(PV_volt_pin)*0.0048828*(37.5/7.5);
PV_current = ((read_adc(PV_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
PV_watts = PV_current * PV_volt;
bat_volt= read_adc(bat_volt_pin)*0.0048828*(37.5/7.5);
bat_current = ((read_adc(bat_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
inv_volt= read_adc(inv_volt_pin)*0.0048828*(37.5/7.5);
inv_current = ((read_adc(inv_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity1 );
load_volt= read_adc(load_volt_pin)*0.0048828*(37.5/7.5);
load_current = ((read_adc(load_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
load_watts = load_current * load_volt;
if (PV_current <0 || PV_watts < 0)
{
PV_current = 0;
PV_watts = 0;
}
if (bat_current <0)
{
bat_current = 0;
}
if (load_current <0 || load_watts < 0)
{
load_current = 0;
load_watts = 0;
}
temp_change =celsius-25.0; // 25deg cel is taken as standard room temperature ( STC)
// for floaded battery
if (bat_type == 0)
{
bulk_charge_sp =BULK_CHRARGE_SP-(0.020*temp_change) ;
float_charge_sp=FLOAT_CHARGE_SP-(0.020*temp_change) ;
}
// for AGM battery // set point is lowered to avoid excess gassing
else
{
bulk_charge_sp =(BULK_CHRARGE_SP-0.2)-(0.020*temp_change) ;
float_charge_sp=(FLOAT_CHARGE_SP-0.2)-(0.020*temp_change) ;
}
}
void Run_charge()
{
PV_volt= read_adc(PV_volt_pin)*0.0048828*(37.5/7.5);
PV_current = ((read_adc(PV_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
PV_watts = PV_current * PV_volt;
bat_volt= read_adc(bat_volt_pin)*0.0048828*(37.5/7.5);
bat_current = ((read_adc(bat_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
inv_volt= read_adc(inv_volt_pin)*0.0048828*(37.5/7.5);
inv_current = ((read_adc(inv_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity1 );
load_volt= read_adc(load_volt_pin)*0.0048828*(37.5/7.5);
load_current = ((read_adc(load_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
load_watts = load_current * load_volt;
if (PV_current <0 || PV_watts < 0)
{
PV_current = 0;
PV_watts = 0;
}
if (bat_current <0)
{
bat_current = 0;
}
if (load_current <0 || load_watts < 0)
{
load_current = 0;
load_watts = 0;
}
temp_SoC_Detect = SOC_Detect();
if(temp_SoC_Detect < 0)
{
temp_SoC_Detect = 0;
}
current_time = millis();
if(current_time - last_time >= 1000)
{
bat_volt_k_1 = bat_volt;
SoCCorr_k_1 = temp_SoC_Detect;
last_time = current_time;
}
SoC_Estimator(SoCCorr_k_1,bat_volt_k_1,bat_volt,&SoC_VB);
SOC_VB_Temp = (int) SoC_VB;
if(SOC_VB_Temp > 99)
{
SOC_VB_Temp = 99;
}
if(SOC_VB_Temp < 0)
{
SOC_VB_Temp = 0;
}
temp_change =celsius-25.0; // 25deg cel is taken as standard room temperature ( STC)
// for floaded battery
if (bat_type == 0)
{
bulk_charge_sp =BULK_CHRARGE_SP-(0.020*temp_change) ;
float_charge_sp=FLOAT_CHARGE_SP-(0.020*temp_change) ;
}
// for AGM battery // set point is lowered to avoid excess gassing
else
{
bulk_charge_sp =(BULK_CHRARGE_SP-0.2)-(0.020*temp_change) ;
float_charge_sp=(FLOAT_CHARGE_SP-0.2)-(0.020*temp_change) ;
}
switch (charger_state) {
case off :
if (( bat_volt < float_charge_sp) && (PV_volt > (bat_volt + 0.5) )){
charger_millis = millis() ;
charger_state = bulk ;
}
else if ( ( bat_volt > float_charge_sp) && (PV_volt > (bat_volt + 0.5)) && (absorption_time > ABSORPTION_LIMIT)){
charger_millis = millis() ;
restart_time = 0 ;
charger_state = Float ;
}
else
{
disp=0;
absorption_time = 0;
pwm_duty = 0;
persen1=(pwm_duty/255)*100;
persen = (int) persen1;
analogWrite(pwm_pin, pwm_duty);
}
break; // end of case off condition
// During this stage the MOSFET is fully on by setting the duty cycle to 100%
// Constant Current Charging
case bulk :
if (PV_volt < bat_volt ){
charger_millis = millis();
charger_state = off;
disp=0;
}
else if ((bat_volt > bulk_charge_sp) && (PV_volt > (bat_volt + 0.5)) && (absorption_time < ABSORPTION_LIMIT)){
charger_millis = millis();
charger_state = absorption;
disp=2;
}
else if ((bat_volt > float_charge_sp) && (PV_volt > (bat_volt + 0.5)) && (absorption_time > ABSORPTION_LIMIT)){
charger_millis = millis();
restart_time = 0;
charger_state = Float;
disp=3;
}
else{
disp=1;
pwm_duty = 255;
persen1=(pwm_duty/255)*100;
persen = (int) persen1;
//ledcWrite(pwm_pin, pwm_duty); //generate PWM from GPIO 14 @ 100% duty // MOSFET Q1 is ON
analogWrite(pwm_pin, pwm_duty);
if( bat_volt > bulk_charge_sp )
{
charger_state = absorption;
disp=2;
}
}
break; // end of case bulk condition
// During this stage the MOSFET is partially on by setting the duty cycle to between 0% and 100%
// Constant voltage
case absorption :
if (PV_volt < bat_volt){
charger_millis = millis();
charger_state = off;
disp=0;
}
else if ((bat_volt > float_charge_sp) && (PV_volt > (bat_volt + 0.5)) && (absorption_time > ABSORPTION_LIMIT)){
charger_millis = millis();
charger_state = Float;
disp=3;
}
else{
// increment absorption timer and test for duration in absorption state
absorption_time = absorption_time + millis() - charger_millis ;
charger_millis = millis();
disp=2;
input_val = bat_volt;
Sp = bulk_charge_sp;
myPID.Compute(); // Compute PID Output
pwm_duty = u*5 ; // Output = kp * error + ki * errSum + kd * dErr
if(pwm_duty < 0)
{
pwm_duty=0;
}
if(pwm_duty >255)
{
pwm_duty=255;
}
persen1=(pwm_duty/255)*100;
persen = (int) persen1;
//ledcWrite(pwm_pin, pwm_duty);
analogWrite(pwm_pin, pwm_duty);
}
break; // end of case absorption condition
// During this stage the MOSFET is partially on by setting the duty cycle between 0% and 100%
// Constant Voltage Charging
case Float :
if (PV_volt < bat_volt){
charger_millis = millis();
charger_state = off;
disp=0;
}
else if ((bat_volt < Charge_Restart_SP) && (PV_volt > (bat_volt + 0.5) ) && (restart_time > CHARGER_RESTART_TIME)){
charger_millis = millis();
charger_state = bulk;
disp=1;
}
else if ((bat_volt >float_charge_sp) && (PV_volt > (bat_volt + 0.5) ) && ( absorption_time < ABSORPTION_LIMIT)){
charger_millis = millis();
charger_state = absorption;
disp=2;
}
else
{
disp=3;
if (bat_volt > float_charge_sp)
{
pwm_duty--;
if(pwm_duty <0)
{
pwm_duty=0;
}
persen1=(pwm_duty/255)*100;
persen = (int) persen1;
analogWrite(pwm_pin, pwm_duty);
}
else
{
pwm_duty = 12.75; // setting duty cycle = 5% for trickle charge
persen1=(pwm_duty/255)*100;
persen = (int) persen1;
analogWrite(pwm_pin, pwm_duty); //generate PWM from D3 @ 5% duty // Q1 is driving @ 5% duty cycle
}
if (bat_volt < Charge_Restart_SP)
{
restart_time = restart_time + millis() - charger_millis ;
charger_millis = millis();
}
}
break; // end of case float condition
}
}
void DCLoad_Control()
{
PV_volt= read_adc(PV_volt_pin)*0.0048828*(37.5/7.5);
PV_current = ((read_adc(PV_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
PV_watts = PV_current * PV_volt;
bat_volt= read_adc(bat_volt_pin)*0.0048828*(37.5/7.5);
load_volt= read_adc(load_volt_pin)*0.0048828*(37.5/7.5);
load_current = ((read_adc(load_current_pin)*0.0048828 - offsetVoltage)/ Sensitivity );
load_watts = load_current * load_volt;
if (PV_current <0 || PV_watts < 0)
{
PV_current = 0;
PV_watts = 0;
}
if (bat_current <0)
{
bat_current = 0;
}
if (load_current <0 || load_watts < 0)
{
load_current = 0;
load_watts = 0;
}
if ((PV_volt < 8.0) && (bat_volt > LVD))
{
//DC Load Control Voltage for Constant Voltage in 12V
if(load_volt > DCLOAD_VOLT_SP)
{
pwm_dutyDC--;
pwm_dutyDC = constrain(pwm_dutyDC,0,254);
}
else
{
pwm_dutyDC++;
pwm_dutyDC = constrain(pwm_dutyDC,0,254);
}
analogWrite(DCload_pin, pwm_dutyDC); //load will turn on during evening
dcloadstate =1;
}
else if ((PV_volt < 8.0) && (bat_volt < LVD))
{
pwm_dutyDC = 0;
analogWrite(DCload_pin, pwm_dutyDC); // load will turn off during morning
dcloadstate =0;
}
else if (PV_volt > MIN_SOLAR_VOLT)
{
pwm_dutyDC = 0;
analogWrite(DCload_pin, pwm_dutyDC); // load will turn off during morning
dcloadstate =0;
}
else if((bat_volt < LVD) || (load_watts > 60))
{
pwm_dutyDC = 0;
analogWrite(DCload_pin, pwm_dutyDC); // load will turn off during morning
dcloadstate =0;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
analogReadResolution(10);
pinMode(pwm_pin, OUTPUT);
pinMode(DCload_pin, OUTPUT);
// Sets up a channel (0-15), a PWM duty cycle frequency, and a PWM resolution (1 - 16 bits)
// ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);
// ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
// // ledcAttachPin(uint8_t pin, uint8_t channel);
// ledcAttachPin(pwm_pin, PWM_CHANNEL);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0); lcd.print("*******************");
lcd.setCursor(0,1); lcd.print(" SOLAR PUMP ");
lcd.setCursor(0,2); lcd.print(" CONTROLLER V00 ");
lcd.setCursor(0,3); lcd.print("*******************");
delay(1000);
lcd.clear();
input_val = 12;
Sp= 14.5;
//turn the PID on
myPID.SetMode(AUTOMATIC);
Initial_setpoint();
INIT(dcloadstate);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
Run_charge();
INIT(dcloadstate);
DCLoad_Control();
Serial.print(pwm_duty);
Serial.print("; ");
Serial.print(disp);
Serial.print("; ");
Serial.print(persen1);
Serial.print("; ");
Serial.print(load_volt);
Serial.print("; ");
Serial.print(pwm_dutyDC);
Serial.print("\n");
} | {
"version": 1,
"author": "Ilmirrizki Imaduddin",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 76.8, "left": -23.96, "attrs": {} },
{
"type": "wokwi-lcd2004",
"id": "lcd1",
"top": -118.4,
"left": -61.6,
"attrs": { "pins": "i2c" }
},
{ "type": "wokwi-potentiometer", "id": "pot1", "top": -58.9, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot2", "top": 37.1, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot3", "top": 142.7, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot4", "top": 248.3, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot5", "top": 411.5, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot6", "top": 507.5, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot7", "top": 603.5, "left": -384.2, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot8", "top": 709.1, "left": -384.2, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 188.4,
"left": 138.2,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 283.2,
"left": 133.85,
"rotate": 90,
"attrs": { "value": "330" }
},
{ "type": "wokwi-led", "id": "led2", "top": 399.6, "left": 23, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r2",
"top": 475.2,
"left": 18.65,
"rotate": 90,
"attrs": { "value": "330" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "lcd1:SCL", "esp:22", "green", [ "h-48", "v125.1", "h211.2", "v57.6" ] ],
[ "esp:21", "lcd1:SDA", "green", [ "h57.6", "v-76.8", "h-220.8", "v-144", "h9.6" ] ],
[ "lcd1:GND", "esp:GND.1", "black", [ "h-67.2", "v28.8" ] ],
[ "lcd1:VCC", "esp:5V", "red", [ "h-86.4", "v48.1" ] ],
[ "pot1:GND", "pot2:GND", "black", [ "v0", "h-38.4", "v115.2", "h38.4" ] ],
[ "pot2:GND", "pot3:GND", "black", [ "v28.8", "h-48", "v96", "h48" ] ],
[ "pot3:GND", "pot4:GND", "black", [ "v19.2", "h-48", "v105.6", "h48" ] ],
[ "pot4:GND", "pot5:GND", "black", [ "v19.2", "h-48", "v163.2", "h48" ] ],
[ "pot5:GND", "pot6:GND", "black", [ "v19.2", "h-48", "v96", "h48" ] ],
[ "pot6:GND", "pot7:GND", "black", [ "v19.2", "h-48", "v96", "h48" ] ],
[ "pot1:VCC", "pot2:VCC", "red", [ "v19.2", "h37.6", "v96", "h-38.4" ] ],
[ "pot2:VCC", "pot3:VCC", "red", [ "v19.2", "h37.6", "v105.6", "h-38.4" ] ],
[ "pot3:VCC", "pot4:VCC", "red", [ "v19.2", "h37.6", "v105.6", "h-38.4", "v-19.2" ] ],
[ "pot4:VCC", "pot5:VCC", "red", [ "v19.2", "h37.6", "v163.2", "h-38.4" ] ],
[ "pot5:VCC", "pot6:VCC", "red", [ "v19.2", "h37.6", "v96", "h-38.4" ] ],
[ "pot6:VCC", "pot7:VCC", "red", [ "v19.2", "h37.6", "v96", "h-38.4" ] ],
[ "pot1:SIG", "esp:34", "green", [ "v28.8", "h182", "v96" ] ],
[ "pot2:SIG", "esp:35", "green", [ "v28.8", "h86", "v9.6" ] ],
[ "pot3:SIG", "esp:32", "green", [ "v28.8", "h182", "v-76.8" ] ],
[ "pot4:SIG", "esp:33", "green", [ "v38.4", "h182", "v-182.4" ] ],
[ "pot5:SIG", "esp:25", "green", [ "v28.8", "h182", "v-307.2" ] ],
[ "pot6:SIG", "esp:26", "green", [ "v19.2", "h182", "v-297.6" ] ],
[ "pot7:GND", "pot8:GND", "black", [ "v19.2", "h-48", "v115.2", "h48", "v-9.6" ] ],
[ "pot7:VCC", "pot8:VCC", "red", [ "v19.2", "h37.6", "v115.2", "h-38.4" ] ],
[ "esp:GND.3", "led1:C", "black", [ "h67.2", "v86.4", "h9.6" ] ],
[ "led1:A", "r1:1", "green", [ "v0" ] ],
[ "r1:2", "esp:13", "green", [ "h-211.2", "v-78" ] ],
[ "led2:C", "esp:GND.1", "green", [ "v0", "h-114.8", "v-211.2" ] ],
[ "led2:A", "r2:1", "green", [ "v0" ] ],
[ "r2:2", "esp:14", "green", [ "h-105.6", "v-298.8" ] ],
[ "pot7:SIG", "esp:12", "green", [ "v38.4", "h210.8", "v-489.6", "h48" ] ],
[ "pot8:SIG", "esp:27", "green", [ "v38.4", "h249.2", "v-28.8" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for servo control using ESP-32 | #include <ESP32MCPWM.h>
ESP32MCPWM motorController;
void setup() {
Serial.begin(115200);
// Attach servo to GPIO pin 5
motorController.attachServo(5);
// Set initial angle to 0 degrees
motorController.setServoAngle(0);
delay(2000);
// Rotate servo to 90 degrees
motorController.setServoAngle(90);
delay(2000);
// Rotate servo to 180 degrees
motorController.setServoAngle(180);
delay(2000);
// Return servo to 0 degrees
motorController.setServoAngle(0);
}
void loop() {
// Add your loop logic here
} | {
"version": 1,
"author": "Asep Oman Somantri",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo1", "top": 46, "left": 182.4, "attrs": {} }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "servo1:GND", "esp:GND.1", "black", [ "h-28.8", "v124.8", "h-182.4", "v-76.8", "h9.6" ] ],
[ "esp:5V", "servo1:V+", "red", [ "h-23.81", "v38.4", "h0", "v0", "h182.4", "v-124.8" ] ],
[ "esp:5", "servo1:PWM", "green", [ "h0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Gas Leak and Fire Outbreak Monitoring System | #include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "AbhinavAce"
#define AIO_KEY "aio_cXhx00DLUv05w7NRa4eeeJ3bXCT2"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish sector1_status = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sensor data");
LiquidCrystal_I2C lcd(0x27, 16, 2);
int gas=34;
int heat=32;
int led=2;
#define gas_leak 500
#define fire_break 700
void connectWiFi() {
Serial.print("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}
void connectMQTT() {
while (!mqtt.connected()) {
Serial.print("Connecting to MQTT...");
if (mqtt.connect()) {
Serial.println("Connected!");
} else {
delay(2000);
}
}
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
// Initialize WiFi and MQTT
connectWiFi();
connectMQTT();
lcd.setCursor(0, 1);
lcd.print("System Ready");
delay(2000);
lcd.clear();
pinMode(gas,INPUT);
pinMode(heat,INPUT);
pinMode(led,OUTPUT);
}
void loop() {
if (!mqtt.connected()) {
connectMQTT();}
mqtt.processPackets(10000);
mqtt.ping();
int gas1=analogRead(gas);
int heat1=analogRead(heat);
bool gasLeak1=gas1>gas_leak;
bool fire1=heat1<fire_break;
if (gasLeak1 && fire1){
digitalWrite(led, HIGH);
lcd.setCursor(0, 0);
lcd.print("Danger in Sec 1");
sector1_status.publish("Gas + Fire");
}
else if (gasLeak1){
digitalWrite(led, HIGH);
lcd.setCursor(0, 0);
lcd.print("Gas Leak Sec 1");
sector1_status.publish("Gas Leak");
}
else if (fire1){
lcd.setCursor(0, 0);
lcd.print("Fire in Sec 1");
sector1_status.publish("Fire");
}
else{
lcd.setCursor(0, 0);
lcd.print("Sec 1: Stable");
sector1_status.publish("Stable");
}
delay(1000);
} | {
"version": 1,
"author": "Abhinavendra A R",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 96, "left": 33.64, "attrs": {} },
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": -128,
"left": 178.4,
"attrs": { "pins": "i2c" }
},
{
"type": "wokwi-ntc-temperature-sensor",
"id": "ntc1",
"top": -103,
"left": -173.4,
"attrs": {}
},
{ "type": "chip-gas-sensor", "id": "chip1", "top": -162.18, "left": 4.8, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -224.4,
"left": -111.4,
"attrs": { "color": "red" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "lcd1:SDA", "esp:21", "green", [ "h-38.4", "v249.8" ] ],
[ "lcd1:SCL", "esp:22", "green", [ "h-19.2", "v9.9" ] ],
[ "esp:GND.2", "lcd1:GND", "black", [ "v0" ] ],
[ "esp:3V3", "lcd1:VCC", "red", [ "h0.15", "v-211.2" ] ],
[ "esp:3V3", "ntc1:VCC", "red", [ "h-38.25", "v-192" ] ],
[ "ntc1:GND", "esp:GND.2", "black", [ "h0" ] ],
[ "ntc1:OUT", "esp:32", "yellow", [ "h9.6", "v230.3" ] ],
[ "chip1:VCC", "esp:3V3", "red", [ "h-28.8", "v249.6" ] ],
[ "chip1:GND", "esp:GND.2", "black", [ "h-19.2", "v230.4", "h144" ] ],
[ "chip1:A0", "esp:34", "yellow", [ "h-19.2", "v278.4" ] ],
[ "led1:C", "chip1:GND", "black", [ "v0" ] ],
[ "led1:A", "esp:2", "green", [ "v76.8", "h105.6", "v76.8", "h163.2", "v182.4" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Dot matrix display clock with temperature info | #include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <DHT.h>
#include <SPI.h>
#include <Wire.h>
#include "Font7Seg.h"
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4 // Define the number of displays connected
#define CLK_PIN 13 // CLK or SCK
#define DATA_PIN 11 // DATA or MOSI
#define CS_PIN 10 // CS or SS
#define SPEED_TIME 75 // Speed of the transition
#define PAUSE_TIME 0
#define MAX_MESG 20
// These are for the clock
#define DS1307_ADDRESS 0x68
// These are for the temperature
#define DHTPIN 2
#define DHTTYPE DHT22
#define TIMEDHT 1000
// Global variables
uint8_t wday, mday, month, year;
uint8_t hours, minutes, seconds;
char szTime[9]; // mm:ss\0
char szMesg[MAX_MESG + 1] = "";
float humidity, celsius, fahrenheit;
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C
uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 }; // Deg F
uint8_t clear = 0x00;
uint32_t timerDHT = TIMEDHT;
DHT dht(DHTPIN, DHTTYPE);
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
void beginDS1307()
{
// Read the values (date and time) of the DS1307 module
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(clear);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 0x07);
seconds = bcdToDec(Wire.read());
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0xff);
wday = bcdToDec(Wire.read());
mday = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
uint8_t decToBcd(uint8_t value)
{
return ((value / 10 * 16) + (value % 10));
}
uint8_t bcdToDec(uint8_t value)
{
return ((value / 16 * 10) + (value % 16));
}
// Code for reading clock time
void getTime(char *psz, bool f = true)
{
sprintf(psz, "%02d%c%02d", hours, (f ? ':' : ' '), minutes);
}
// Code for reading clock date
void getDate(char *psz)
{
char szBuf[10];
sprintf(psz, "%d %s %04d", mday , mon2str(month, szBuf, sizeof(szBuf) - 1), (year + 2000));
}
// Code for get Temperature
void getTemperature()
{
// Wait for a time between measurements
if ((millis() - timerDHT) > TIMEDHT) {
// Update the timer
timerDHT = millis();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
celsius = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
fahrenheit = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(celsius) || isnan(fahrenheit)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
// Get a label from PROGMEM into a char array
char *mon2str(uint8_t mon, char *psz, uint8_t len)
{
static const __FlashStringHelper* str[] =
{
F("Jan"), F("Feb"), F("Mar"), F("Apr"),
F("May"), F("Jun"), F("Jul"), F("Aug"),
F("Sep"), F("Oct"), F("Nov"), F("Dec")
};
strncpy_P(psz, (const char PROGMEM *)str[mon - 1], len);
psz[len] = '\0';
return (psz);
}
char *dow2str(uint8_t code, char *psz, uint8_t len)
{
static const __FlashStringHelper* str[] =
{
F("Sunday"), F("Monday"), F("Tuesday"),
F("Wednesday"), F("Thursday"), F("Friday"),
F("Saturday")
};
strncpy_P(psz, (const char PROGMEM *)str[code - 1], len);
psz[len] = '\0';
return (psz);
}
void setup(void)
{
Wire.begin();
P.begin(2);
P.setInvert(false);
P.setZone(0, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.setZone(1, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.displayZoneText(1, szTime, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
P.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT , PA_NO_EFFECT);
P.addChar('$', degC);
P.addChar('&', degF);
dht.begin();
}
void loop(void)
{
static uint32_t lastTime = 0; // Memory (ms)
static uint8_t display = 0; // Current display mode
static bool flasher = false; // Seconds passing flasher
beginDS1307();
getTemperature();
P.displayAnimate();
if (P.getZoneStatus(0))
{
switch (display)
{
case 0: // Temperature deg Celsius
P.setPause(0, 1000);
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_UP);
display++;
dtostrf(celsius, 3, 1, szMesg);
strcat(szMesg, "$");
break;
case 1: // Temperature deg Fahrenheit
P.setTextEffect(0, PA_SCROLL_UP, PA_SCROLL_DOWN);
display++;
dtostrf(fahrenheit, 3, 1, szMesg);
strcat(szMesg, "&");
break;
case 2: // Humidity
P.setTextEffect(0, PA_SCROLL_DOWN, PA_SCROLL_LEFT);
display++;
dtostrf(humidity, 3, 0, szMesg);
strcat(szMesg, "%UR");
break;
case 3: // Clock
P.setFont(0, numeric7Seg);
P.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
P.setPause(0, 0);
if ((millis() - lastTime) >= 1000)
{
lastTime = millis();
getTime(szMesg, flasher);
flasher = !flasher;
}
if ((seconds == 00) && (seconds <= 30)) {
display++;
P.setTextEffect(0, PA_PRINT, PA_WIPE_CURSOR);
}
break;
case 4: // Day of week
P.setFont(0, nullptr);
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
display++;
dow2str(wday, szMesg, MAX_MESG);
break;
default: // Calendar
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
display = 0;
getDate(szMesg);
break;
}
P.displayReset(0); // Rest zone zero
}
} | {
"version": 1,
"author": "Uri Shaked, Anderson Costa",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 120, "left": 80, "attrs": {} },
{
"type": "wokwi-max7219-matrix",
"id": "m1",
"top": 20,
"left": 115,
"attrs": { "chain": "4" }
},
{ "type": "wokwi-ds1307", "id": "ds1307", "top": 254.03, "left": 380, "attrs": {} },
{
"type": "wokwi-dht22",
"id": "dht",
"top": 122.35,
"left": 398.34,
"attrs": { "color": "red" }
}
],
"connections": [
[ "uno:GND.1", "m1:GND", "black", [ "v-12", "*", "h30" ] ],
[ "uno:10", "m1:CS", "green", [ "v-24", "*", "h22" ] ],
[ "uno:11", "m1:DIN", "orange", [ "v-20", "*", "h26" ] ],
[ "uno:13", "m1:CLK", "blue", [ "v-16", "*", "h18" ] ],
[ "uno:GND.3", "ds1307:GND", "black", [ "v20", "*", "h-30" ] ],
[ "uno:5V", "ds1307:5V", "red", [ "v35", "*", "h-15" ] ],
[ "uno:A4", "ds1307:SDA", "orange", [ "v30", "*", "h-20" ] ],
[ "uno:A5", "ds1307:SCL", "purple", [ "v25", "*", "h-25" ] ],
[ "uno:2", "dht:SDA", "yellow", [ "v0", "h0", "*", "h-50", "v5" ] ],
[ "uno:5V", "m1:V+", "red", [ "v35.21", "h247.23", "v-310.74" ] ],
[ "uno:5V", "dht:VCC", "red", [ "v35.11", "h247.22", "v-95.82", "h-80.12" ] ],
[ "uno:GND.3", "dht:GND", "black", [ "v20.24", "h101.02", "v-85", "h89.21" ] ]
]
} |
Generate a diagram.json code for NLSF595 Arduino Shift Register | const int dataPin = 2; /* SI */
const int clockPin = 3; /* SCK */
const int latchPin = 4; /* RCK */
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void sendData(uint8_t pattern) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, pattern);
digitalWrite(latchPin, HIGH);
}
void loop() {
// Red
sendData(0b11011011);
delay(500);
// Green
sendData(0b10110111);
delay(500);
// Purple
sendData(0b01001011);
delay(500);
} | {
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 183.33, "left": -51.33, "attrs": {} },
{
"type": "wokwi-nlsf595",
"id": "sr1",
"top": 102.5,
"left": 182.16,
"rotate": 90,
"attrs": {}
},
{ "type": "wokwi-rgb-led", "id": "rgb1", "top": -20, "left": 3.34, "attrs": {} },
{ "type": "wokwi-rgb-led", "id": "rgb2", "top": -20, "left": 101.33, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 75.34,
"left": -18.67,
"rotate": 90,
"attrs": {}
},
{ "type": "wokwi-resistor", "id": "r2", "top": 75.34, "left": 0, "rotate": 90, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r3",
"top": 75.33,
"left": 17.34,
"rotate": 90,
"attrs": {}
},
{ "type": "wokwi-resistor", "id": "r4", "top": 70, "left": 79.34, "rotate": 90, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r5", "top": 69.33, "left": 98, "rotate": 90, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r6",
"top": 68.66,
"left": 114.67,
"rotate": 90,
"attrs": {}
}
],
"connections": [
[ "sr1:GND", "uno:GND.1", "black", [ "h0" ] ],
[ "sr1:VCC", "uno:5V", "red", [ "h30", "v323", "h-156" ] ],
[ "sr1:SI", "uno:2", "blue", [ "h16", "v70", "h-68" ] ],
[ "uno:GND.1", "sr1:OE", "black", [ "v-22", "h184", "v-52" ] ],
[ "sr1:RCK", "uno:4", "gray", [ "h4", "v38", "h-66" ] ],
[ "r3:1", "rgb1:B", "blue", [ "h0" ] ],
[ "r2:1", "rgb1:G", "green", [ "h0" ] ],
[ "rgb1:R", "r1:1", "brown", [ "v0" ] ],
[ "rgb2:R", "r4:1", "brown", [ "v0" ] ],
[ "rgb2:G", "r5:1", "green", [ "v0" ] ],
[ "rgb2:B", "r6:1", "blue", [ "v0" ] ],
[ "sr1:QA", "r6:2", "blue", [ "h10.86", "v-27.16", "h-88.61", "v44.05" ] ],
[ "sr1:QC", "r4:2", "brown", [ "h-36.66", "v25.24", "h-57.6" ] ],
[ "sr1:QB", "r5:2", "green", [ "h-41.46", "v30.04", "h-33.6" ] ],
[ "sr1:QD", "r3:2", "blue", [ "h-31.86", "v20.44", "h-124.8" ] ],
[ "sr1:QE", "r2:2", "green", [ "h-22.26", "v15.64", "h-148.8" ] ],
[ "sr1:QF", "r1:2", "brown", [ "h-17.46", "v10.84", "h-172.8" ] ],
[ "rgb2:COM", "rgb1:COM", "red", [ "v9", "h-97.75" ] ],
[ "uno:5V", "rgb1:COM", "red", [ "v19.59", "h-164.65", "v-351", "h77.65" ] ],
[ "sr1:SCK", "uno:3", "purple", [ "h8.28", "v33", "h-67.15" ] ]
]
} |
Generate a diagram.json code for LED with switch using arduino-uno | //int buttonPin = 2; // Button connected to pin 2
int ledPin = 13; // LED connected to pin 13
//int buttonState = 0;
void setup() {
pinMode(2, INPUT_PULLUP); // Set the button pin as input.
pinMode(ledPin, OUTPUT); // Set the LED pin as output.
}
void loop() {
digitalRead(2); // Read the button state.
if (digitalRead(2) == LOW) { // If the button is pressed
digitalWrite(ledPin, HIGH); // Turn on the LED.
} else {
digitalWrite(ledPin, LOW); // Turn off the LED.
}
} | {
"version": 1,
"author": "Yuvaraj s",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-pushbutton-6mm",
"id": "btn1",
"top": -69.4,
"left": 240,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": -63.25,
"left": 144,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-led", "id": "led1", "top": -99.6, "left": 90.2, "attrs": { "color": "red" } }
],
"connections": [
[ "led1:A", "r1:1", "green", [ "v0" ] ],
[ "r1:2", "uno:13", "green", [ "v38.4", "h-78" ] ],
[ "btn1:2.l", "uno:2", "green", [ "h0" ] ],
[ "led1:C", "uno:GND.1", "green", [ "v48", "h10" ] ],
[ "btn1:1.r", "uno:GND.1", "green", [ "v-48", "h-181.6", "v96" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for multi-colour LED | void setup() {
pinMode(4, OUTPUT);//กำหนดขา 4 เป็น OUTPUT
pinMode(5, OUTPUT);//กำหนดขา 5 เป็น OUTPUT
pinMode(6, OUTPUT);//กำหนดขา 6 เป็น OUTPUT
pinMode(7, OUTPUT);//กำหนดขา 7 เป็น OUTPUT
pinMode(8, OUTPUT);//กำหนดขา 8 เป็น OUTPUT
pinMode(9, OUTPUT);//กำหนดขา 9 เป็น OUTPUT
pinMode(10, OUTPUT);//กำหนดขา 10 เป็น OUTPUT
pinMode(11, OUTPUT);//กำหนดขา 11 เป็น OUTPUT
pinMode(12, OUTPUT);//กำหนดขา 12 เป็น OUTPUT
pinMode(13, OUTPUT);//กำหนดขา 13 เป็น OUTPUT
}
void loop() {
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
delay(300);
digitalWrite(12, HIGH);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
digitalWrite(5, LOW);
delay(300);
digitalWrite(11, HIGH);
digitalWrite(11, LOW);
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
delay(300);
digitalWrite(10, HIGH);
digitalWrite(10, LOW);
digitalWrite(7, HIGH);
digitalWrite(7, LOW);
delay(300);
digitalWrite(9, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
digitalWrite(8, LOW);
delay(300);
digitalWrite(9, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
digitalWrite(8, LOW);
digitalWrite(10, HIGH);
digitalWrite(10, LOW);
digitalWrite(7, HIGH);
digitalWrite(7, LOW);
delay(300);
digitalWrite(11, HIGH);
digitalWrite(11, LOW);
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
delay(300);
digitalWrite(12, HIGH);
digitalWrite(12, LOW);
digitalWrite(5, HIGH);
digitalWrite(5, LOW);
delay(300);
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
digitalWrite(4, HIGH);
digitalWrite(4, LOW);
delay(300);
} | {
"version": 1,
"author": "ศุภชัย ยามี",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 183, "left": -0.6, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -42, "left": -140.2, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led2",
"top": -42,
"left": -53.8,
"attrs": { "color": "green" }
},
{ "type": "wokwi-led", "id": "led3", "top": -42, "left": 71, "attrs": { "color": "yellow" } },
{ "type": "wokwi-led", "id": "led4", "top": -42, "left": 13.4, "attrs": { "color": "blue" } },
{
"type": "wokwi-led",
"id": "led5",
"top": -51.6,
"left": 119,
"attrs": { "color": "orange" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": -42,
"left": 224.6,
"attrs": { "color": "purple" }
},
{
"type": "wokwi-led",
"id": "led7",
"top": -42,
"left": 349.4,
"attrs": { "color": "limegreen" }
},
{
"type": "wokwi-led",
"id": "led8",
"top": -42,
"left": 176.6,
"attrs": { "color": "white" }
},
{
"type": "wokwi-led",
"id": "led9",
"top": -42,
"left": 282.2,
"attrs": { "color": "magenta" }
},
{
"type": "wokwi-led",
"id": "led10",
"top": -42,
"left": 397.4,
"attrs": { "color": "cyan" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 61,
"left": -144.85,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 80.2,
"left": 373.55,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 70.6,
"left": 335.15,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 51.4,
"left": 277.55,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": 51.4,
"left": 210.35,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": 41.8,
"left": 171.95,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r7",
"top": 51.4,
"left": 114.35,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r8",
"top": 61,
"left": 66.35,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r9",
"top": 61,
"left": 18.35,
"rotate": 270,
"attrs": { "value": "220000" }
},
{
"type": "wokwi-resistor",
"id": "r10",
"top": 70.6,
"left": -58.45,
"rotate": 270,
"attrs": { "value": "220000" }
}
],
"connections": [
[ "led2:C", "uno:GND.1", "green", [ "v28.8", "h115.6" ] ],
[ "led4:C", "uno:GND.1", "green", [ "v19.2", "h86.8" ] ],
[ "led3:C", "uno:GND.1", "green", [ "v48", "h58" ] ],
[ "led5:C", "uno:GND.1", "green", [ "v153.6", "h-19.1" ] ],
[ "led8:C", "uno:GND.1", "green", [ "v153.6", "h-57.5" ] ],
[ "led6:C", "uno:GND.1", "green", [ "v28.8", "h-28.4" ] ],
[ "led9:C", "uno:GND.1", "green", [ "v38.4", "h-57.2" ] ],
[ "led7:C", "uno:GND.1", "green", [ "v28.8", "h-86" ] ],
[ "led10:C", "uno:GND.1", "green", [ "v48", "h-105.2" ] ],
[ "led1:A", "r1:2", "green", [ "v0" ] ],
[ "led2:A", "r10:2", "green", [ "v0" ] ],
[ "led4:A", "r9:2", "green", [ "v0" ] ],
[ "led3:A", "r8:2", "green", [ "v0" ] ],
[ "led5:A", "r7:2", "green", [ "v0" ] ],
[ "led8:A", "r6:2", "green", [ "v0" ] ],
[ "led6:A", "r5:2", "green", [ "v0" ] ],
[ "led9:A", "r4:2", "green", [ "v0" ] ],
[ "led7:A", "r3:2", "green", [ "v0" ] ],
[ "led10:A", "r2:2", "green", [ "v0" ] ],
[ "led1:C", "uno:GND.1", "green", [ "v19.2", "h-18.8", "v134.4", "h144" ] ],
[ "r1:1", "uno:13", "green", [ "h0", "v28.8", "h134.4" ] ],
[ "r10:1", "uno:12", "green", [ "h0", "v38.4", "h105.6" ] ],
[ "r9:1", "uno:11", "green", [ "h0", "v28.8", "h86.4" ] ],
[ "r8:1", "uno:10", "green", [ "h0", "v38.4", "h67.2" ] ],
[ "r7:1", "uno:9", "green", [ "h0", "v38.4", "h48" ] ],
[ "r6:1", "uno:8", "green", [ "h9.6", "v38.4", "h19.2" ] ],
[ "r5:1", "uno:7", "green", [ "h0" ] ],
[ "r4:1", "uno:6", "green", [ "h0" ] ],
[ "r3:1", "uno:5", "green", [ "h0" ] ],
[ "r2:1", "uno:4", "green", [ "h0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for four road junction traffic light using arduino-uno | int red1 = 13;
int yellow1 = 12;
int green1 = 11;
int red2 = 10;
int yellow2 = 9;
int green2 = 8;
int red3 =5;
int yellow3 =6;
int green3 = 7;
int red4 =2;
int yellow4=3;
int green4=4 ;
void setup() {
// put your setup code here, to run once:
//1
pinMode(red1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(green1, OUTPUT);
//2
pinMode(red2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green2, OUTPUT);
//3
pinMode(red3, OUTPUT);
pinMode(yellow3, OUTPUT);
pinMode(green3, OUTPUT);
//4
pinMode(red4, OUTPUT);
pinMode(yellow4, OUTPUT);
pinMode(green4, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(green1, HIGH);
digitalWrite(yellow1,LOW);
digitalWrite(red1,LOW);
delay(5000);
digitalWrite(green1, LOW);
digitalWrite(yellow1, HIGH);
digitalWrite(red1,LOW);
delay(2000);
digitalWrite(green1, LOW);
digitalWrite(yellow1, LOW);
digitalWrite(red1,HIGH);
delay(3000);
//2
digitalWrite(green2, HIGH);
digitalWrite(yellow2,LOW);
digitalWrite(red2,LOW);
delay(5000);
digitalWrite(green2, LOW);
digitalWrite(yellow2, HIGH);
digitalWrite(red2,LOW);
delay(2000);
digitalWrite(green2, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(red2,HIGH);
delay(3000);
//3
digitalWrite(green3, HIGH);
digitalWrite(yellow3,LOW);
digitalWrite(red3,LOW);
delay(5000);
digitalWrite(green3, LOW);
digitalWrite(yellow3, HIGH);
digitalWrite(red3,LOW);
delay(2000);
digitalWrite(green3, LOW);
digitalWrite(yellow3, LOW);
digitalWrite(red3,HIGH);
delay(3000);
//4
digitalWrite(green4, HIGH);
digitalWrite(yellow4,LOW);
digitalWrite(red4,LOW);
delay(5000);
digitalWrite(green3, LOW);
digitalWrite(yellow4, HIGH);
digitalWrite(red4,LOW);
delay(2000);
digitalWrite(green4, LOW);
digitalWrite(yellow4, LOW);
digitalWrite(red4,HIGH);
delay(3000);
} | {
"version": 1,
"author": "Doungkmon Namyhong",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-mini", "id": "bb1", "top": 104.2, "left": -472.8, "attrs": {} },
{ "type": "wokwi-breadboard-mini", "id": "bb2", "top": 469, "left": 36, "attrs": {} },
{ "type": "wokwi-breadboard-mini", "id": "bb3", "top": 104.2, "left": 439.2, "attrs": {} },
{ "type": "wokwi-breadboard-mini", "id": "bb4", "top": -203, "left": -12, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 87, "left": -77.4, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": 82.8,
"left": 493.4,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 447.6,
"left": 99.8,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": 447.6,
"left": 128.6,
"attrs": { "color": "limegreen" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": 82.8,
"left": -447.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": 82.8,
"left": -418.6,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": 82.8,
"left": -389.8,
"attrs": { "color": "limegreen" }
},
{ "type": "wokwi-led", "id": "led7", "top": -224.4, "left": 23, "attrs": { "color": "red" } },
{ "type": "wokwi-led", "id": "led8", "top": 447.6, "left": 71, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led9",
"top": -224.4,
"left": 51.8,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-led",
"id": "led10",
"top": -224.4,
"left": 80.6,
"attrs": { "color": "limegreen" }
},
{
"type": "wokwi-led",
"id": "led11",
"top": 82.8,
"left": 531.8,
"attrs": { "color": "limegreen" }
},
{ "type": "wokwi-led", "id": "led12", "top": 82.8, "left": 455, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 148.8,
"left": -451.75,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 148.8,
"left": -422.95,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 148.8,
"left": -394.15,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 513.6,
"left": 124.25,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": 513.6,
"left": 66.65,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": 513.6,
"left": 95.45,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r7",
"top": 148.8,
"left": 450.65,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r8",
"top": 148.8,
"left": 489.05,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r9",
"top": 148.8,
"left": 527.45,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r10",
"top": -158.4,
"left": 76.25,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r11",
"top": -158.4,
"left": 47.45,
"rotate": 90,
"attrs": { "value": "220" }
},
{
"type": "wokwi-resistor",
"id": "r12",
"top": -158.4,
"left": 18.65,
"rotate": 90,
"attrs": { "value": "220" }
}
],
"connections": [
[ "uno:13", "r7:2", "red", [ "v-86.4", "h-66.8" ] ],
[ "uno:12", "r8:2", "gold", [ "v-96", "h-37.9" ] ],
[ "uno:11", "r9:2", "green", [ "v-96", "h-9" ] ],
[ "uno:10", "r5:2", "red", [ "v-48", "h192.7", "v192", "h134.4" ] ],
[ "uno:9", "r6:2", "gold", [ "v-28.8", "h173.6", "v192", "h182.4" ] ],
[ "uno:8", "r4:2", "green", [ "v-19.2", "h144.4", "v192", "h230.4" ] ],
[ "uno:5", "r1:2", "red", [ "v278.4", "h90.2", "v220.8", "h-192" ] ],
[ "uno:6", "bb1:8b.g", "gold", [ "v288", "h90.1", "v201.6", "h-163.2" ] ],
[ "bb1:8b.g", "r2:2", "gold", [ "v0" ] ],
[ "uno:7", "r3:2", "green", [ "v297.6", "h90", "v172.8", "h-124.8" ] ],
[ "uno:2", "r12:2", "red", [ "v-19.2", "h-351.1", "v182.4", "h-172.8" ] ],
[ "uno:3", "r11:2", "gold", [ "v-38.4", "h-360.8", "v192", "h-124.8" ] ],
[ "uno:4", "r10:2", "green", [ "v-57.6", "h-370.5", "v201.6", "h-76.8" ] ],
[ "uno:GND.1", "led12:C", "black", [ "v-76.8", "h-66.9" ] ],
[ "uno:GND.1", "led1:C", "black", [ "v-76.8", "h-28.5" ] ],
[ "uno:GND.1", "led11:C", "black", [ "v-76.8", "h19.5" ] ],
[ "uno:GND.1", "led8:C", "black", [ "v-67.2", "h240.3", "v201.6", "h115.2" ] ],
[ "uno:GND.1", "led2:C", "black", [ "v-67.2", "h240.3", "v201.6", "h144" ] ],
[ "uno:GND.1", "led3:C", "black", [ "v-67.2", "h240.3", "v201.6", "h172.8" ] ],
[ "uno:GND.1", "led4:C", "black", [ "v278.4", "h-18.9" ] ],
[ "uno:GND.1", "led5:C", "black", [ "v259.2", "h-441.7" ] ],
[ "uno:GND.1", "led6:C", "black", [ "v288", "h38.7" ] ],
[ "uno:GND.1", "led7:C", "black", [ "v-57.6", "h-412.5" ] ],
[ "uno:GND.1", "led9:C", "black", [ "v-57.6", "h-383.7" ] ],
[ "uno:GND.1", "led10:C", "black", [ "v-57.6", "h-345.3" ] ],
[ "led1:A", "bb3:8t.b", "", [ "$bb" ] ],
[ "led1:C", "bb3:7t.b", "", [ "$bb" ] ],
[ "led11:A", "bb3:12t.b", "", [ "$bb" ] ],
[ "led11:C", "bb3:11t.b", "", [ "$bb" ] ],
[ "led12:A", "bb3:4t.b", "", [ "$bb" ] ],
[ "led12:C", "bb3:3t.b", "", [ "$bb" ] ],
[ "r7:1", "bb3:4t.b", "", [ "$bb" ] ],
[ "r7:2", "bb3:4b.f", "", [ "$bb" ] ],
[ "r8:1", "bb3:8t.b", "", [ "$bb" ] ],
[ "r8:2", "bb3:8b.f", "", [ "$bb" ] ],
[ "r9:1", "bb3:12t.b", "", [ "$bb" ] ],
[ "r9:2", "bb3:12b.f", "", [ "$bb" ] ],
[ "led2:A", "bb2:9t.b", "", [ "$bb" ] ],
[ "led2:C", "bb2:8t.b", "", [ "$bb" ] ],
[ "led3:A", "bb2:12t.b", "", [ "$bb" ] ],
[ "led3:C", "bb2:11t.b", "", [ "$bb" ] ],
[ "led8:A", "bb2:6t.b", "", [ "$bb" ] ],
[ "led8:C", "bb2:5t.b", "", [ "$bb" ] ],
[ "r4:1", "bb2:12t.b", "", [ "$bb" ] ],
[ "r4:2", "bb2:12b.f", "", [ "$bb" ] ],
[ "r5:1", "bb2:6t.b", "", [ "$bb" ] ],
[ "r5:2", "bb2:6b.f", "", [ "$bb" ] ],
[ "r6:1", "bb2:9t.b", "", [ "$bb" ] ],
[ "r6:2", "bb2:9b.f", "", [ "$bb" ] ],
[ "led7:A", "bb4:6t.b", "", [ "$bb" ] ],
[ "led7:C", "bb4:5t.b", "", [ "$bb" ] ],
[ "led9:A", "bb4:9t.b", "", [ "$bb" ] ],
[ "led9:C", "bb4:8t.b", "", [ "$bb" ] ],
[ "led10:A", "bb4:12t.b", "", [ "$bb" ] ],
[ "led10:C", "bb4:11t.b", "", [ "$bb" ] ],
[ "r10:1", "bb4:12t.b", "", [ "$bb" ] ],
[ "r10:2", "bb4:12b.f", "", [ "$bb" ] ],
[ "r11:1", "bb4:9t.b", "", [ "$bb" ] ],
[ "r11:2", "bb4:9b.f", "", [ "$bb" ] ],
[ "r12:1", "bb4:6t.b", "", [ "$bb" ] ],
[ "r12:2", "bb4:6b.f", "", [ "$bb" ] ],
[ "led4:A", "bb1:5t.b", "", [ "$bb" ] ],
[ "led4:C", "bb1:4t.b", "", [ "$bb" ] ],
[ "led5:A", "bb1:8t.b", "", [ "$bb" ] ],
[ "led5:C", "bb1:7t.b", "", [ "$bb" ] ],
[ "led6:A", "bb1:11t.b", "", [ "$bb" ] ],
[ "led6:C", "bb1:10t.b", "", [ "$bb" ] ],
[ "r1:1", "bb1:5t.b", "", [ "$bb" ] ],
[ "r1:2", "bb1:5b.f", "", [ "$bb" ] ],
[ "r2:1", "bb1:8t.b", "", [ "$bb" ] ],
[ "r2:2", "bb1:8b.f", "", [ "$bb" ] ],
[ "r3:1", "bb1:11t.b", "", [ "$bb" ] ],
[ "r3:2", "bb1:11b.f", "", [ "$bb" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for motion detection alarm using PIR sensor,esp-32,buzzer | #include <Arduino.h>
const int pirPin = 14; // PIR Motion Sensor connected to GPIO 14
const int buzzerPin = 13; // Buzzer connected to GPIO 13
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
Serial.println("Motion Detected!");
tone(buzzerPin, 1000, 500); // Sound the buzzer for 500 milliseconds
} else {
noTone(buzzerPin);
}
delay(1000); // Delay for 1 second to avoid false triggers
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-mini", "id": "bb1", "top": 94.6, "left": 122.4, "attrs": {} },
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 9.6, "left": -23.96, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -45.6,
"left": 117,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-pir-motion-sensor",
"id": "pir1",
"top": -15.2,
"left": -180.18,
"attrs": {}
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "bz1:2", "esp:13", "green", [ "v0" ] ],
[ "bz1:1", "bb1:1t.d", "black", [ "v0" ] ],
[ "pir1:VCC", "esp:3V3", "red", [ "v0" ] ],
[ "pir1:GND", "bb1:1t.c", "black", [ "v0" ] ],
[ "pir1:OUT", "esp:14", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for water level indicator using arduino-uno | const int sensorPin = A0;
const int ledPins[] = {2, 3, 4, 5, 6};
const int numLeds = 5;
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
int ledValue = map(sensorValue, 0, 1023, 0, numLeds);
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], i < ledValue ? HIGH : LOW);
}
Serial.println(sensorValue);
delay(1000);
} | {
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-breadboard-half", "id": "bb1", "top": -41.4, "left": -64.4, "attrs": {} },
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 125.4, "left": -48.6, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": -51.6, "left": 61.4, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led2",
"top": -51.6,
"left": 90.2,
"attrs": { "color": "orange" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": -51.6,
"left": 119,
"attrs": { "color": "green" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": -51.6,
"left": 176.6,
"attrs": { "color": "white" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": -51.6,
"left": 147.8,
"attrs": { "color": "cyan" }
},
{ "type": "chip-water-level-sensor", "id": "chip1", "top": 356.22, "left": 33.6, "attrs": {} }
],
"connections": [
[ "led4:A", "uno:2", "green", [ "v67.2", "h-13.7" ] ],
[ "led5:A", "uno:3", "green", [ "h9.6", "v144" ] ],
[ "led3:A", "uno:4", "green", [ "v76.8", "h24.9" ] ],
[ "led2:A", "uno:5", "green", [ "v86.4", "h9.6" ] ],
[ "led1:A", "uno:6", "green", [ "v96", "h9.6" ] ],
[ "led1:C", "bb1:4t.a", "black", [ "v0" ] ],
[ "led2:C", "bb1:4t.b", "black", [ "v0" ] ],
[ "led3:C", "bb1:4t.c", "black", [ "v0" ] ],
[ "led5:C", "bb1:4t.d", "black", [ "v0" ] ],
[ "led4:C", "bb1:4t.e", "black", [ "v0" ] ],
[ "bb1:4t.e", "uno:GND.1", "black", [ "v0" ] ],
[ "chip1:VCC", "uno:5V", "red", [ "h0" ] ],
[ "chip1:GND", "uno:GND.2", "black", [ "h0" ] ],
[ "chip1:OUT", "uno:A0", "green", [ "v0" ] ]
],
"dependencies": {}
}
{
"name": "water-level-sensor",
"author": "",
"pins": [
"VCC",
"GND",
"OUT"
],
"controls": []
} |
Generate a diagram.json code for Flame Detection System using IR Sensor, Buzzer, Arduino Nano | const int flameSensorPin = 2;
const int buzzerPin = 3;
void setup() {
pinMode(flameSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int flameValue = digitalRead(flameSensorPin);
if (flameValue == HIGH) {
Serial.println("Flame detected!");
tone(buzzerPin, 1000, 500); // Sound the buzzer for 500 milliseconds
} else {
Serial.println("No flame detected.");
}
delay(1000); // Check every second
} | {
"version": 1,
"author": "Vigneshwaran",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
{ "type": "wokwi-ir-receiver", "id": "ir1", "top": -106.95, "left": 27.02, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": 50.4,
"left": 136.2,
"attrs": { "volume": "0.1" }
}
],
"connections": [
[ "ir1:VCC", "nano:5V", "red", [ "v0" ] ],
[ "ir1:GND", "nano:GND.1", "black", [ "v96", "h96" ] ],
[ "ir1:DAT", "nano:2", "green", [ "h57.6", "v24" ] ],
[ "bz1:1", "nano:GND.2", "black", [ "h-28.8", "v-153.6" ] ],
[ "bz1:2", "nano:3", "yellow", [ "v19.2", "h-58", "v-153.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Sound reactive LED ring | #include <Adafruit_NeoPixel.h>
#define PIN 6 // Pin connected to the Data In of the NeoPixel ring
#define NUMPIXELS 16 // Number of LEDs in the ring
#define SOUND_SENSOR A0 // Analog pin connected to the sound sensor
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pixels.begin(); // Initialize the NeoPixel library
Serial.begin(9600);
}
void loop() {
int soundLevel = analogRead(SOUND_SENSOR); // Read the sound level from the sensor
int brightness = map(soundLevel, 0, 1023, 0, 255); // Map the sound level to a brightness value
// Set the brightness for each LED in the ring
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, brightness)); // Blue color, adjust RGB values as needed
}
pixels.show(); // Update the LEDs to show the new colors
Serial.println(soundLevel); // Print the sound level to the Serial Monitor
delay(50); // Small delay to avoid rapid changes
} | {
"version": 1,
"author": "Vigneshwaran",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-small-sound-sensor",
"id": "",
"top": -107,
"left": 48,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led-ring",
"id": "ring1",
"top": 77.44,
"left": 20.39,
"attrs": { "pixels": "16" }
}
],
"connections": [
[ ":DOUT", "nano:5V", "red", [ "v28.1", "h67.2", "v100.8" ] ],
[ ":GND", "nano:GND.2", "black", [ "h0" ] ],
[ ":AOUT", "nano:A0", "green", [ "h-9.6", "v76.8", "h10.1" ] ],
[ "ring1:GND", "nano:GND.1", "black", [ "v0" ] ],
[ "ring1:VCC", "nano:5V", "red", [ "v0" ] ],
[ "ring1:DIN", "nano:6", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Keypad Door Lock using arduino-uno,servo and membrane-keypad | include <Keypad.h>
#include <Servo.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo myServo;
const String password = "1234";
String inputPassword;
void setup() {
Serial.begin(9600);
myServo.attach(10); // Servo connected to pin D10
myServo.write(0); // Lock position
Serial.println("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print(key);
if (key == '#') {
if (inputPassword == password) {
Serial.println("\nAccess Granted");
myServo.write(90); // Unlock position
delay(5000); // Keep the door unlocked for 5 seconds
myServo.write(0); // Lock position
} else {
Serial.println("\nAccess Denied");
}
inputPassword = ""; // Clear the input password
} else {
inputPassword += key; // Append the key to the input password
}
}
} | {
"version": 1,
"author": "",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 115.8, "left": -10.2, "attrs": {} },
{ "type": "wokwi-membrane-keypad", "id": "keypad1", "top": -309.2, "left": 34.4, "attrs": {} },
{ "type": "wokwi-servo", "id": "servo1", "top": 26.8, "left": -201.6, "attrs": {} }
],
"connections": [
[ "keypad1:R1", "uno:2", "green", [ "v57.6", "h96" ] ],
[ "keypad1:R2", "uno:3", "green", [ "v67.2", "h76.4" ] ],
[ "keypad1:R3", "uno:4", "green", [ "v76.8", "h57.3" ] ],
[ "keypad1:R4", "uno:5", "green", [ "v86.4", "h28.6" ] ],
[ "keypad1:C1", "uno:6", "green", [ "v0" ] ],
[ "keypad1:C2", "uno:7", "green", [ "v0" ] ],
[ "keypad1:C3", "uno:8", "green", [ "v0" ] ],
[ "keypad1:C4", "uno:9", "green", [ "v0" ] ],
[ "servo1:GND", "uno:GND.2", "black", [ "h-67.2", "v230.5" ] ],
[ "servo1:V+", "uno:5V", "red", [ "h-48", "v57.5", "h399.4" ] ],
[ "servo1:PWM", "uno:10", "orange", [ "h-19.2", "v67.4", "h345.6" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Temperature data logger using dht-22,arduino-mega,micro-sd-card | #include <SPI.h>
#include <SD.h>
#include <dht.h>
// DHT22 Pin
#define DHTPIN 2
dht DHT;
// SD Card Module CS Pin
#define SD_CS_PIN 53
void setup() {
Serial.begin(9600);
DHT.begin();
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD Card initialization failed!");
return;
}
Serial.println("SD Card initialized successfully!");
}
void loop() {
delay(2000); // Read every 2 seconds
float humidity = DHT.readHumidity();
float temperature = DHT.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String dataString = String(humidity) + "," + String(temperature);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println("Data logged to SD card!");
} else {
Serial.println("Error opening datalog.txt");
}
} | {
"version": 1,
"author": "",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-mega", "id": "mega", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-dht22", "id": "dht1", "top": -143.7, "left": 186.6, "attrs": {} },
{ "type": "wokwi-microsd-card", "id": "sd1", "top": -114.97, "left": 9.67, "attrs": {} }
],
"connections": [
[ "dht1:VCC", "mega:5V", "red", [ "v163.2", "h-38.4" ] ],
[ "dht1:GND", "mega:GND.2", "black", [ "v182.4", "h-57.6" ] ],
[ "dht1:SDA", "mega:2", "green", [ "v0" ] ],
[ "sd1:VCC", "mega:5V", "red", [ "h0" ] ],
[ "sd1:GND", "mega:GND.3", "black", [ "h0" ] ],
[ "sd1:DO", "mega:50", "green", [ "h0" ] ],
[ "sd1:DI", "mega:51", "green", [ "h0" ] ],
[ "sd1:CS", "mega:53", "green", [ "h307.2", "v211.26" ] ],
[ "sd1:CD", "mega:GND.3", "green", [ "h57.6", "v326.4", "h38.4" ] ],
[ "sd1:SCK", "mega:52", "green", [ "h259.2", "v239.99" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Remote controlled RGB LED | #include <IRremote.h>
const int IR_PIN = 14;
const int RED_PIN = 2;
const int GREEN_PIN = 3;
const int BLUE_PIN = 4;
IRrecv irrecv(IR_PIN);
decode_results results;
void setup() {
irrecv.enableIRIn(); // Start the receiver
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFF30CF:
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
break;
case 0xFF18E7:
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
break;
case 0xFF7A85:
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
break;
default:
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
break;
}
irrecv.resume(); // Receive the next value
}
} | {
"version": 1,
"author": "",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -19.2, "left": 120.04, "attrs": {} },
{ "type": "wokwi-ir-receiver", "id": "ir1", "top": -68.55, "left": -1.78, "attrs": {} },
{ "type": "wokwi-ir-remote", "id": "remote1", "top": 105.6, "left": -192, "attrs": {} },
{
"type": "wokwi-rgb-led",
"id": "rgb1",
"top": -120.8,
"left": 106.7,
"attrs": { "common": "cathode" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 81.6,
"left": 57.05,
"rotate": 90,
"attrs": { "value": "220" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "r1:1", "rgb1:COM", "black", [ "h0", "v-163.2" ] ],
[ "r1:2", "esp:GND.1", "black", [ "h0", "v18" ] ],
[ "rgb1:G", "esp:D3", "green", [ "v0" ] ],
[ "rgb1:R", "esp:D2", "red", [ "v0" ] ],
[ "rgb1:B", "esp:4", "blue", [ "v0" ] ],
[ "ir1:VCC", "esp:3V3", "red", [ "v0" ] ],
[ "ir1:GND", "esp:GND.1", "black", [ "v0" ] ],
[ "ir1:DAT", "esp:14", "green", [ "v0" ] ]
],
"dependencies": {}
} |
Generate a diagram.json code for Tilt sensor alarm usng esp-32 | #include <Arduino.h>
const int tiltSensorPin = 4; // GPIO pin connected to the tilt switch
const int buzzerPin = 13; // GPIO pin connected to the buzzer
void setup() {
pinMode(tiltSensorPin, INPUT_PULLUP); // Set tilt switch pin as input with pull-up resistor
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
Serial.begin(115200);
}
void loop() {
int sensorValue = digitalRead(tiltSensorPin);
if (sensorValue == HIGH) {
// Tilt detected, turn on buzzer
digitalWrite(buzzerPin, HIGH);
Serial.println("Tilt detected! Buzzer ON.");
} else {
// No tilt, turn off buzzer
digitalWrite(buzzerPin, LOW);
Serial.println("No tilt detected. Buzzer OFF.");
}
delay(100); // Short delay to avoid rapid changes
} | {
"version": 1,
"author": "Ananoymous maker",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": -64.8,
"left": -123,
"attrs": { "volume": "0.1" }
},
{
"type": "wokwi-tilt-switch",
"id": "btn1",
"top": -114,
"left": -11.2,
"attrs": { "color": "green" }
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "bz1:2", "esp:13", "red", [ "v0" ] ],
[ "bz1:1", "esp:GND.1", "black", [ "v134.4", "h96" ] ],
[ "btn1:GND", "esp:GND.3", "black", [ "h38.4", "v182.4" ] ],
[ "btn1:VCC", "esp:3V3", "red", [ "h19.2", "v76.6", "h-105.6", "v38.4" ] ],
[ "btn1:OUT", "esp:4", "green", [ "h48", "v220.5" ] ]
],
"dependencies": {}
} |
Subsets and Splits