Hello! I need help to solve an arduino program performed on Tinkercad. It doesn't work. If anybody can find the mistakes and tell me the correction I will thank. The program pretends to emulate a run - stop engine based on grafcet.
No thank You. That’s asking for too much.
Post the code here, schematics for the hardware, and better replies can be expected.
Hello , sir ! Thanks for attending my requirement. I paste code:
// C++ code
//
bool buttonState0;
bool buttonState1;
int grafcet_stage_0=1;
int grafcet_stage_1=0;
void setup()
{
Serial.begin (9600); //iniciamos monitor serial a 9600 baudios
pinMode(13, INPUT);
pinMode(12, INPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
grafcet();
outputs();
}
void grafcet()
{
buttonState0 = digitalRead(13);
buttonState1 = digitalRead(12);
if (grafcet_stage_0 == HIGH && buttonState1 == LOW ){
grafcet_stage_0=0;
grafcet_stage_1=1; }
if (grafcet_stage_1 == HIGH && buttonState0 == LOW ){
grafcet_stage_0=1;
grafcet_stage_1=0; }
}
void outputs()
{
if (grafcet_stage_0==1 ){
Serial.println("Estado 0 activado ");
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
if (grafcet_stage_1==1 ){
Serial.println("Estado 1 activado ");
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
}
}
Put your code in a <CODE> block. See post #3.
You must describe this. "doesn't work" can be too many possibilities.
I believe your problem is:
- your wiring of the buttons, or/and
- the wiring of your LEDs, or/and
- you tried this without a limiting resistor, destroying the LEDs
It works in the Wokwi simulator...
State 0
State 1
[aside] If Wokwi ever wants suggestions; it would be nice to have the "attrib" if an LED passes over-current, the LED is destroyed, turns brown, "x" over LED eyes and a frowny face[/end aside]
// C++ code
//
bool buttonState0;
bool buttonState1;
int grafcet_stage_0=1;
int grafcet_stage_1=0;
void setup()
{
Serial.begin (9600); //iniciamos monitor serial a 9600 baudios
pinMode(13, INPUT);
pinMode(12, INPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
grafcet();
outputs();
}
void grafcet()
{
buttonState0 = digitalRead(13);
buttonState1 = digitalRead(12);
if (grafcet_stage_0 == HIGH && buttonState1 == LOW ){
grafcet_stage_0=0;
grafcet_stage_1=1; }
if (grafcet_stage_1 == HIGH && buttonState0 == LOW ){
grafcet_stage_0=1;
grafcet_stage_1=0; }
}
void outputs()
{
if (grafcet_stage_0==1 ){
Serial.println("Estado 0 activado ");
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
if (grafcet_stage_1==1 ){
Serial.println("Estado 1 activado ");
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
}
}
You should "format" your code before pasting... compare your code (flat along left edge) to my code (indents).
Your buttons are wired incorrectly I think. Maybe you should use INPUT_PULLUP in your code.
This is a guess because you didn't post your schematic. Your tinkercad link is useless, I cannot access it.
Were you able to understand the suggestions? Your code "works" but we think you have some connection mistakes.
Many thanks the people who answered me! I'll try your suggestions .
Hello!
You're showing me a software that I hadn't listened about it never before. WOKWI . I have it in consideration. Thanks !
AVR chips like on the Uno process 8-bit variables twice as fast as 16-bit variables. For small positive integer values like pin numbers, use type byte variables instead of type int. That also saves RAM, a good habit to develop with limited RAM MCU’s.


