[SOLVED] Easy Javascript game does not trigger photon

Hey guys,
My goal is to trigger the photon’s led when you are playing an easy javascript game called Simon. When the game is over, the led should turn off. I just started with programming and I’m learning every day. I found the game on Github and wanted to let it interact with my photon. The javascript of the game is telling me this:

endGame: function() {
  // notify the user that they lost and change the "round" text to zero
  $('p[data-action=lose]').show();
  $($('[data-round]').get(0)).text('0');
},

So I should do something with the data-action=lose, because this is the trigger that the game is over.
I adapted a piece of JavaScript code if found on ‘the particle community’ about turning on and off a led with html buttons. (https://community.particle.io/t/javascript-turn-on-led/10589) Still it doesn’t work. The standard ‘web-connected-led’ is uploaded on my photon (https://docs.particle.io/guide/getting-started/examples/core/#control-leds-over-the-39-net).
When you press the start button of the game, the game starts and the led starts to shine, so particle and website have a connection. ({“id”:“xxxx”,“last_app”:"",“connected”:true,“return_value”:1}
When the game ends, the led should go out, but the photon doesn’t get any input. Can someone please help and tell me what’s wrong with the code below that is part of my html code?
Thanks! (:

<script language="javascript" type="text/javascript">

  setInterval(led, 1000);
  var deviceID    = "xxxx";
  var accessToken = "xxxx";
  var setFunc = "led";

  function led() {
    function ledToggle(led,newValue) {
      if (document.getElementById("p").data('action') == "lose") {
        paramStr = "off";

      }
      var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + setFunc + "/";
      $.post( requestURL, { params: paramStr, access_token: accessToken });
    }
  }

</script>

Why not place the function call in the ‘endGame’ function?

2 Likes

Thanks a lot for the fast answer! I followed your advice and got this. I hope I did what you meant.

endGame: function() {
  // notify the user that they lost and change the "round" text to zero
  $('p[data-action=lose]').show();
  $($('[data-round]').get(0)).text('0');
  curl https://api.particle.io/v1/devices/xxxx/ledToggle \
 -d access_token=xxxxxx \
 -d "args=off"
},

Now the game stoped working. In my Chrome Console, i see this.

Uncaught TypeError: Cannot read property 'init' of undefined
    at HTMLDocument.<anonymous> (main.js:18)
    at l (jquery.min.js:4)
    at Object.fireWith [as resolveWith] (jquery.min.js:4)
    at Function.ready (jquery.min.js:4)
    at S (jquery.min.js:4)


    require(["jquery", "app/simon"], function($, simon) {
        $(document).ready(function() {
    		simon.init(); (HERE IS A RED CROSS)
        });

How can I solve this? Should I attach a library or something? I’m sorry but my knowledge is not that big about this jet.
Thanks! (:

That CURL part looks nothing like the function call you had above, and can’t be used as such. Give the method above a shot

2 Likes

I tried your call-fuction in the ‘endGame’ Javascript function. Is this the correct way to do it? I can you give me advice to make it better.The game still don’t react to the last trigger and the led stays on, even after the game. When I use the function ledToggle to call, it gives the same effect.
Thanks!(:

endGame: function() {
			// notify the user that they lost and change the "round" text to zero
			$('p[data-action=lose]').show();
			$($('[data-round]').get(0)).text('0');	
			led.call() ;
		},

or

endGame: function() {
			// notify the user that they lost and change the "round" text to zero
			$('p[data-action=lose]').show();
			$($('[data-round]').get(0)).text('0');	
			ledToggle.call(led,newValue) ;
	},

This might help:

I made some functions for calling a Particle function in Javascript awhile ago.

1 Like

I worked it out by using by placing the ‘ledToggle’ in the ‘endGame’ javascript function.

endGame: function() {
			ledToggle();
			// notify the user that they lost and change the "round" text to zero
			$('p[data-action=lose]').show();
			$($('[data-round]').get(0)).text('0');	
			
		},

after that, I placed this piece of code on the html page:

<script language="javascript" type="text/javascript">	

			function ledToggle(led,newValue) {
			
				$.post( "https://api.particle.io/v1/devices/xxxxx/led?access_token=xxxx",{ arg: "off"});
			}
	
		</script>

This worked and my led went off. I putted the complete link in the POST with behind it the argumentation what the Photon should do. I know it’s maybe not the cleanest way, but it works for my project.
Thanks for the fast response!

1 Like